Skip to content

Commit 2f4a4a9

Browse files
committed
优化代码逻辑
1 parent 6d1499c commit 2f4a4a9

File tree

3 files changed

+66
-82
lines changed

3 files changed

+66
-82
lines changed

app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.kt

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,102 +13,91 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
package com.hippo.drawable
1617

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
3024

3125
/**
3226
* Show a part of the original drawable
3327
*/
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()
3539

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()
5243
if (originWidth <= 0 || originHeight <= 0) {
5344
// Can not clip
54-
mClip = false;
45+
mClip = false
5546
} 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()
6356
}
6457
}
6558

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
7971
}
80-
super.onBoundsChange(bounds);
72+
super.onBoundsChange(bounds)
8173
}
8274

8375

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()
8879
} else {
89-
return super.getIntrinsicWidth();
80+
super.getIntrinsicWidth()
9081
}
9182
}
9283

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()
9787
} else {
98-
return super.getIntrinsicHeight();
88+
super.getIntrinsicHeight()
9989
}
10090
}
10191

102-
@Override
103-
public void draw(@NonNull Canvas canvas) {
92+
override fun draw(canvas: Canvas) {
10493
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)
10998
}
11099
} else {
111-
super.draw(canvas);
100+
super.draw(canvas)
112101
}
113102
}
114103
}

app/src/main/java/com/hippo/lib/image/Image.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ class Image private constructor(
197197
updateBitmap()
198198
mBitmap!!
199199
} else {
200+
if (mObtainedDrawable==null){
201+
return
202+
}
200203
(mObtainedDrawable as BitmapDrawable).bitmap
201204
}
202205
nativeTexImage(

app/src/main/java/com/hippo/widget/LoadImageView.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ private Drawable getImageDrawable() {
150150
}
151151

152152
private void clearDrawable() {
153-
// // Recycle ImageDrawable
154-
// ImageDrawable imageDrawable = getImageDrawable();
155-
// if (imageDrawable != null) {
156-
// imageDrawable.recycle();
157-
// }
158153

159154
// Set drawable null
160155
setImageDrawable(null);
@@ -278,21 +273,18 @@ public void onWait() {
278273
public boolean onGetValue(@NonNull Image value, int source) {
279274
Drawable drawable;
280275
try {
281-
Drawable.ConstantState state = value.getDrawable().getConstantState();
282-
if (state!=null){
283-
drawable = state.newDrawable();
284-
}else {
285-
drawable = value.getDrawable();
286-
}
276+
drawable = value.getDrawable();
287277
} catch (Exception e) {
288278
// The image might be recycled because it is removed from memory cache.
289279
Log.d(TAG, "The image is recycled", e);
290280
return false;
291281
}
292282

293-
// clearDrawable();
294-
295283
if (Integer.MIN_VALUE != mOffsetX) {
284+
Drawable.ConstantState state = value.getDrawable().getConstantState();
285+
if (state!=null){
286+
drawable = state.newDrawable();
287+
}
296288
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
297289
}
298290

0 commit comments

Comments
 (0)