1/* 2 * Copyright (C) 2015 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 android.support.v7.widget; 18 19import android.graphics.drawable.Drawable; 20import android.os.Build; 21import android.support.v4.content.ContextCompat; 22import android.support.v7.appcompat.R; 23import android.util.AttributeSet; 24import android.widget.ImageView; 25 26/** 27 * @hide 28 */ 29public class AppCompatImageHelper { 30 31 private final ImageView mView; 32 private final AppCompatDrawableManager mDrawableManager; 33 34 public AppCompatImageHelper(ImageView view, AppCompatDrawableManager drawableManager) { 35 mView = view; 36 mDrawableManager = drawableManager; 37 } 38 39 public void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { 40 TintTypedArray a = null; 41 try { 42 Drawable drawable = mView.getDrawable(); 43 44 if (drawable == null) { 45 a = TintTypedArray.obtainStyledAttributes(mView.getContext(), attrs, 46 R.styleable.AppCompatImageView, defStyleAttr, 0); 47 48 // If the view doesn't already have a drawable (from android:src), try loading 49 // it from srcCompat 50 final int id = a.getResourceId(R.styleable.AppCompatImageView_srcCompat, -1); 51 if (id != -1) { 52 drawable = mDrawableManager.getDrawable(mView.getContext(), id); 53 if (drawable != null) { 54 mView.setImageDrawable(drawable); 55 } 56 } 57 } 58 59 if (drawable != null) { 60 DrawableUtils.fixDrawable(drawable); 61 } 62 } finally { 63 if (a != null) { 64 a.recycle(); 65 } 66 } 67 } 68 69 public void setImageResource(int resId) { 70 if (resId != 0) { 71 final Drawable d = mDrawableManager != null 72 ? mDrawableManager.getDrawable(mView.getContext(), resId) 73 : ContextCompat.getDrawable(mView.getContext(), resId); 74 if (d != null) { 75 DrawableUtils.fixDrawable(d); 76 } 77 mView.setImageDrawable(d); 78 } else { 79 mView.setImageDrawable(null); 80 } 81 } 82 83 boolean hasOverlappingRendering() { 84 final Drawable background = mView.getBackground(); 85 if (Build.VERSION.SDK_INT >= 21 86 && background instanceof android.graphics.drawable.RippleDrawable) { 87 // RippleDrawable has an issue on L+ when used with an alpha animation. 88 // This workaround should be disabled when the platform bug is fixed. See b/27715789 89 return false; 90 } 91 return true; 92 } 93} 94