1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.widget; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Canvas; 22 import android.graphics.RectF; 23 import android.graphics.drawable.Drawable; 24 import android.graphics.drawable.LayerDrawable; 25 import android.util.AttributeSet; 26 import android.widget.ImageView; 27 28 import com.android.tv.settings.R; 29 30 public class RefcountImageView extends ImageView { 31 32 private boolean mAutoUnrefOnDetach; 33 private boolean mHasClipRect; 34 private final RectF mClipRect = new RectF(); 35 RefcountImageView(Context context)36 public RefcountImageView(Context context) { 37 this(context, null); 38 } 39 RefcountImageView(Context context, AttributeSet attrs)40 public RefcountImageView(Context context, AttributeSet attrs) { 41 this(context, attrs, 0); 42 } 43 RefcountImageView(Context context, AttributeSet attrs, int defStyle)44 public RefcountImageView(Context context, AttributeSet attrs, int defStyle) { 45 super(context, attrs, defStyle); 46 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RefcountImageView); 47 mAutoUnrefOnDetach = a.getBoolean(R.styleable.RefcountImageView_autoUnrefOnDetach, true); 48 } 49 setAutoUnrefOnDetach(boolean autoUnref)50 public void setAutoUnrefOnDetach(boolean autoUnref) { 51 mAutoUnrefOnDetach = autoUnref; 52 } 53 getAutoUnrefOnDetach()54 public boolean getAutoUnrefOnDetach() { 55 return mAutoUnrefOnDetach; 56 } 57 58 @Override onDetachedFromWindow()59 protected void onDetachedFromWindow() { 60 if (mAutoUnrefOnDetach) { 61 setImageDrawable(null); 62 } 63 super.onDetachedFromWindow(); 64 } 65 66 @Override setImageDrawable(Drawable drawable)67 public void setImageDrawable(Drawable drawable) { 68 Drawable previousDrawable = getDrawable(); 69 super.setImageDrawable(drawable); 70 releaseRef(previousDrawable); 71 } 72 releaseRef(Drawable drawable)73 private static void releaseRef(Drawable drawable) { 74 if (drawable instanceof RefcountBitmapDrawable) { 75 ((RefcountBitmapDrawable) drawable).getRefcountObject().releaseRef(); 76 } else if (drawable instanceof LayerDrawable) { 77 LayerDrawable layerDrawable = (LayerDrawable) drawable; 78 for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) { 79 releaseRef(layerDrawable.getDrawable(i)); 80 } 81 } 82 } 83 setClipRect(float l, float t, float r, float b)84 public void setClipRect(float l, float t, float r, float b) { 85 mClipRect.set(l, t, r, b); 86 mHasClipRect = true; 87 } 88 clearClipRect()89 public void clearClipRect() { 90 mHasClipRect = false; 91 } 92 93 @Override onDraw(Canvas canvas)94 protected void onDraw(Canvas canvas) { 95 if (mHasClipRect) { 96 int saveCount = canvas.save(); 97 canvas.clipRect(mClipRect); 98 super.onDraw(canvas); 99 canvas.restoreToCount(saveCount); 100 } else { 101 super.onDraw(canvas); 102 } 103 } 104 } 105