1 package com
.owncloud
.android
.ui
.preview
;
3 import android
.annotation
.SuppressLint
;
4 import android
.content
.Context
;
5 import android
.graphics
.Bitmap
;
6 import android
.graphics
.Canvas
;
7 import android
.graphics
.Movie
;
8 import android
.os
.Build
;
9 import android
.util
.AttributeSet
;
10 import android
.view
.View
;
11 import android
.widget
.ImageView
;
13 import com
.owncloud
.android
.datamodel
.OCFile
;
15 import java
.io
.FileInputStream
;
16 import java
.io
.InputStream
;
18 public class ImageViewCustom
extends ImageView
{
20 private static final String TAG
= ImageViewCustom
.class.getSimpleName();
22 private static final boolean IS_ICS_OR_HIGHER
= Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
;
23 private static final boolean IS_VERSION_BUGGY_ON_RECYCLES
=
24 Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.JELLY_BEAN_MR1
||
25 Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.JELLY_BEAN_MR2
;
27 private int mBitmapHeight
;
28 private int mBitmapWidth
;
30 private Movie mGifMovie
;
31 private int mMovieWidth
, mMovieHeight
;
32 private long mMovieDuration
;
33 private long mMovieRunDuration
;
34 private long mLastTick
;
36 public ImageViewCustom(Context context
) {
40 public ImageViewCustom(Context context
, AttributeSet attrs
) {
41 super(context
, attrs
);
44 public ImageViewCustom(Context context
, AttributeSet attrs
, int defStyle
) {
45 super(context
, attrs
, defStyle
);
48 @SuppressLint("NewApi")
50 protected void onDraw(Canvas canvas
) {
51 if(IS_ICS_OR_HIGHER
&& checkIfMaximumBitmapExceed(canvas
) || IS_VERSION_BUGGY_ON_RECYCLES
) {
52 // Software type is set with two targets:
53 // 1. prevent that bitmaps larger than maximum textures allowed are shown as black views in devices
54 // with LAYER_TYPE_HARDWARE enabled by default;
55 // 2. grant that bitmaps are correctly de-allocated from memory in versions suffering the bug fixed in
56 // https://android.googlesource.com/platform/frameworks/base/+/034de6b1ec561797a2422314e6ef03e3cd3e08e0;
58 setLayerType(View
.LAYER_TYPE_SOFTWARE
, null
);
61 if(mGifMovie
== null
){
64 long nowTick
= android
.os
.SystemClock
.uptimeMillis();
66 mMovieRunDuration
= 0;
68 mMovieRunDuration
+= nowTick
- mLastTick
;
69 if(mMovieRunDuration
> mMovieDuration
){
70 mMovieRunDuration
= 0;
74 mGifMovie
.setTime((int) mMovieRunDuration
);
77 if(mGifMovie
.height() > getHeight() || mGifMovie
.width() > getWidth()) {
78 scale
= (1f
/ Math
.min(canvas
.getHeight() / mGifMovie
.height(),
79 canvas
.getWidth() / mGifMovie
.width())) + 0.25f
;
81 scale
= Math
.min(canvas
.getHeight() / mGifMovie
.height(),
82 canvas
.getWidth() / mGifMovie
.width());
85 canvas
.scale(scale
, scale
);
86 canvas
.translate(((float) getWidth() / scale
- (float) mGifMovie
.width()) / 2f
,
87 ((float) getHeight() / scale
- (float) mGifMovie
.height()) /2f
);
89 mGifMovie
.draw(canvas
, 0, 0);
97 protected void onMeasure(int widthMeasureSpec
, int heightMeasureSpec
) {
98 if (mGifMovie
== null
){
99 setMeasuredDimension(widthMeasureSpec
, heightMeasureSpec
);
101 setMeasuredDimension(mMovieWidth
, mMovieHeight
);
106 * Checks if current bitmaps exceed the maximum OpenGL texture size limit
107 * @param canvas Canvas where the view will be drawn into.
108 * @return boolean True means that the bitmap is too big for the canvas.
110 @SuppressLint("NewApi")
111 private boolean checkIfMaximumBitmapExceed(Canvas canvas
) {
112 return mBitmapWidth
> canvas
.getMaximumBitmapWidth()
113 || mBitmapHeight
> canvas
.getMaximumBitmapHeight();
119 * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
120 * but without keeping another reference to the {@link Bitmap}
122 public void setImageBitmap(Bitmap bm
) {
123 mBitmapWidth
= bm
.getWidth();
124 mBitmapHeight
= bm
.getHeight();
125 super.setImageBitmap(bm
);
128 public void setGifImage(OCFile file
){
130 InputStream gifInputStream
= new FileInputStream(file
.getStoragePath());
131 setLayerType(View
.LAYER_TYPE_SOFTWARE
, null
);
134 mGifMovie
= Movie
.decodeStream(gifInputStream
);
135 mMovieWidth
= mGifMovie
.width();
136 mMovieHeight
= mGifMovie
.height();
137 mMovieDuration
= mGifMovie
.duration();
138 } catch (Exception e
) {