Minor changes after reviewing Bitmaps life cycle in PreviewImageFragment
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / ImageViewCustom.java
1 package com.owncloud.android.ui.preview;
2
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;
11
12 import com.owncloud.android.lib.common.utils.Log_OC;
13
14 public class ImageViewCustom extends ImageView {
15
16 private static final String TAG = ImageViewCustom.class.getSimpleName();
17
18 private static final boolean IS_ICS_OR_HIGHER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
19
20 private int mBitmapHeight;
21 private int mBitmapWidth;
22
23
24 public ImageViewCustom(Context context) {
25 super(context);
26 }
27
28 public ImageViewCustom(Context context, AttributeSet attrs) {
29 super(context, attrs);
30 }
31
32 public ImageViewCustom(Context context, AttributeSet attrs, int defStyle) {
33 super(context, attrs, defStyle);
34 }
35
36 @SuppressLint("NewApi")
37 @Override
38 protected void onDraw(Canvas canvas) {
39
40 if(IS_ICS_OR_HIGHER && checkIfMaximumBitmapExceed(canvas)) {
41 // Set layer type to software one for avoiding exceed
42 // and problems in visualization
43 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
44 }
45
46 super.onDraw(canvas);
47 }
48
49 /**
50 * Checks if current bitmaps exceed the maximum OpenGL texture size limit
51 * @param canvas Canvas where the view will be drawn into.
52 * @return boolean True means that the bitmap is too big for the canvas.
53 */
54 @SuppressLint("NewApi")
55 private boolean checkIfMaximumBitmapExceed(Canvas canvas) {
56 Log_OC.v(TAG, "Canvas maximum: " + canvas.getMaximumBitmapWidth() + " - " + canvas.getMaximumBitmapHeight());
57 if (mBitmapWidth > canvas.getMaximumBitmapWidth()
58 || mBitmapHeight > canvas.getMaximumBitmapHeight()) {
59 return true;
60 }
61
62 return false;
63 }
64
65 @Override
66 /**
67 * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
68 * but without keeping another reference to the {@link Bitmap}
69 */
70 public void setImageBitmap (Bitmap bm) {
71 mBitmapWidth = bm.getWidth();
72 mBitmapHeight = bm.getHeight();
73 super.setImageBitmap(bm);
74 }
75
76 }