cmnewfan
cmnewfan

Reputation: 129

Displaying gif in ImageView with Movie

i am meeting a problem while displaying gif in ImageView with Movie.

the code is like 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;
    }
}

}

but it crashed with no exception captured.

android logcat is below:

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)

i am just stuck in this problem and don't know what to do.

Upvotes: 2

Views: 754

Answers (2)

Salauddin Gazi
Salauddin Gazi

Reputation: 1517

you can use the following library to show gif

1.android-gif-drawable

2.GifImageView

3.GifView

Upvotes: 1

Valdio
Valdio

Reputation: 109

Give a try to Glide. https://github.com/bumptech/glide

Here is an example to load Gifs

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

Upvotes: 1

Related Questions