显示gif动画(帧动画的播放)
2017-04-01 13:54
357 查看
在android上显示gif不太方便,虽然有控件可以实现,但是效果不是很好,保险点儿的作法还是使用帧动画来处理。
①在XML中定义animation-list:
<?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/f_1" android:duration="30"/>
<item android:drawable="@drawable/f_2" android:duration="30"/>
<item android:drawable="@drawable/f_3" android:duration="30"/>
<item android:drawable="@drawable/f_4" android:duration="30"/>
</animation-list>
②在XML布局中定义ImageView,指定其src属性或background属性为"@anim/loading"
③在java代码中:
ImageView imageView = (ImageView) findViewById(R.id.frame);
AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
// 使用background属性时
// AnimationDrawable drawable = (AnimationDrawable) imageView.getBackground();
drawable.setOneShot(false); // 重复播放
④在java代码中调用:
drawable.start()或drawable.stop()实现动画的播放和停止。
值得注意的是上面的代码在android4.0系统没问题,但是在2.3系统上动画无效,所以我们需要使用另一种方式。
// 兼容android 4.0 以下系统
imageView.post(new Runnable() {
@Override
public void run() {
draw.start();
}
});
①在XML中定义animation-list:
<?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/f_1" android:duration="30"/>
<item android:drawable="@drawable/f_2" android:duration="30"/>
<item android:drawable="@drawable/f_3" android:duration="30"/>
<item android:drawable="@drawable/f_4" android:duration="30"/>
</animation-list>
②在XML布局中定义ImageView,指定其src属性或background属性为"@anim/loading"
③在java代码中:
ImageView imageView = (ImageView) findViewById(R.id.frame);
AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
// 使用background属性时
// AnimationDrawable drawable = (AnimationDrawable) imageView.getBackground();
drawable.setOneShot(false); // 重复播放
④在java代码中调用:
drawable.start()或drawable.stop()实现动画的播放和停止。
值得注意的是上面的代码在android4.0系统没问题,但是在2.3系统上动画无效,所以我们需要使用另一种方式。
// 兼容android 4.0 以下系统
imageView.post(new Runnable() {
@Override
public void run() {
draw.start();
}
});
相关文章推荐
- .net学习:显示/播放Gif动画
- GTK显示GIF、GTK动画(图片连续播放)
- .net 显示/播放 Gif动画
- wxwidgets中分解GIF动画图片并显示播放
- 显示gif动画(帧动画的播放)
- 如何利用 WebBrowser 控件,显示 .GIF 动画?
- 显示GIF动画
- PictureBox控件中显示GIF动画(图片)
- 如何利用 WebBrowser 控件,显示 .GIF 动画?
- 网页中的gif动画无法正常播放
- Delphi2007 显示GIF 动画
- SWT图片显示,支持JPEG,GIF(动画),BMP,ICO,PNG和TIFF
- EVC编程点滴-GIF动画显示类
- 如何利用 WebBrowser 控件,显示 .GIF 动画?
- WPF:暂时解决播放gif动画图片问题
- GDI+显示GIF动画
- 利用GDI+显示GIF动画
- 用GDI+显示GIF动画的一个类
- GDI+显示GIF动画
- GDI+显示GIF动画