I know we can crop a bitmap in rectangular shape using:
Bitmap.createBitmap(source, x, y, width, height)
But I want to know to crop a bitmap in freehand mode or some polygon shape. Its best example can be seen in Jigsaw Puzzle game
I have spent a lot of time googling about this but found no help. Can anybody help me in this regard?
You can use a Path to determine a clip area on your Canvas when drawing a Bitmap.
For a full example see:
Android - Crop an image from multipoints
and also:
Cutting a multipoint ploygon out of Bitmap and placing it on transparency
Related
I need to draw blur on image view and UnBlur also. I tried with RenderScript but I got full image blur,can any one help me regarding libraries or coding.Like below image
Thank you, enter image description here
This is a nice blurring library - https://github.com/500px/500px-android-blur
If you write a blurring function yourself, crop only the area you need by creating a new bitmap - Bitmap.createBitmap(sourceBitmap, startX, startY, width, height)
Hello everyone I am new to android and I just have a quick question. I am making an animation using Bitmaps and drawing them to the canvas using the onDraw(). I am trying to draw a background to the canvas that I made in photoshop. I already uploaded it as a drawable and then decoded it to a Bitmap then added it to the onDraw method. It draws, but I doesn't fill the entire canvas like I want it to. Any suggestions and or fixes? Thank you so much, I looked around and saw something about converting the bitmap to an image view but didn't really understand all to well.
Use drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) where the rect for source is the entire bitmap (from 0,0 to width, height) and dest is the entire canvas. This will stretch the bitmap to fill the canvas. Be warned this can cause the aspect ratio to be off and/or issues with fuzziness or blockiness.
I am trying to rotate a bitmap and crop out a valid Rectangle out of it, and then save it on the disk. What i am trying to do is explained in the following link:
http://i.stack.imgur.com/IIhBw.png
Black is the original image. Red is rotated image (by 15 degrees here) and green is the valid portion of the red image.
This feature is similar to the Image straightening which Instagram app does.
So far, I have tried this:
public static Bitmap getRotatedBitmap2(Bitmap bmp, int rotation){
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, false);
return bitmap;
}
This rotates the bitmap but it shrinks in size, also the rotated bitmap is enclosed is an black rectangle. i need to get rid of the out-bounding black rectangle. Also, How can I obtain the valid portion of bitmap?
Aviary and ImageMagick are libraries that can help to achieve the results asked in the question, as well perform other Bitmap operations and Image Manipulations.
I know you already solved this question, but if somebody is looking for another solution, you can check this library image-rotator that solves problem like this or very similar.
How can I crop a triangle from an image (given the degree) using it's center.
for e.g. In the below image, fig_1 is the original image. I want to cut it like fig_2, fig_3 and fig_4 where, x, y and z are the degree of the angles.
Any help would be appreciated. Thanks.
Not sure if this is the best solution but you might want to use the BitMap class. First write the contents of the image to a Buffer, then use copyPixelsFromBuffer(Buffer) in BitMap class to get a BitMap. Then write the logic to edit the BitMap, e.g. you can use eraseColor(int). Then copy your edited BitMap to an ImageView using setImageBitMap(BitMap) method in ImageView class.
It seems to be a very trivial thing, but I am not able to find a sample code for it.
I am trying to do a very simple thing. On a canvas I plan to draw a round rect and save that in a drawable which I can then set as a background of a button.
Now I am looking for 2 things, how to draw and create a round rect so that the transparency effect can be created (like in a png drawable)
Second how to save the canvas to an image.
Please help
Please try to use this code.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.color.transparant);
canvas.drawBitmap(bitmap, matrix, paint);