0

I have an image "joke.jpg" enter image description here

I want to animate this frame by frame. I already able to animate using frame animation using different images for different frames.

But I want to animate this image part by part. Total size of this image is 2400 * 320. So basically my intention is to divide this image into 5 frames and animate it

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • [TRY THIS LINK][1] [1]: http://stackoverflow.com/questions/7341017/spritesheet-programmatically-cutting-best-practices.., ^_^ hope it could help!.., – zerocool Jan 17 '14 at 04:30

3 Answers3

0

You can do it using the onWindowFocusChanged() method. So, in this method you can put something like that:

ImageView img = (ImageView)findViewById(R.id.some layout);
AnimationDrawable frameAnimation = (AnimationDrawable)img.getDrawable();
frameAnimation.setCallback(img);
frameAnimation.setVisible(true, true);
frameAnimation.start();

In your xml layout, you can use:

<animation-list   android:id="@+id/my_animation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/frame1" android:duration="150" />
    <item android:drawable="@drawable/frame2" android:duration="150" />

</animation-list>  

Font: Starting Frame-By-Frame Animation

Community
  • 1
  • 1
0

My problem solved.

I did something like follow

/***************************** onCreate() ***********************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgJoke = (ImageView) findViewById(R.id.img_joke);


        AnimationDrawable animation = new AnimationDrawable();


        Bitmap bmpJoke = BitmapFactory.decodeResource(getResources(), R.drawable.joke);
        splitImage(bmpJoke, 5);

        int duration = 200;    

        for(Bitmap image: chunkedImages){
            BitmapDrawable frame = new BitmapDrawable(image);
            animation.setOneShot(false);
            animation.addFrame(frame, duration);
        }


        imgJoke.setBackgroundDrawable(animation);

        animation.start();
}
/*********** Method to split a single image ****************************/
private void splitImage(Bitmap bitmap, int chunkNumbers) {


        // For height and width of the small image chunks
        int chunkHeight, chunkWidth;

        // To store all the small image chunks in bitmap format in this list
        chunkedImages = new ArrayList<Bitmap>(chunkNumbers);

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
        bitmap.getWidth(), bitmap.getHeight(), true);

        chunkHeight = bitmap.getHeight();
        chunkWidth = bitmap.getWidth() / chunkNumbers;

        // xCoord and yCoord are the pixel positions of the image chunks
        int yCoord = 0;

        int xCoord = 0;
        for (int y = 0; y < chunkNumbers; y++) {
            chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord,
                    chunkWidth, chunkHeight));
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;

}

where ArrayList<Bitmap> chunkedImages; is declared globally

Kunu
  • 5,078
  • 6
  • 33
  • 61
0

It looks like you are developing a game ?

Here is a sprite animation example:

http://obviam.net/index.php/sprite-animation-with-android/

Ksharp
  • 102
  • 3