如何在Android中停止gif动画?

时间:2015-12-11 10:46:48

标签: android gif animated-gif

我使用以下课程。如何根据点击事件停止并开始循环?我尝试删除invalidate(),但Gif根本没有开始加载。

GifView类

import java.io.InputStream;

import com.example.merchant.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View{

private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long movieStart;

public GifView(Context context) {
    super(context);
    init(context);
}


public GifView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

private void init(Context context) {
    setFocusable(true);
    gifInputStream = context.getResources().openRawResource(R.drawable.ani_bart);

    gifMovie = Movie.decodeStream(gifInputStream);  
    movieWidth = gifMovie.width();
    movieHeight = gifMovie.height();
    movieDuration = gifMovie.duration();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(movieWidth, movieHeight);
}

public int getMovieWidth() {
    return movieWidth;
}

public int getMovieHeight() {
    return movieHeight;
}

public long getMovieDuration() {
    return movieDuration;
}

@Override
protected void onDraw(Canvas canvas) {

    long now = SystemClock.uptimeMillis();

    if(movieStart == 0) {
        movieStart = now;
    }

    if(gifMovie != null) {

        int dur = gifMovie.duration();
        if(dur == 0) {
            dur = 1000;
        }

        int relTime = (int)((now - movieStart) % dur);

        gifMovie.setTime(relTime);

        gifMovie.draw(canvas, 0, 0);
        invalidate();
    }
}

}

2 个答案:

答案 0 :(得分:1)

这是我用来克服这个问题的。但是不确定这是否是一个有效的解决方案。虽然为我工作。

{{1}}

答案 1 :(得分:0)

猜猜它是不可能的但是如果你需要控制合作,你可以通过animation-list

在android中创建frme aniation