ImageViewCustom.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.owncloud.android.ui.preview;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Canvas;
  6. import android.graphics.Movie;
  7. import android.os.Build;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. import android.widget.ImageView;
  11. import com.owncloud.android.datamodel.OCFile;
  12. import com.owncloud.android.lib.common.utils.Log_OC;
  13. import java.io.FileInputStream;
  14. import java.io.InputStream;
  15. public class ImageViewCustom extends ImageView {
  16. private static final String TAG = ImageViewCustom.class.getSimpleName();
  17. private static final boolean IS_ICS_OR_HIGHER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
  18. private static final boolean IS_VERSION_BUGGY_ON_RECYCLES =
  19. Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1 ||
  20. Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2;
  21. private int mBitmapHeight;
  22. private int mBitmapWidth;
  23. private Movie mGifMovie;
  24. private int mMovieWidth, mMovieHeight;
  25. private long mMovieDuration;
  26. private long mMovieRunDuration;
  27. private long mLastTick;
  28. public ImageViewCustom(Context context) {
  29. super(context);
  30. }
  31. public ImageViewCustom(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. }
  34. public ImageViewCustom(Context context, AttributeSet attrs, int defStyle) {
  35. super(context, attrs, defStyle);
  36. }
  37. @SuppressLint("NewApi")
  38. @Override
  39. protected void onDraw(Canvas canvas) {
  40. if(IS_ICS_OR_HIGHER && checkIfMaximumBitmapExceed(canvas) || IS_VERSION_BUGGY_ON_RECYCLES ) {
  41. // Software type is set with two targets:
  42. // 1. prevent that bitmaps larger than maximum textures allowed are shown as black views in devices
  43. // with LAYER_TYPE_HARDWARE enabled by default;
  44. // 2. grant that bitmaps are correctly de-allocated from memory in versions suffering the bug fixed in
  45. // https://android.googlesource.com/platform/frameworks/base/+/034de6b1ec561797a2422314e6ef03e3cd3e08e0;
  46. //
  47. setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  48. }
  49. super.onDraw(canvas);
  50. if(mGifMovie != null) {
  51. long nowTick = android.os.SystemClock.uptimeMillis();
  52. if (mLastTick == 0) {
  53. mMovieRunDuration = 0;
  54. } else {
  55. mMovieRunDuration += nowTick - mLastTick;
  56. if(mMovieRunDuration > mMovieDuration) {
  57. mMovieRunDuration = 0;
  58. }
  59. }
  60. mGifMovie.setTime((int) mMovieRunDuration);
  61. float scale = getScaleToViewFactor(mGifMovie, canvas);
  62. canvas.scale(scale, scale);
  63. canvas.translate(((float) getWidth() / scale - (float) mGifMovie.width()) / 2f,
  64. ((float) getHeight() / scale - (float) mGifMovie.height()) /2f);
  65. mGifMovie.draw(canvas, 0, 0);
  66. mLastTick = nowTick;
  67. invalidate();
  68. }
  69. }
  70. private float getScaleToViewFactor(Movie movie, Canvas canvas) {
  71. if (movie.height() > getHeight() || movie.width() > getWidth()) {
  72. float offset = 0.25f;
  73. return (1f / Math.min(canvas.getHeight() / movie.height(), canvas.getWidth() / movie.width())) + offset;
  74. }
  75. return Math.min(canvas.getHeight() / movie.height(), canvas.getWidth() / movie.width());
  76. }
  77. @Override
  78. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  79. if (mGifMovie == null) {
  80. setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
  81. } else {
  82. setMeasuredDimension(mMovieWidth, mMovieHeight);
  83. }
  84. }
  85. /**
  86. * Checks if current bitmaps exceed the maximum OpenGL texture size limit
  87. * @param canvas Canvas where the view will be drawn into.
  88. * @return boolean True means that the bitmap is too big for the canvas.
  89. */
  90. @SuppressLint("NewApi")
  91. private boolean checkIfMaximumBitmapExceed(Canvas canvas) {
  92. return mBitmapWidth > canvas.getMaximumBitmapWidth()
  93. || mBitmapHeight > canvas.getMaximumBitmapHeight();
  94. }
  95. @Override
  96. /**
  97. * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
  98. * but without keeping another reference to the {@link Bitmap}
  99. */
  100. public void setImageBitmap(Bitmap bm) {
  101. mBitmapWidth = bm.getWidth();
  102. mBitmapHeight = bm.getHeight();
  103. super.setImageBitmap(bm);
  104. }
  105. public void setGIFImage(OCFile file) {
  106. try {
  107. InputStream gifInputStream = new FileInputStream(file.getStoragePath());
  108. setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  109. setFocusable(true);
  110. mGifMovie = Movie.decodeStream(gifInputStream);
  111. mMovieWidth = mGifMovie.width();
  112. mMovieHeight = mGifMovie.height();
  113. mMovieDuration = mGifMovie.duration();
  114. } catch (Exception e) {
  115. Log_OC.e(TAG, "Failed to set GIF image");
  116. }
  117. }
  118. }