How to animate .gif images in an android?

前端 未结 6 1691
一整个雨季
一整个雨季 2020-12-05 05:17

Here is the code for xml:



        
6条回答
  •  忘掉有多难
    2020-12-05 06:09

    It's not good practice to use Threads (i.e. View.post(new Runnable)) because the view might have changed during the time the drawable is going to be painted (one case is using the animated image on ListView with items containing different background images), which may cause a ClassCastException if the ImageView, by the time the thread runs, has a background that is not an animated resource.

    ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
    loadingImg.setBackgroundResource(R.drawable.progressdialog);
    loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
      @Override
      public void onViewAttachedToWindow(View v) {
        AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
        loadingAnimation.start();
      }
    
      @Override
      public void onViewDetachedFromWindow(View v) {
      }
    });
    

    Example shown here

提交回复
热议问题