Android: Canvas drawBitmap? - android

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!

You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);

Related

Canvas Paint text size android

I have a mutable Bitmap and I'm drawing it on a Canvas. After that I want to draw a pre-defined text on the bitmap. The problem is I have different bitmap sizes, even though I'm setting the text size to 20sp, depending on the bitmap size the text is bigger of smaller, but I want the text size to be the same for ALL images. What Should I do? I thought of maybe scaling the text size depending on the image width and weight but I'm not sure how to do that.
final Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
final Canvas canvas = new Canvas(mutableBitmap);
final Paint paint = new Paint();
paint.setTextSize(getResources().getDimensionPixelSize(R.dimen.myFontSize);
canvas.drawText(text, x, y, paint);
First of if this is your onDraw method you are doing wrong work here. You shouldn't allocate anything inside the onDraw method. On the other hand , you shouldn't change your text size for bitmaps. Resize your bitmaps on same values(width and height) , then setting the text for same value.

Android Animation with spritesheet

I want to show a sequence of animation(background Transparent) by using a spritesheet (generated by Texturepacker). is there any other engine(way) to show sprite sheet animation other than AndEngine?
If you're drawing the bitmap using canvas you can call
public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
The bitmap is the spritesheet, the src Rect would be the individual sprite you would wish to show from the spritesheet. So if your spritesheet is a 100x100 bitmap of 16 25x25 px sprites you would use a rect of 0,0,25,25 to draw the first frame.
The dst Rect is the actual x/y coordinates and dimension of drawn sprite onto the canvas. Note you're able to change the original sprite dimensions and the canvas with automatically scale the sprite for you.
Now to animate the spritesheet all you need is some code to change the src Rect every time the frame index should increment.

transform a sqaure shape drawable in to circular shape drawable withaout affecting the pixel quality

I have an application which takes picture from camera .And normally this pictures have either rectangle shapes or square shapes.But due to make these pictures circular in shape i limited the shape to only square.So now my problem is to make this square picture into a circular shape having radius of half of width or height.And the clipped part should be removed from the picture.
Its same like making a circle from the square which touches midpoint of all of the faces of square.
I don't have any experience with canvas or other in built drawing functions.
For example i have square at first now i want to make image circular such that parts pointed by arrow should get clipped from image.
Note:as i have to work with camera images .quality is crucial factor.So please suggest the possible ways that will not affect the pixel quality and other important factors.
You can do it by masking the image with your desired shape, see this post for detailed info.
example code:
Resources resources = context.getResources();
Bitmap original = BitmapFactory.decodeResource(resources,R.drawable.original);
Bitmap mask = BitmapFactory.decodeResource(resources,R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
c.drawBitmap(original, 0, 0, null);
c.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
imageView.setImageBitmap(result);

How do I draw a flipped subset of a bitmap on Android?

I'm using drawBitmap(Bitmap image, Rect src, Rect dst, Paint paint); to draw a portion of an image. Is there a way to do this and also flip the image?
Yes, image is a matrix, so assuem its 4x4 pixel image. so extract block wise pixel. Once you get a portion of image i think then you can apply any matrix transformation on image like rotateX().

Editing the portion of canvas

I want to cut the selected portion of bitmap and i have to paste it in the same bitmap using .. please help me out..
You can do it by generating an second Bitmap with the portion the user selected. Them to draw, just use the Canvas of the first. It's not so hard.
A tip is: The Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This draws a portion of the Bitmap limited by the Rects args.

Categories

Resources