Created
February 13, 2019 06:36
-
-
Save samigehi/608d8429676ce7bc33d53f9e3c74c21c to your computer and use it in GitHub Desktop.
Gif View using Movie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.samigehi.widget; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Movie; | |
import android.view.View; | |
import java.io.InputStream; | |
public class GifView extends View { | |
public Movie mMovie; | |
public long movieStart; | |
public GifView(Context context, int resourceId) { | |
super(context); | |
init(resourceId, context); | |
} | |
private void init(final int id, Context context) { | |
InputStream is = context.getResources().openRawResource(id); | |
mMovie = Movie.decodeStream(is); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
canvas.drawColor(Color.TRANSPARENT); | |
super.onDraw(canvas); | |
long now = android.os.SystemClock.uptimeMillis(); | |
if (movieStart == 0) { | |
movieStart = now; | |
} | |
if (mMovie != null) { | |
int relTime = (int) ((now - movieStart) % mMovie.duration()); | |
mMovie.setTime(relTime); | |
mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2, | |
(getHeight() / 2) - mMovie.height() / 2); | |
this.invalidate(); | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int desiredWidth = 200; | |
int desiredHeight = 200; | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
int width; | |
int height; | |
//Measure Width | |
if (widthMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
width = widthSize; | |
} else if (widthMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
width = Math.min(desiredWidth, widthSize); | |
} else { | |
//Be whatever you want | |
width = desiredWidth; | |
} | |
//Measure Height | |
if (heightMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
height = heightSize; | |
} else if (heightMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
height = Math.min(desiredHeight, heightSize); | |
} else { | |
//Be whatever you want | |
height = desiredHeight; | |
} | |
//MUST CALL THIS | |
setMeasuredDimension(width, height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment