您的位置:首页 > 移动开发 > Android开发

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.效果展示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: