AppCompatImageHelper.java revision 8e10080c914d1ad0784394fa3026b85535535847
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 static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.graphics.drawable.Drawable; 22import android.os.Build; 23import android.support.annotation.RestrictTo; 24import android.support.v7.appcompat.R; 25import android.support.v7.content.res.AppCompatResources; 26import android.util.AttributeSet; 27import android.widget.ImageView; 28 29/** 30 * @hide 31 */ 32@RestrictTo(LIBRARY_GROUP) 33public class AppCompatImageHelper { 34 35 private final ImageView mView; 36 37 public AppCompatImageHelper(ImageView view) { 38 mView = view; 39 } 40 41 public void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { 42 TintTypedArray a = null; 43 try { 44 Drawable drawable = mView.getDrawable(); 45 46 if (drawable == null) { 47 a = TintTypedArray.obtainStyledAttributes(mView.getContext(), attrs, 48 R.styleable.AppCompatImageView, defStyleAttr, 0); 49 50 // If the view doesn't already have a drawable (from android:src), try loading 51 // it from srcCompat 52 final int id = a.getResourceId(R.styleable.AppCompatImageView_srcCompat, -1); 53 if (id != -1) { 54 drawable = AppCompatResources.getDrawable(mView.getContext(), id); 55 if (drawable != null) { 56 mView.setImageDrawable(drawable); 57 } 58 } 59 } 60 61 if (drawable != null) { 62 DrawableUtils.fixDrawable(drawable); 63 } 64 } finally { 65 if (a != null) { 66 a.recycle(); 67 } 68 } 69 } 70 71 public void setImageResource(int resId) { 72 if (resId != 0) { 73 final Drawable d = AppCompatResources.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