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().
Related
I want to calculate center of a bitmap that is drawn on a canvas with a matrix, it can be rotated, scaled or translated with a arbitrary value. What is the easiest way to find center of the this bitmap on canvas?
You need to apply the matrix to the coordinates of the center of bitmap.
If you use a canvas that has a transformation matrix, you can get the final matrix through Canvas.getMatrix()
If you draw the Bitmap on the Canvas with a Matrix : drawBitmap(bitmap, matrix, paint), then you you need to use that Matrix, or concatenate it to that of the Canvas (in the case it has one).
Then you can finally apply that matrix to the center of the matrix using Matrix.mapPoints.
Something like :
canvas.drawBitmap(bitmap, bitmap_transform, paint);
Matrix full_transform = new Matrix(canvas.getMatrix());
full_transform.postConcat(bitmap_transform);
float[] center = new float[] {bitmap.getHeight()/2.0, bitmap.getWidth()/2.0};
full_transform.mapPoints(center);
Alternatively, if you apply transformations to your bitmap without a matrix, you can use the full_transform.postRotate, full_transform.postScale, etc with the same values. In particular, if you draw your bitmap with drawBitmap(bitmap, left, top, paint) then you need to do a full_transform.postTranslate(left, top).
If you're looking for the "easiest" way then just sticking with the translate rotate and scale functions would solve the problem. The reason that those were developed was so developers wouldn't have to do vector calculus for simple animations. Also the only value you would have to actually calculate in that sense it is the translate value after you take into account the original coordinates.
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);
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.
I have a bunch of bitmaps that I draw on a Canvas applied with 2D transformation (android.graphics.Camera and android.graphics.Matrix). How do I get the sizes/positions of the bitmaps on Canvas? The size would be the bounds of the bitmap as rendered on Canvas and the position would be the coordinates of the top/left corner of the bounds. All the bitmaps are regular rectangles without an alpha channel.
The matrix class has a mapRect function for this purpose:
mapRect(RectF dst, RectF src)
Apply this matrix to the src rectangle, and write the transformed rectangle into dst.
Give the dimensions of the image as the source (and 0,0 for the x,y if you're using the whole image) and it will give you the transformed dimensions back as the dst rectangle.
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.