帧动画
来源:互联网 发布:知乎 显微镜原理 编辑:程序博客网 时间:2024/06/14 11:08
public class MainActivity extends BaseActivity implements OnClickListener {
private static final int FLAG = 123;
private ImageView imgShow;
private TextView txtTime;
private Button btnStart;
private Button btnStop;
private AnimationDrawable animation;
private Timer timer;
/**
* 初始化视图相关控件
*/
@Override
protected void initView() {
setContentView(R.layout.activity_main);
imgShow = (ImageView) findViewById(R.id.img_show);
txtTime = (TextView) findViewById(R.id.txt_time);
// imgShow.setImageResource(R.drawable.ic_launcher);
btnStart = (Button) findViewById(R.id.btn_start_anim);
btnStop = (Button) findViewById(R.id.btn_stop_anim);
}
/**
* 初始化数据
*/
@Override
public void initData() {
// xml资源文件的形式实现动画效果
// animation = (AnimationDrawable)
// getResources().getDrawable(R.drawable.anim_frame);
// 代码方式实现帧动画效果
initAnimation();
timer = new Timer();
}
/**
* 设置监听器
*/
@Override
public void setListeners() {
super.setListeners();
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
/**
* 其他操作
*/
@Override
public void setMoreAction() {
super.setMoreAction();
imgShow.setImageDrawable(animation);
}
/**
* 初始化动画,添加每一帧
*/
private void initAnimation() {
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.img0), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img1), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img2), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img3), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img4), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img5), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img6), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img7), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img8), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img9), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img10), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img11), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img12), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img13), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img14), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img15), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img16), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img17), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img18), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img19), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img20), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img21), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img22), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img23), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img24), 50);
// 设置单次播放
animation.setOneShot(true);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case FLAG:
int total = (Integer) msg.obj;
if (total < 0) {
return;
}
txtTime.setText("倒计时:" + total + "秒");
if (total == 0) {
animation.start();
}
break;
default:
break;
}
};
};
TimerTask task = new TimerTask() {
int total = 10;
@Override
public void run() {
total--;
Message msg = Message.obtain();
msg.what = FLAG;
msg.obj = total;
handler.sendMessage(msg);
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start_anim:
timer.schedule(task, 0, 1000);
// animation.start();
break;
case R.id.btn_stop_anim:
if (animation.isRunning()) {
animation.stop();
}
break;
default:
break;
}
}
private static final int FLAG = 123;
private ImageView imgShow;
private TextView txtTime;
private Button btnStart;
private Button btnStop;
private AnimationDrawable animation;
private Timer timer;
/**
* 初始化视图相关控件
*/
@Override
protected void initView() {
setContentView(R.layout.activity_main);
imgShow = (ImageView) findViewById(R.id.img_show);
txtTime = (TextView) findViewById(R.id.txt_time);
// imgShow.setImageResource(R.drawable.ic_launcher);
btnStart = (Button) findViewById(R.id.btn_start_anim);
btnStop = (Button) findViewById(R.id.btn_stop_anim);
}
/**
* 初始化数据
*/
@Override
public void initData() {
// xml资源文件的形式实现动画效果
// animation = (AnimationDrawable)
// getResources().getDrawable(R.drawable.anim_frame);
// 代码方式实现帧动画效果
initAnimation();
timer = new Timer();
}
/**
* 设置监听器
*/
@Override
public void setListeners() {
super.setListeners();
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
/**
* 其他操作
*/
@Override
public void setMoreAction() {
super.setMoreAction();
imgShow.setImageDrawable(animation);
}
/**
* 初始化动画,添加每一帧
*/
private void initAnimation() {
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.img0), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img1), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img2), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img3), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img4), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img5), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img6), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img7), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img8), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img9), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img10), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img11), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img12), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img13), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img14), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img15), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img16), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img17), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img18), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img19), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img20), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img21), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img22), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img23), 50);
animation.addFrame(getResources().getDrawable(R.drawable.img24), 50);
// 设置单次播放
animation.setOneShot(true);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case FLAG:
int total = (Integer) msg.obj;
if (total < 0) {
return;
}
txtTime.setText("倒计时:" + total + "秒");
if (total == 0) {
animation.start();
}
break;
default:
break;
}
};
};
TimerTask task = new TimerTask() {
int total = 10;
@Override
public void run() {
total--;
Message msg = Message.obtain();
msg.what = FLAG;
msg.obj = total;
handler.sendMessage(msg);
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start_anim:
timer.schedule(task, 0, 1000);
// animation.start();
break;
case R.id.btn_stop_anim:
if (animation.isRunning()) {
animation.stop();
}
break;
default:
break;
}
}
}
<TextView
android:id="@+id/txt_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="倒计时:10秒"/>
<ImageView
android:id="@+id/img_show"
android:layout_width="100dp"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_start_anim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="开始动画" />
<Button
android:id="@+id/btn_stop_anim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="结束动画" />
阅读全文
0 0
- 动画__帧动画
- 转场动画 帧动画
- 动画 AnimationDrawable 帧动画
- 动画----帧动画
- 安卓补间动画、帧动画
- Android动画-帧动画
- Android动画--帧动画
- egret_gif动画(帧动画)
- android动画 -- 帧动画 补间动画
- 属性动画,补间动画,帧动画
- View动画、帧动画、属性动画
- Android动画-帧动画&补间动画
- Android加载动画 旋转动画 、帧动画
- 帧动画、补间动画、属性动画
- 动画(过渡动画、关键帧动画)
- Android动画-帧动画-属性动画
- 帧动画&补间动画&属性动画
- 安卓 动画帧动画基础动画
- 并查集
- 01背包和完全背包
- leetcode--Happy Number
- 区块链是个什么鬼(1)从脑海中出现的第一印象开始
- leetcode--House Robber
- 帧动画
- java基础之集合总结
- JS常见算法-累加/累积
- leetcode--Reverse Bits
- 基于Django开发的SkyNet博客二——base Template
- C语言中的const和c++中的const的区别
- java代理之静态代理
- 为什么不用 ES6 完全替换 Lodash
- 2017最新App Store 审核指南中文版