如何在Android动画中将图像加载到imageview?

时间:2012-07-11 09:11:19

标签: android animation

我做了如下

imageView = (ImageView) findViewById(R.id.animation_iv);
imageView.setImageResource(R.drawable.loadinganim);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();

我在loading.xml中有复制图像

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="false">  
    <item android:drawable="@drawable/v1" android:duration="160" />  
    <item android:drawable="@drawable/v4" android:duration="160" />  
    <item android:drawable="@drawable/v7" android:duration="160" />  
</animation-list>

它在galaxy note中得到了支持。如果任何人有想法请帮帮我。谢谢提前

2 个答案:

答案 0 :(得分:0)

你必须这样做:

    imageView.setBackgroundResource(R.drawable.loading);
    final AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground(); 
    startAnimation(frameAnimation);

startAnimation方法是:

  public void startAnimation(final AnimationDrawable frameAnimation){
    new Thread(new Runnable() {
        public void run(){
            // some code that runs outside the ui thread.
            frameAnimation.start();
        }
    }).start();
  }

我认为在UI线程以外的线程上运行动画将解决您的问题。

答案 1 :(得分:-1)

AnimationDrawable imageAnimation;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);


    ImageView image = (ImageView) findViewById(R.id.test_image);
    image.setImageResource(R.drawable.loading);
    imageAnimation=(AnimationDrawable) image.getDrawable();

 }

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    imageAnimation.start();
  }