I'm currently working on an app where people can place objects in a SurfaceView -- images etc and they can resize it and things like that. Now i have a use case where someone can place in a frame like image -- lets say its a square image with the center as transparent. Now they can set an image that can go inside this frame and will be restricted to the bounds of that frame only and from there they can resize it as they will. Anyone does an implementation like this? Note that this is different from when a frame is full screen, that is easy to do. See my sample below:
The green is the main background area of the SurfaceView, he added the maroon/red frame, now he sets the picture to go in the frame (this is all within the same surface). The main difference this time around is that when he resize/move the image here, it should be bounded by the red frame ( he wont see it overlap outside like in the picture below). Anyone have experience how to do this on Android.?
Use the clipRect() method of the Canvas. You can supply it with a rectangle specifying your frame before you draw the image to be framed. Something like this should work:
First when moving the frame you need to have a corresponding Rect:
myRect.set(frameleft, frametop, frameright, framebottom);
And in your onDraw() you clip the picture with this rectangle:
canvas.save(Canvas.CLIP_SAVE_FLAG);
canvas.clipRect(pictureRect);
canvas.drawBitmap(/*your drawing parameters for the image*/);
canvas.restore(); //Restore to drawing the entire canvas
Related
is there any way to blur only part of an image in android?
searched a lot but dint find any help.
most of the examples use gaussian blur which blurs full imageview.
i want to allow user to dynamically draw rectangle on imageview & on action up
area within rectangle should be blured.
any help will be really appreciated.
Bluring images on the fly is not an easy task, ask Roman Nurik (The one behind Muzei app)
He gave useful tips on this Google+ post but it's for animated images, from focus to blur.
In your case, I would say that you need to (roughly):
Get the bounds of the drawn rectangle
Get the image part that corresponds to the previous bounds
Blur on the fly the previous image part
Draw, into the same canvas, the blurred image part on top of the original image
Set up a onDrag Listener to move the drawn rectangle and do again step 1
EDIT: After re-thinking about it, computing and draw a blurred area each time the drawn rectangle move it too heavy, it won't work. The solution is probably this:
Blur the entire image and keep it into memory
Get the bounds of the drawn rectangle
Get the part of the blurred image that corresponds to the previous bounds
Draw, into the same canvas, the blurred image part on top of the original image
Set up a onDrag Listener to move the drawn rectangle to do again step 2-3-4
put the image view in a relative layout.
you detect the touch events of the user.
for each rectangle that he is drawing, you add an image view of it is size superposed to the initial one (I mean in the same relative layout) and of course with your blur effect.
you will see your image view blured part by part ...
put another layout on the half part of the image and set a translucent type background to the layout.
Once you have drawn your rectangle set alpha = 0.5 or as per your need so that your dynamic view that you have drawn will look blurred.
This is code for blur:
http://shaikhhamadali.blogspot.in/2013/07/gaussian-blur-imagebitmap-in-imageview.html
In this code computeConvolution3x3 method is used for computing the blur.For computing blur it convert the bitmap in to pixel array then it apply on those pixel. So you have to just do is get pixels array of that part of the image that you want blur.
I need a camera function on my App. This is what I need:
(From barcode-scanner but I need something for making pictures)
I need a transparency margin. Now when I would press "shot photo" I need exactly this:
Someone know a tool like that?
Thanky in advance.
You're going to have to map that out yourself and crop the bitmap.
You can draw the boundaries of the transparency window on the Canvas of the FrameLayout your camera's view.
From there, you grab the position of the transparency window in relation to the screen and then use those boundaries to cut out the desired area from the bitmap returned by the camera.
In my android application I am stuck in a problem and nothing seems to work for me.
I have an ImageView on the top of another ImageView inside a relative layout.
Now I need to resize the imageview on top when user touches one of its corners and drags.
Just like a cropping frame we generally see. When we drag any one corner, then the diagonally opposite corner must remain fixed and the resizing must be done across the corner which is being dragged.
What I am doing is setting OnTouchListener and getting new/dragged coordinates on Action.MOVE then I tried to resize using Bitmap's createScaledBitmap. This does resize the image view but not across the corner which is being dragged. I am totally confused .
How I can use the coordinates to draw an Image View just like we do it while drawing a rectangle using Canvas.
Please help.
I wouldn't do this in an ImageView. I would subclass View and override onTouchEvent and onDraw to handle the input and draw all the various components. You have to break this down into it's components and manage a number of objects in this view.
You have a Rect that represents the size of the crop area. This probably defaults to the size of the control. In onTouchEvent, you need to test for an area around each corner and then keep track of which corner is being dragged to resize your Rect appropriately.
You don't have to call createScaledBitmap each time you draw it, and you probably shouldn't because you are flirting with an OutOfMemoryException at that point (clean up your Bitmaps too slowly and you'll find out the hard way what this is). Just decode the Bitmap when the control gets created and draw it to the canvas using a destination Rect.
Lots of code to write, but it sounds like a fun project. There's no easy way to drop in a control like this (if I'm understanding you correctly). You have to manage the touches, drags, and the destination rectangle inside the custom View.
in my application I want to have a frame (with a hole in the middle) and an image under it. I want to be able to drag and zoom the image to fit (the face in the picture) it in the frame. Then Crop the image (could I find the pixel x,y that are in the border of the screen?) and then save the zoomed image so that I can put the frame over it then.
Any suggestions? If possible please tell me the classes I will have to use.
Thanks!!
It is possible to do all the functionalities (What you have discussed above) using Android View class with onDraw method and canvas Object.
Refer the link:
http://developer.android.com/reference/android/graphics/Canvas.html
What I'm trying to do is have a background image, for sake of simplicity, lets say it's a picture of the front of a house. Then, I want to have a red ball move from window to window.
**I want to have a background picture, and a picture on top of it.
**I then want to be able to tell the top picture EXACTLY where to go.
How can I do this?
I'm just beginning to learn about animations in Android, and have not yet run across any way to do this.
There are two routes to animation in android: Canvas and OpenGL ES.
I would recommend OpenGL for anything requiring smoothness and speed, like a moving ball.
You should create a view using the helper class GLSurfaceView
http://android-developers.blogspot.com/2009/04/introducing-glsurfaceview.html, and implement a Renderer.
I assume you have the images saved in your res/drawable folders, in a format like png and the ball file contains an alpha channel.
You can see many tutorials online, but basically you need to load your background image and your ball resource at onSurfaceCreated and store it in a texture using GLUtils.texImage2D.
In the onDrawFrame method, you should set up a 2D projection such as glOrtho2D, then draw the background.
Then just before you draw the ball texture, you can use the glTranslate(x,y,0) function to move the ball over the house. Use an alpha blend for the ball:
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA);
glEnable(GL_BLEND);
Unfortunately writing in OpenGL isn't as straightforward as you might hope. Everything is done with 3D coordinates, despite the fact you want only a 2D image. But hopefully this gives you enough info to google for good exmaples, which are abundant!