Display Animated GIF

后端 未结 30 2409
无人及你
无人及你 2020-11-22 02:11

I want to display animated GIF images in my aplication. As I found out the hard way Android doesn\'t support animated GIF natively.

However it can display animations

30条回答
  •  没有蜡笔的小新
    2020-11-22 02:56

    Easiest way - Can be consider the below code

    We can take advantage of Imageview setImageResource , refer below code for the same.

    The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

    image_1.png, image_2.png, etc.

    Have the handler to change the image dynamically.

    int imagePosition = 1;
        Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                public void run() {
                    updateImage();
                }
            };
    
    
    
    
        public void updateImage() {
    
                    appInstance.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                            gifImageViewDummy.setImageResource(resId);
                            imagePosition++;
        //Consider you have 30 image for the anim
                            if (imagePosition == 30) {
    //this make animation play only once
                                handler.removeCallbacks(runnable);
    
                            } else {
        //You can define your own time based on the animation
                                handler.postDelayed(runnable, 50);
                            }
    
    //to make animation to continue use below code and remove above if else
    // if (imagePosition == 30)
    //imagePosition = 1;
    // handler.postDelayed(runnable, 50);
    // 
                        }
                    });
                  }
    

提交回复
热议问题