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
.os
.Build
;
8 import android
.util
.AttributeSet
;
9 import android
.view
.View
;
10 import android
.widget
.ImageView
;
12 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
14 public class ImageViewCustom
extends ImageView
{
16 private static final String TAG
= ImageViewCustom
.class.getSimpleName();
18 private static final boolean IS_ICS_OR_HIGHER
= Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
;
19 private static final boolean IS_VERSION_BUGGY_ON_RECYCLES
=
20 Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.JELLY_BEAN_MR1
||
21 Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.JELLY_BEAN_MR2
;
23 private int mBitmapHeight
;
24 private int mBitmapWidth
;
27 public ImageViewCustom(Context context
) {
31 public ImageViewCustom(Context context
, AttributeSet attrs
) {
32 super(context
, attrs
);
35 public ImageViewCustom(Context context
, AttributeSet attrs
, int defStyle
) {
36 super(context
, attrs
, defStyle
);
39 @SuppressLint("NewApi")
41 protected void onDraw(Canvas canvas
) {
43 if(IS_ICS_OR_HIGHER
&& checkIfMaximumBitmapExceed(canvas
) || IS_VERSION_BUGGY_ON_RECYCLES
) {
44 // Software type is set with two targets:
45 // 1. prevent that bitmaps larger than maximum textures allowed are shown as black views in devices
46 // with LAYER_TYPE_HARDWARE enabled by default;
47 // 2. grant that bitmaps are correctly dellocated from memory in versions suffering the bug fixed in
48 // https://android.googlesource.com/platform/frameworks/base/+/034de6b1ec561797a2422314e6ef03e3cd3e08e0;
50 setLayerType(View
.LAYER_TYPE_SOFTWARE
, null
);
57 * Checks if current bitmaps exceed the maximum OpenGL texture size limit
58 * @param canvas Canvas where the view will be drawn into.
59 * @return boolean True means that the bitmap is too big for the canvas.
61 @SuppressLint("NewApi")
62 private boolean checkIfMaximumBitmapExceed(Canvas canvas
) {
63 Log_OC
.v(TAG
, "Canvas maximum: " + canvas
.getMaximumBitmapWidth() + " - " + canvas
.getMaximumBitmapHeight());
64 if (mBitmapWidth
> canvas
.getMaximumBitmapWidth()
65 || mBitmapHeight
> canvas
.getMaximumBitmapHeight()) {
74 * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
75 * but without keeping another reference to the {@link Bitmap}
77 public void setImageBitmap (Bitmap bm
) {
78 mBitmapWidth
= bm
.getWidth();
79 mBitmapHeight
= bm
.getHeight();
80 super.setImageBitmap(bm
);