android play gif,Play.gif image in android without using webview

本文讲述了如何在Android中使用自定义View播放GIF动画失败的问题,推荐使用WebView以外的方法,如GifSplitter工具将GIF拆分为帧并配合AnimationDrawable。作者分享了详细的步骤和相关链接,指出Android原生API无法直接播放完整的GIF。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

I tried like this, which i found on the net to play gif images:

private class MYGIFView extends View {

Movie movie;

InputStream is = null;

long moviestart;

public MYGIFView(Context context) {

super(context);

// Provide your own gif animation file

is = context.getResources().openRawResource(R.drawable.mygif);

movie = Movie.decodeStream(is);

}

@Override

protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);

super.onDraw(canvas);

long now = android.os.SystemClock.uptimeMillis();

System.out.println("now=" + now);

if (moviestart == 0) { // first time

moviestart = now;

}

System.out.println("\tmoviestart=" + moviestart);

int relTime = (int) ((now - moviestart) % movie.duration());

System.out.println("time=" + relTime + "\treltime="

+ movie.duration());

movie.setTime(relTime);

movie.draw(canvas, this.getWidth() / 2 - 20,

this.getHeight() / 2 - 40);

this.invalidate();

}

Though many have said that they got their desired result.

I have put the gif image in drawable.

I am getting movie.duration() null.

Please suggest where am i wrong or if is there any way.

回答1:

Android cannot play GIF files without WebView. You must break it apart into frames and animate it yourself.

I found on another StackOverflow post this bit of software from XoyoSoft, called GifSplitter, that can split a GIF into frames. You would then want to use AnimationDrawable to combine those.

It would look something like this:

// Your files:

res\drawable-xxxx\frame1.jpg

res\drawable-xxxx\frame2.jpg

res\drawable-xxxx\frame3.jpg

// ...

res\drawable-xxxx\frame99.jpg

Then, there is an example in the AnimationDrawable documentation above that will show how to display those images.

Lastly, you must load and play the animation; that, too, is detailed in the AnimationDrawable documentation.

来源:https://stackoverflow.com/questions/11806320/play-gif-image-in-android-without-using-webview

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值