1

我正在制作一个包含帧动画的 android 项目。我的动画在 4.0 中运行良好,但在 2.2 中没有显示。有什么办法可以让它在 2.2/2.3 中工作吗?2.2 的任何工作代码片段都会很棒。

如果需要,可以发布我的代码。

编辑: 这是我的工作代码

public class FrameAnimationExample extends Activity {
     AnimationDrawable animation;
    @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

           Button btnStart = (Button) findViewById(R.id.btnStart);
           final ImageView imgView = (ImageView)findViewById(R.id.img);

           btnStart.setOnClickListener(new View.OnClickListener() {
             @Override
              public void onClick(View v) {
                 startAnimation();
              }
           });
           imgView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

       }
        });
       }

    class Starter implements Runnable {
          public void run() {
               animation.start();
           }
       }

       private void startAnimation(){
           animation = new AnimationDrawable();
           animation.addFrame(getResources().getDrawable(R.drawable.hud_bubble_fill_line), 100);
           animation.addFrame(getResources().getDrawable(R.drawable.hud_bubble_fill), 100);
           animation.addFrame(getResources().getDrawable(R.drawable.medal_brown), 100);
           animation.addFrame(getResources().getDrawable(R.drawable.medal_silver), 100);
           animation.addFrame(getResources().getDrawable(R.drawable.medal_gold), 100);
           animation.setOneShot(true);

           ImageView imageView = (ImageView) findViewById(R.id.img);
           RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
           params.alignWithParent = true;
           params.addRule(RelativeLayout.CENTER_IN_PARENT);       

           imageView.setLayoutParams(params);
           imageView.setImageDrawable(animation);
           imageView.post(new Starter());
       }
    }
4

1 回答 1

0

你可以试试

// animation call
ImageView img = (ImageView) findViewById(R.id.img);
img.post(animateMe);

final Runnable animateMe = new Runnable() {

@Override
public void run() {
    ImageView imageView = (ImageView) findViewById(R.id.img);
    imageView.setBackgroundResource(R.anim.img_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
    }
};

并使用 img_animation.xml 像:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/selected" 
    android:oneshot="true">

<!-- ur anim images here -->
<item android:drawable="@drawable/hud_bubble_fill_line" android:duration="100" />
<item android:drawable="@drawable/hud_bubble_fill" android:duration="100" />
<!-- and so on -->

</animation-list>
于 2013-11-21T17:38:28.583 回答