How to increase Canvas size in Android? - android

Is there anyway to increase Canvas size more then my screen size in Android? Like if my screen size is 320 x 480 I want to increase my canvas size 3 times bigger then that.

You can create a bitmap of desired size and then draw needed part of it to canvas using drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint) method of canvas.

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 - canvas drawBitmap displaying top left quarter of image

When I call drawBitmap with the (Bitmap bitmap, Rect src, Rect dst, Paint paint) implementation, I only get the top left quarter of the image displayed - my source image is 16x16 but only 8x8 is displayed.
rSource = new Rect();
rDest = new Rect();
rSource.set(0,0,16,16);
rDest.set(0,0,16,16);
canvas.drawBitmap(tiles, rSource, rDest, null);
Managed to sort this out myself. Read up on device independent pixels vs. physical pixels. As I'm using an XHDPI device, everything is x 2 of the baseline MDPI.

Android: Canvas drawBitmap?

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);

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.

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().

Categories

Resources