|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
| 16 | +package com.hippo.drawable |
16 | 17 |
|
17 |
| -package com.hippo.drawable; |
18 |
| - |
19 |
| -import android.graphics.Canvas; |
20 |
| -import android.graphics.Rect; |
21 |
| -import android.graphics.RectF; |
22 |
| -import android.graphics.drawable.ClipDrawable; |
23 |
| -import android.graphics.drawable.Drawable; |
24 |
| -import android.graphics.drawable.DrawableWrapper; |
25 |
| -import android.view.Gravity; |
26 |
| - |
27 |
| -import androidx.annotation.NonNull; |
28 |
| - |
29 |
| -import com.hippo.lib.yorozuya.MathUtils; |
| 18 | +import android.graphics.Canvas |
| 19 | +import android.graphics.Rect |
| 20 | +import android.graphics.RectF |
| 21 | +import android.graphics.drawable.Drawable |
| 22 | +import android.graphics.drawable.DrawableWrapper |
| 23 | +import com.hippo.lib.yorozuya.MathUtils |
30 | 24 |
|
31 | 25 | /**
|
32 | 26 | * Show a part of the original drawable
|
33 | 27 | */
|
34 |
| -public class PreciselyClipDrawable extends DrawableWrapper { |
| 28 | +class PreciselyClipDrawable( |
| 29 | + drawable: Drawable, |
| 30 | + offsetX: Int, |
| 31 | + offsetY: Int, |
| 32 | + width: Int, |
| 33 | + height: Int |
| 34 | +) : |
| 35 | + DrawableWrapper(drawable) { |
| 36 | + private var mClip = false |
| 37 | + private var mScale: RectF = RectF() |
| 38 | + private var mTemp: Rect = Rect() |
35 | 39 |
|
36 |
| - private final boolean mClip; |
37 |
| - private RectF mScale; |
38 |
| - private Rect mTemp; |
39 |
| -// private int offsetX; |
40 |
| -// private int offsetY; |
41 |
| -// private int width; |
42 |
| -// private int height; |
43 |
| - |
44 |
| - public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) { |
45 |
| - super(drawable); |
46 |
| -// this.offsetX = offsetX; |
47 |
| -// this.offsetY = offsetY; |
48 |
| -// this.width = width; |
49 |
| -// this.height = height; |
50 |
| - float originWidth = drawable.getIntrinsicWidth(); |
51 |
| - float originHeight = drawable.getIntrinsicHeight(); |
| 40 | + init { |
| 41 | + val originWidth = drawable.intrinsicWidth.toFloat() |
| 42 | + val originHeight = drawable.intrinsicHeight.toFloat() |
52 | 43 | if (originWidth <= 0 || originHeight <= 0) {
|
53 | 44 | // Can not clip
|
54 |
| - mClip = false; |
| 45 | + mClip = false |
55 | 46 | } else {
|
56 |
| - mClip = true; |
57 |
| - mScale = new RectF(); |
58 |
| - mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f), |
59 |
| - MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f), |
60 |
| - MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f), |
61 |
| - MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f)); |
62 |
| - mTemp = new Rect(); |
| 47 | + mClip = true |
| 48 | + mScale = RectF() |
| 49 | + mScale.set( |
| 50 | + MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f), |
| 51 | + MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f), |
| 52 | + MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f), |
| 53 | + MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f) |
| 54 | + ) |
| 55 | + mTemp = Rect() |
63 | 56 | }
|
64 | 57 | }
|
65 | 58 |
|
66 |
| - @Override |
67 |
| - protected void onBoundsChange(@NonNull Rect bounds) { |
68 |
| - if (mClip&&!mScale.isEmpty()) { |
69 |
| - mTemp.left = (int) ((mScale.left * bounds.right - mScale.right * bounds.left) / |
70 |
| - (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))); |
71 |
| - mTemp.right = (int) (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) / |
72 |
| - (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))); |
73 |
| - mTemp.top = (int) ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) / |
74 |
| - (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))); |
75 |
| - mTemp.bottom = (int) (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) / |
76 |
| - (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))); |
77 |
| - super.onBoundsChange(mTemp); |
78 |
| - return; |
| 59 | + override fun onBoundsChange(bounds: Rect) { |
| 60 | + if (mClip && !mScale.isEmpty) { |
| 61 | + mTemp.left = ((mScale.left * bounds.right - mScale.right * bounds.left) / |
| 62 | + (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))).toInt() |
| 63 | + mTemp.right = (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) / |
| 64 | + (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))).toInt() |
| 65 | + mTemp.top = ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) / |
| 66 | + (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))).toInt() |
| 67 | + mTemp.bottom = (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) / |
| 68 | + (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))).toInt() |
| 69 | + super.onBoundsChange(mTemp) |
| 70 | + return |
79 | 71 | }
|
80 |
| - super.onBoundsChange(bounds); |
| 72 | + super.onBoundsChange(bounds) |
81 | 73 | }
|
82 | 74 |
|
83 | 75 |
|
84 |
| - @Override |
85 |
| - public int getIntrinsicWidth() { |
86 |
| - if (mClip) { |
87 |
| - return (int) (super.getIntrinsicWidth() * mScale.width()); |
| 76 | + override fun getIntrinsicWidth(): Int { |
| 77 | + return if (mClip) { |
| 78 | + (super.getIntrinsicWidth() * mScale.width()).toInt() |
88 | 79 | } else {
|
89 |
| - return super.getIntrinsicWidth(); |
| 80 | + super.getIntrinsicWidth() |
90 | 81 | }
|
91 | 82 | }
|
92 | 83 |
|
93 |
| - @Override |
94 |
| - public int getIntrinsicHeight() { |
95 |
| - if (mClip) { |
96 |
| - return (int) (super.getIntrinsicHeight() * mScale.height()); |
| 84 | + override fun getIntrinsicHeight(): Int { |
| 85 | + return if (mClip) { |
| 86 | + (super.getIntrinsicHeight() * mScale.height()).toInt() |
97 | 87 | } else {
|
98 |
| - return super.getIntrinsicHeight(); |
| 88 | + super.getIntrinsicHeight() |
99 | 89 | }
|
100 | 90 | }
|
101 | 91 |
|
102 |
| - @Override |
103 |
| - public void draw(@NonNull Canvas canvas) { |
| 92 | + override fun draw(canvas: Canvas) { |
104 | 93 | if (mClip) {
|
105 |
| - if (!mScale.isEmpty()) { |
106 |
| - Rect rect = getBounds(); |
107 |
| - canvas.clipRect(rect); |
108 |
| - super.draw(canvas); |
| 94 | + if (!mScale.isEmpty) { |
| 95 | + val rect = bounds |
| 96 | + canvas.clipRect(rect) |
| 97 | + super.draw(canvas) |
109 | 98 | }
|
110 | 99 | } else {
|
111 |
| - super.draw(canvas); |
| 100 | + super.draw(canvas) |
112 | 101 | }
|
113 | 102 | }
|
114 | 103 | }
|
0 commit comments