ImageViewCustom.java 2.3 KB

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