【发布时间】:2014-05-23 15:28:19
【问题描述】:
我想播放一个 gif 文件。我可以使用电影和画布来显示 gif(代码如下)。但通常 gifs 文件带有一个属性,告诉你需要循环的数量。如何提取该值并显示电影的最后一帧?下面是我用来播放 gif 的代码:
public TutorialGifView(Context context , InputStream is , int width , int height ) {
super(context);
mContext = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
ScreenWidth = width;
ScreenHeight = height ;
mMovie = Movie.decodeStream(is);
if(ScreenWidth > ScreenHeight)
{
scaleh = (float)height/(float)(mMovie.height()+1);
scalew = ((float)(4*height))/((float)(mMovie.width()*3));
}
else
{
scalew = ((float)width/((float)mMovie.width()+1));
scaleh = ((float)(4*mMovie.width())/(float)(3*mMovie.width()));
}
}
protected void onDraw(Canvas canvas)
{
if (mMovie == null) {
return;
}
canvas.scale(scalew, scaleh);
canvas.drawColor(Color.TRANSPARENT);
super.onDraw(canvas);
final long now = SystemClock.uptimeMillis();
if (mMoviestart == 0) {
mMoviestart = now;
}
final int relTime = (int)((now - mMoviestart) % mMovie.duration());
mMovie.setTime(relTime);
mMovie.draw(canvas, padLeft/scalew , padTop/scaleh);//padLeft/scaleh);
// here i shoud get the loop count
if(LoopCount > 0 )
{
if((now- mMoviestart) > mMovie.duration()*(LoopCount) )
{
setWillNotDraw(true);
return ;
}
}
this.invalidate();
}
【问题讨论】: