|
16 | 16 |
|
17 | 17 | package com.google.android.material.drawable; |
18 | 18 |
|
| 19 | +import android.content.res.Resources; |
19 | 20 | import android.graphics.drawable.Drawable; |
| 21 | +import android.os.Build.VERSION_CODES; |
20 | 22 | import androidx.appcompat.graphics.drawable.DrawableWrapperCompat; |
21 | 23 | import androidx.annotation.NonNull; |
| 24 | +import androidx.annotation.Nullable; |
| 25 | +import androidx.annotation.RequiresApi; |
22 | 26 | import androidx.annotation.RestrictTo; |
23 | 27 | import androidx.annotation.RestrictTo.Scope; |
24 | 28 |
|
|
28 | 32 | */ |
29 | 33 | @RestrictTo(Scope.LIBRARY_GROUP) |
30 | 34 | public class ScaledDrawableWrapper extends DrawableWrapperCompat { |
31 | | - private final int width; |
32 | | - private final int height; |
| 35 | + private ScaledDrawableWrapperState state; |
| 36 | + private boolean mutated; |
33 | 37 |
|
34 | 38 | public ScaledDrawableWrapper(@NonNull Drawable drawable, int width, int height) { |
35 | 39 | super(drawable); |
36 | | - this.width = width; |
37 | | - this.height = height; |
| 40 | + state = new ScaledDrawableWrapperState(getConstantStateFrom(drawable), width, height); |
| 41 | + } |
| 42 | + |
| 43 | + @Nullable |
| 44 | + private ConstantState getConstantStateFrom(@Nullable Drawable drawable) { |
| 45 | + return drawable != null ? drawable.getConstantState() : null; |
38 | 46 | } |
39 | 47 |
|
40 | 48 | @Override |
41 | 49 | public int getIntrinsicWidth() { |
42 | | - return width; |
| 50 | + return state.width; |
43 | 51 | } |
44 | 52 |
|
45 | 53 | @Override |
46 | 54 | public int getIntrinsicHeight() { |
47 | | - return height; |
| 55 | + return state.height; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void setDrawable(@Nullable Drawable drawable) { |
| 60 | + super.setDrawable(drawable); |
| 61 | + |
| 62 | + if (state != null) { |
| 63 | + state.wrappedDrawableState = getConstantStateFrom(drawable); |
| 64 | + mutated = false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Nullable |
| 69 | + @Override |
| 70 | + public ConstantState getConstantState() { |
| 71 | + return state.canConstantState() ? state : null; |
| 72 | + } |
| 73 | + |
| 74 | + @NonNull |
| 75 | + @Override |
| 76 | + public Drawable mutate() { |
| 77 | + if (!mutated && super.mutate() == this) { |
| 78 | + Drawable drawable = getDrawable(); |
| 79 | + if (drawable != null) { |
| 80 | + drawable.mutate(); |
| 81 | + } |
| 82 | + |
| 83 | + state = |
| 84 | + new ScaledDrawableWrapperState(getConstantStateFrom(drawable), state.width, state.height); |
| 85 | + mutated = true; |
| 86 | + } |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + private static final class ScaledDrawableWrapperState extends ConstantState { |
| 92 | + private ConstantState wrappedDrawableState; |
| 93 | + private final int width; |
| 94 | + private final int height; |
| 95 | + |
| 96 | + ScaledDrawableWrapperState( |
| 97 | + @Nullable ConstantState wrappedDrawableState, int width, int height) { |
| 98 | + this.wrappedDrawableState = wrappedDrawableState; |
| 99 | + this.width = width; |
| 100 | + this.height = height; |
| 101 | + } |
| 102 | + |
| 103 | + @NonNull |
| 104 | + @Override |
| 105 | + public Drawable newDrawable() { |
| 106 | + Drawable newWrappedDrawable = wrappedDrawableState.newDrawable(); |
| 107 | + return new ScaledDrawableWrapper(newWrappedDrawable, width, height); |
| 108 | + } |
| 109 | + |
| 110 | + @NonNull |
| 111 | + @Override |
| 112 | + public Drawable newDrawable(@Nullable Resources res) { |
| 113 | + Drawable newWrappedDrawable = wrappedDrawableState.newDrawable(res); |
| 114 | + return new ScaledDrawableWrapper(newWrappedDrawable, width, height); |
| 115 | + } |
| 116 | + |
| 117 | + @RequiresApi(VERSION_CODES.LOLLIPOP) |
| 118 | + @NonNull |
| 119 | + @Override |
| 120 | + public Drawable newDrawable(@Nullable Resources res, @Nullable Resources.Theme theme) { |
| 121 | + Drawable newWrappedDrawable = wrappedDrawableState.newDrawable(res, theme); |
| 122 | + return new ScaledDrawableWrapper(newWrappedDrawable, width, height); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int getChangingConfigurations() { |
| 127 | + return wrappedDrawableState != null ? wrappedDrawableState.getChangingConfigurations() : 0; |
| 128 | + } |
| 129 | + |
| 130 | + boolean canConstantState() { |
| 131 | + return wrappedDrawableState != null; |
| 132 | + } |
48 | 133 | } |
49 | 134 | } |
0 commit comments