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 17package com.android.tv.settings.widget; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Canvas; 22import android.graphics.RectF; 23import android.graphics.drawable.Drawable; 24import android.graphics.drawable.LayerDrawable; 25import android.util.AttributeSet; 26import android.widget.ImageView; 27 28import com.android.tv.settings.R; 29 30public class RefcountImageView extends ImageView { 31 32 private boolean mAutoUnrefOnDetach; 33 private boolean mHasClipRect; 34 private final RectF mClipRect = new RectF(); 35 36 public RefcountImageView(Context context) { 37 this(context, null); 38 } 39 40 public RefcountImageView(Context context, AttributeSet attrs) { 41 this(context, attrs, 0); 42 } 43 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 50 public void setAutoUnrefOnDetach(boolean autoUnref) { 51 mAutoUnrefOnDetach = autoUnref; 52 } 53 54 public boolean getAutoUnrefOnDetach() { 55 return mAutoUnrefOnDetach; 56 } 57 58 @Override 59 protected void onDetachedFromWindow() { 60 if (mAutoUnrefOnDetach) { 61 setImageDrawable(null); 62 } 63 super.onDetachedFromWindow(); 64 } 65 66 @Override 67 public void setImageDrawable(Drawable drawable) { 68 Drawable previousDrawable = getDrawable(); 69 super.setImageDrawable(drawable); 70 releaseRef(previousDrawable); 71 } 72 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 84 public void setClipRect(float l, float t, float r, float b) { 85 mClipRect.set(l, t, r, b); 86 mHasClipRect = true; 87 } 88 89 public void clearClipRect() { 90 mHasClipRect = false; 91 } 92 93 @Override 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