Problems releasing GIFs for Android

In my activity_main.xml file, I have the following block of code because I want to use it to animate my GIF file.

<matthewallenlinsoftware.keepy_uppy.PlayGifView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewGif"
    android:layout_gravity="center"
    android:layout_weight="0.04"
    android:src="@drawable/bouncing_ball" />

GIF animation works great, but it causes rendering problems in Android Studio. Is there a fix for this?

The code for PlayGifView is below:

public class PlayGifView extends View {

private static final int DEFAULT_MOVIEW_DURATION = 1000;

private int mMovieResourceId;
private Movie mMovie;

private long mMovieStart = 0;
private int mCurrentAnimationTime = 0;

@SuppressLint("NewApi")
public PlayGifView(Context context, AttributeSet attrs) {
    super(context, attrs);

    /**
     * Starting from HONEYCOMB have to turn off HardWare acceleration to draw
     * Movie on Canvas.
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
}

public void setImageResource(int mvId){
    this.mMovieResourceId = mvId;
    mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mMovie != null){
        setMeasuredDimension(mMovie.width(), mMovie.height());
    }else{
        setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
    }
}

@Override
protected void onDraw(Canvas canvas) {
    if (mMovie != null){
        updateAnimtionTime();
        drawGif(canvas);
        invalidate();
    }else{
        drawGif(canvas);    //NULLPTR EXCEPTION
    }
}

private void updateAnimtionTime() {
    long now = android.os.SystemClock.uptimeMillis();

    if (mMovieStart == 0) {
        mMovieStart = now;
    }
    int dur = mMovie.duration();
    if (dur == 0) {
        dur = DEFAULT_MOVIEW_DURATION;
    }
    mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}

private void drawGif(Canvas canvas) {
    mMovie.setTime(mCurrentAnimationTime);  //NULLPTR EXCEPTION
    mMovie.draw(canvas, 0, 0);
    canvas.restore();
}

}
+4
source share
1 answer

If you saved GIF in folders such as hdpi, xhdpi, etc. Copy the GIF directly to "drawable" and try running android studio as an administrator ...

0
source

Source: https://habr.com/ru/post/1621740/


All Articles