有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java逐部分设置单个图像的动画

我有一个图像“笑话.jpg” enter image description here

我想一帧一帧地制作动画。我已经能够使用不同帧的不同图像使用帧动画制作动画

但是我想一部分一部分地给这个图像设置动画。 此图像的总大小为2400*320。 所以基本上我的意图是把这张图片分成5帧,并制作动画


共 (3) 个答案

  1. # 1 楼答案

    您可以使用onWindowFocusChanged()方法来完成。所以,在这个方法中,你可以这样放置:

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

    在xml布局中,可以使用:

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

    字体:Starting Frame-By-Frame Animation

  2. # 2 楼答案

    我的问题解决了

    我做了一些类似于跟随的事情

    /***************************** 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;
    
    }
    

    其中ArrayList<Bitmap> chunkedImages;是全局声明的