帧动画frame

帧动画的类

 @SuppressLint("NewApi") public class MainActivity extends Activity implements OnClickListener{

    private ImageView img;
    private Button jq;
    private Button jr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img=(ImageView)findViewById(R.id.imageView1);
        jq=(Button)findViewById(R.id.button1);
        jr=(Button)findViewById(R.id.button2);
        jq.setOnClickListener(this);
        jr.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        AnimationDrawable animation;
        switch (v.getId()) {
        case R.id.button1:
//setBackground方法需要API16现在是8,需要解决 @SuppressLint("NewApi")
    img.setBackground(getResources().getDrawable(R.drawable.draw));
            animation = (AnimationDrawable) img.getBackground();
            animation.start();
            break;
        case R.id.button2:
            img.setBackground(getResources().getDrawable(R.drawable.jrdraw));
             animation=(AnimationDrawable) img.getBackground();
             animation.start();
            break;

        default:
            break;
        }
    }


}

xml文件

<?xml version="1.0" encoding="utf-8"?>
 <!-- oneshot 默认为false 发射多次 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
  <item android:drawable="@drawable/w0" android:duration="200"/>
  <item android:drawable="@drawable/wif0" android:duration="200"/>
  <item android:drawable="@drawable/wif1" android:duration="200"/>
  <item android:drawable="@drawable/wif2" android:duration="200"/>
  <item android:drawable="@drawable/wif4" android:duration="200"/>
  <item android:drawable="@drawable/wif5" android:duration="200"/>

</animation-list>

java代码添加动画

@SuppressLint("NewApi")
 public class MainActivity extends Activity implements OnClickListener {

    private ImageView img;
    private Button stop;
    private Button start;
    int draw[] = { R.drawable.p39_d21, R.drawable.p39_d22, R.drawable.p39_d23,
            R.drawable.p39_d24, R.drawable.p39_d25, R.drawable.p39_d26,
            R.drawable.p39_d27, R.drawable.p39_d28, R.drawable.p39_d29,
            R.drawable.p39_d30 };
    private AnimationDrawable animationDrawable;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        start.setOnClickListener(this);
        stop.setOnClickListener(this);

    }

    private void init() {
        img = (ImageView) findViewById(R.id.imageView1);
        start = (Button) findViewById(R.id.button1);
        stop = (Button) findViewById(R.id.button2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            animationDrawable = new AnimationDrawable();
            for (int j = 0; j < draw.length; j++) {
            .AnimationDrawable.addFrame(Drawable frame, int duration)
                animationDrawable
                        .addFrame(getResources().getDrawable(draw[j]), 50);
            }
            animationDrawable.setOneShot(false);
            img.setBackground(animationDrawable);
            animationDrawable.start();
            break;
        case R.id.button2:
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
            }
            break;

        default:
            break;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值