帧动画
程序员文章站
2022-03-01 20:53:33
...
package com.qy.day_android_15_zhendonghua;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.imageView1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
break;
case R.id.button2://java帧动画
//创建帧动画
AnimationDrawable animationDrawable=new AnimationDrawable();
//设置帧(参数1图片,参数2 这张图片在屏幕上显示多久)
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua1 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua2 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua3 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua4 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua5 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua6 ), 20);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.gua7 ), 20);
//把帧动画设置给图片
imageView.setImageDrawable(animationDrawable);
animationDrawable.start();
break;
}
}
}