Advertisement
haleks

GIFView.java

Jan 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package com.media_app.media_app;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Movie;
  6. import android.util.AttributeSet;
  7. import android.view.View;
  8. import java.io.InputStream;
  9.  
  10. public class GIFView extends View {
  11.  
  12. private InputStream gifInputStream;
  13. private Movie gifMovie;
  14. private int movieWidth, movieHeight;
  15. private long movieDuration;
  16. private long mMovieStart;
  17.  
  18. public GIFView(Context context) {
  19. super(context);
  20. init(context);
  21. }
  22.  
  23. public GIFView(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. init(context);
  26.  
  27. }
  28.  
  29. public GIFView(Context context, AttributeSet attrs,
  30.  
  31. int defStyleAttr) {
  32. super(context, attrs, defStyleAttr);
  33. init(context);
  34. }
  35.  
  36. private void init(Context context) {
  37.  
  38. setFocusable(true);
  39. gifInputStream = context.getResources().openRawResource(R.drawable.flower);
  40. gifMovie = Movie.decodeStream(gifInputStream);
  41. movieWidth = gifMovie.width();
  42. movieHeight = gifMovie.height();
  43. movieDuration = gifMovie.duration();
  44.  
  45. }
  46.  
  47. @Override
  48. protected void onMeasure(int widthMeasureSpec,
  49.  
  50. int heightMeasureSpec) {
  51. setMeasuredDimension(movieWidth, movieHeight);
  52. }
  53.  
  54. public int getMovieWidth() {
  55. return movieWidth;
  56. }
  57.  
  58. public int getMovieHeight() {
  59. return movieHeight;
  60. }
  61.  
  62. public long getMovieDuration() {
  63. return movieDuration;
  64. }
  65.  
  66. @Override
  67. protected void onDraw(Canvas canvas) {
  68.  
  69. long now = android.os.SystemClock.uptimeMillis();
  70.  
  71. if (mMovieStart == 0) { // first time
  72. mMovieStart = now;
  73. }
  74.  
  75. if (gifMovie != null) {
  76.  
  77. int dur = gifMovie.duration();
  78. if (dur == 0) {
  79. dur = 1000;
  80. }
  81.  
  82. int relTime = (int) ((now - mMovieStart) % dur);
  83. gifMovie.setTime(relTime);
  84. canvas.scale((float)this.getWidth() / (float)gifMovie.width(),(float)this.getHeight() / (float)gifMovie.height());
  85. //canvas.scale(1.9f, 3.21f);
  86. gifMovie.draw(canvas,0, 0);
  87. invalidate();
  88.  
  89. }
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement