AppCompatImageHelper.java revision 8c3f75732bc35cf683f9014816b26c77d9c8f4e8
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 = TintTypedArray.obtainStyledAttributes(mView.getContext(), attrs, 41 R.styleable.AppCompatImageView, defStyleAttr, 0); 42 try { 43 Drawable d = a.getDrawableIfKnown(R.styleable.AppCompatImageView_android_src); 44 if (d != null) { 45 mView.setImageDrawable(d); 46 } 47 48 final int id = a.getResourceId(R.styleable.AppCompatImageView_srcCompat, -1); 49 if (id != -1) { 50 d = mDrawableManager.getDrawable(mView.getContext(), id); 51 if (d != null) { 52 mView.setImageDrawable(d); 53 } 54 } 55 56 final Drawable drawable = mView.getDrawable(); 57 if (drawable != null) { 58 DrawableUtils.fixDrawable(drawable); 59 } 60 } finally { 61 a.recycle(); 62 } 63 } 64 65 public void setImageResource(int resId) { 66 if (resId != 0) { 67 final Drawable d = mDrawableManager != null 68 ? mDrawableManager.getDrawable(mView.getContext(), resId) 69 : ContextCompat.getDrawable(mView.getContext(), resId); 70 if (d != null) { 71 DrawableUtils.fixDrawable(d); 72 } 73 mView.setImageDrawable(d); 74 } else { 75 mView.setImageDrawable(null); 76 } 77 } 78 79 boolean hasOverlappingRendering() { 80 final Drawable background = mView.getBackground(); 81 if (Build.VERSION.SDK_INT >= 21 82 && background instanceof android.graphics.drawable.RippleDrawable) { 83 // RippleDrawable has an issue on L+ when used with an alpha animation. 84 // This workaround should be disabled when the platform bug is fixed. See b/27715789 85 return false; 86 } 87 return true; 88 } 89} 90