Android Animation with spritesheet - android

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.

Related

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

Drawing animated circle with OpenGL ES 2.0

I have a circle object, created with OpenGL vertices.
When the circle is being touched, I want a scaling animation to be activated. The circle radius will grow bigger until it reaches a certain radius, and then the circle will transform back to its original radius.
What is the best way to implement this animation?
You have a few possibilities. The simplest would be to just create a variable which would hold a scaling factor for your circle and multiply all vertices by it before rendering. Then using some timers and adjusting the scaling factor in time, you could create your animation.
I should add that if you want to scale your circle by its center then you have to first translate the circle vertices so that circle's center will be at (0, 0) point, then scale by the factor and translate the circle back to its initial position.
If the circle animation you're trying to implement is some kind of your GUI element then another way which comes to my mind is to:
create an ImageView in xml or dynamically in code
draw the vertices on it
create an XML android animation and apply it whenever you want
Here's a draft of the code for drawing a circle on the ImageView:
Bitmap tempBitmap = Bitmap.createBitmap(yourCircleWidth, yourCircleHeight, Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);
//Draw the circle into the canvas
tempCanvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
tempCanvas.drawCircle(circleCenterX, circleCenterY, yourCircleRadius, paint);
//Attach the canvas to the ImageView
yourImageView.setImageDrawable(new BitmapDrawable(yourImageView.getResources(), tempBitmap));
For XML animations, here's a good reference:
http://www.androidhive.info/2013/06/android-working-with-xml-animations/

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

Get measured size and position of bitmap on canvas after 2D transformation

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.

How to increase Canvas size in 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.

Categories

Resources