简体   繁体   中英

Animating single image part by part

I have an image "joke.jpg" 在此处输入图片说明

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

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

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

It looks like you are developing a game ?

Here is a sprite animation example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM