方法一、直接播放Gif图片,但是在魅族手机上无法执行
1 自定义View
public class SongEffectView extends View {
private long movieStart;
private Movie movie;
// 此处必须重写该构造方法
public SongEffectView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
// 以文件流(InputStream)读取进gif图片资源
movie = Movie.decodeStream(getResources().openRawResource(
R.drawable.a11232));
}
@Override
protected void onDraw(Canvas canvas) {
long curTime = android.os.SystemClock.uptimeMillis();
// 第一次播放
if (movieStart == 0) {
movieStart = curTime;
}
if (movie != null) {
int duraction = movie.duration();
int relTime = (int) ((curTime - movieStart) % duraction);
movie.setTime(relTime);
movie.draw(canvas, 0, 0);
// 强制重绘
invalidate();
}
super.onDraw(canvas);
}
}
2 布局文件中引用
<com.rednovo.weibo.widget.main.page.SongEffectView
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_gravity="center"
android:visibility="visible"
/>
方法二、播放打散的Gif图片
1 打散gif图片,成为一帧一帧的部分
2 在anim文件下新建文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/song01"
android:duration="150"/>
<item
android:drawable="@drawable/song02"
android:duration="150"/>
</animation-list>
3 布局文件中添加
<ImageView
android:id="@+id/id_img_song_effect"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_gravity="bottom"
android:background="@anim/song_effect"/>
4 代码控制动起来
ImageView songEffect = (ImageView) findViewById(R.id.id_img_song_effect);
AnimationDrawable anim = null;
Object ob = songEffect.getBackground();
anim = (AnimationDrawable) ob;
anim.stop();
anim.start();
8/10/2015 3:16:12 PM