Android播放gif动画
2010-10-29 15:10
501 查看
Android不支持直接播放gif动能,如果在程序中将gif分解,然后播放各个图片这个会很消耗内存,因此我们要提前将动画分解,就其放入资源文件中。
1.在drawable新建xml文件,battery_anim.xml
内容:
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="150" android:drawable="@drawable/battery_anim0" />
<item android:duration="150" android:drawable="@drawable/battery_anim1" />
<item android:duration="150" android:drawable="@drawable/battery_anim2" />
<item android:duration="150" android:drawable="@drawable/battery_anim3" />
<item android:duration="150" android:drawable="@drawable/battery_anim4" />
<item android:duration="150" android:drawable="@drawable/battery_anim5" />
</animation-list>
对应的item 为顺序的图片从开始到结束,duration为每张逐帧播放间隔,oneshot 为false 代表循环播放,设置为true 即播放一次即停止。
2.在main.xml文件中插入ImageView控件代码如下:
<ImageView android:visibility="visible"
android:layout_width="wrap_content"
android:layout_marginTop="100dip" android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:background="@drawable/battery_anim"
android:id="@+id/batteryStatus"></ImageView>
3.java代码如下:
ImageView imageView = null;
AnimationDrawable anim = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ImageView01);
Button mybtn = (Button) findViewById(R.id.Button01);
mybtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
AnimationDrawable anim = null;
Object ob = imageView.getBackground();
anim = (AnimationDrawable) ob;
anim.stop();
anim.start();
}
});
}
4.效果展示
1.在drawable新建xml文件,battery_anim.xml
内容:
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="150" android:drawable="@drawable/battery_anim0" />
<item android:duration="150" android:drawable="@drawable/battery_anim1" />
<item android:duration="150" android:drawable="@drawable/battery_anim2" />
<item android:duration="150" android:drawable="@drawable/battery_anim3" />
<item android:duration="150" android:drawable="@drawable/battery_anim4" />
<item android:duration="150" android:drawable="@drawable/battery_anim5" />
</animation-list>
对应的item 为顺序的图片从开始到结束,duration为每张逐帧播放间隔,oneshot 为false 代表循环播放,设置为true 即播放一次即停止。
2.在main.xml文件中插入ImageView控件代码如下:
<ImageView android:visibility="visible"
android:layout_width="wrap_content"
android:layout_marginTop="100dip" android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:background="@drawable/battery_anim"
android:id="@+id/batteryStatus"></ImageView>
3.java代码如下:
ImageView imageView = null;
AnimationDrawable anim = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ImageView01);
Button mybtn = (Button) findViewById(R.id.Button01);
mybtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
AnimationDrawable anim = null;
Object ob = imageView.getBackground();
anim = (AnimationDrawable) ob;
anim.stop();
anim.start();
}
});
}
4.效果展示
相关文章推荐
- Android扩展ImageView播放gif动画
- Android API Demo系列之---播放Gif动画的类Movie
- android 通过帧动画方式播放Gif动画
- Android 播放Gif 动画
- Android播放GIF动画
- Android 播放Gif 动画
- Android播放GIF动画
- Android中播放GIF图片动画
- Android 播放Gif 动画
- Android播放GIF动画
- Android中播放GIF动画
- android 拓展ImageView播放GIF动画
- Android 播放Gif 动画
- Android实现可播放GIF动画的ImageView
- Android 播放Gif 动画
- Android只播放gif动画
- Android播放gif动画,增加屏幕掉金币效果
- Android 播放Gif 动画
- Android中播放GIF图片动画
- Android高级控件(二)——SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现