Created
June 7, 2017 09:26
-
-
Save hector6872/3dcd3635c8471af5fb6b4ec9bd4871bf to your computer and use it in GitHub Desktop.
GifImageView
This file contains hidden or 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
public class GifImageView extends ImageView { | |
private static final long DEFAULT_MOVIE_VIEW_DURATION = TimeUnit.SECONDS.toMillis(1); | |
private Movie movie; | |
private long movieStart; | |
private int movieCurrentAnimationTime; | |
private boolean running = false; | |
private boolean visible = true; | |
public GifImageView(Context context) { | |
this(context, null); | |
} | |
public GifImageView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public GifImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
private void init() { | |
setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
} | |
@Override protected void onDraw(Canvas canvas) { | |
if (movie != null) { | |
if (running) { | |
updateAnimationTime(); | |
drawMovieFrame(canvas); | |
invalidateView(); | |
} else { | |
drawMovieFrame(canvas); | |
} | |
} | |
} | |
private void invalidateView() { | |
if (visible) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
postInvalidateOnAnimation(); | |
} else { | |
invalidate(); | |
} | |
} | |
} | |
private void updateAnimationTime() { | |
long now = android.os.SystemClock.uptimeMillis(); | |
if (movieStart == 0) { | |
movieStart = now; | |
} | |
int duration = movie.duration(); | |
if (duration == 0) { | |
duration = (int) DEFAULT_MOVIE_VIEW_DURATION; | |
} | |
movieCurrentAnimationTime = (int) ((now - movieStart) % duration); | |
} | |
private void drawMovieFrame(@NonNull Canvas canvas) { | |
float scale = (float) this.getHeight() / (float) movie.height(); | |
movie.setTime(movieCurrentAnimationTime); | |
canvas.save(Canvas.MATRIX_SAVE_FLAG); | |
canvas.scale(scale, scale); | |
movie.draw(canvas, ((canvas.getWidth() / scale) - movie.width()) / 2, 0); | |
canvas.restore(); | |
} | |
@SuppressLint("NewApi") @Override public void onScreenStateChanged(int screenState) { | |
super.onScreenStateChanged(screenState); | |
visible = screenState == SCREEN_STATE_ON; | |
invalidateView(); | |
} | |
@SuppressLint("NewApi") @Override protected void onVisibilityChanged(@NonNull View changedView, int visibility) { | |
super.onVisibilityChanged(changedView, visibility); | |
visible = visibility == View.VISIBLE; | |
invalidateView(); | |
} | |
@Override protected void onWindowVisibilityChanged(int visibility) { | |
super.onWindowVisibilityChanged(visibility); | |
visible = visibility == View.VISIBLE; | |
invalidateView(); | |
} | |
public void play() { | |
if (movie != null) { | |
running = true; | |
movieStart = android.os.SystemClock.uptimeMillis() - movieCurrentAnimationTime; | |
invalidate(); | |
} | |
} | |
public void setBanner(@NonNull Movie movie) { | |
this.movie = movie; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://github.com/Cutta/GifView <3