android播放gif格式图片示例

时间:2021-05-20

复制代码 代码如下:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.nmbs.R;

public class GifView extends View {
private long movieStart;
private Movie movie;

public GifView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
movie = Movie.decodeStream(getResources().openRawResource(
R.drawable.ic_showseat));
}

public GifView(Context context) {
super(context);
movie = Movie.decodeStream(getResources().openRawResource(
R.drawable.ic_showseat));
}

@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);
}

@Override
public void setLayoutParams(LayoutParams params) {
super.setLayoutParams(params);
}
}

复制代码 代码如下:
GifView gifView = new GifView(this);

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章