使用Movie在ImageView中显示gif

时间:2017-02-06 08:37:23

标签: android android-imageview gif

我在使用电影在ImageView中显示gif时遇到问题。

代码就像belw:

public class CustomImageView extends ImageView {
private Context mContext;
private int movieWidth, movieHeight;
private long movieDuration;
private long movieStart;
private Movie movie;
private boolean isGif;
private File gifFile;

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

@Override
protected void onDraw(Canvas canvas) {
    if(isGif){
        long now = android.os.SystemClock.uptimeMillis();
        if (movieStart == 0) {   // first time
            movieStart = now;
        }
        if (movie != null) {
            int dur = movie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int)((now - movieStart) % dur);
            movie.setTime(relTime);
            movie.draw(canvas, 0, 0);
            invalidate();
        }
    }
    //super.onDraw(canvas);
}

public CustomImageView(Context context) {
    super(context);
    mContext = context;
}

public CustomImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
}

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
}

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    mContext = context;
}

public void setIsGif(boolean gif, File file){
    if(gif){
        isGif = true;
        gifFile = file;
        BufferedInputStream uriInputStream = null;
        try {
            uriInputStream = new BufferedInputStream(mContext.getContentResolver().openInputStream(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.gif")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        movie = Movie.decodeStream(uriInputStream);
        movieWidth = movie.width();
        movieHeight = movie.height();
        movieDuration = movie.duration();
    }else{
        isGif = false;
    }
}

}

但它没有捕获任何异常而崩溃。

android logcat如下:

02-06 16:06:52.056 23795-23795/com.weiwa.ljl.testimageview E/System: stat file error, path is /data/app/com.weiwa.ljl.testimageview-2/lib/arm64, exception is android.system.ErrnoException: stat failed: ENOENT (No such file or directory)
02-06 16:06:52.564 23795-23833/com.weiwa.ljl.testimageview E/GED: Failed to get GED Log Buf, err(0)
02-06 16:06:52.605 23795-23795/com.weiwa.ljl.testimageview A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 23795 (l.testimageview)

我只是陷入了这个问题,不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

尝试滑行。 https://github.com/bumptech/glide

以下是加载Gif的示例

  Glide.with(this)
                .load(gifUrl)
                .asGif()
                .centerCrop()
                .into(imageView);

答案 1 :(得分:1)

您可以使用以下库来显示gif

1。android-gif-drawable

2。GifImageView

3。GifView