Skip to content

Commit

Permalink
优化代码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojieonly committed Nov 20, 2024
1 parent 6d1499c commit 2f4a4a9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 82 deletions.
127 changes: 58 additions & 69 deletions app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,102 +13,91 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.drawable

package com.hippo.drawable;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableWrapper;
import android.view.Gravity;

import androidx.annotation.NonNull;

import com.hippo.lib.yorozuya.MathUtils;
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.graphics.drawable.DrawableWrapper
import com.hippo.lib.yorozuya.MathUtils

/**
* Show a part of the original drawable
*/
public class PreciselyClipDrawable extends DrawableWrapper {
class PreciselyClipDrawable(
drawable: Drawable,
offsetX: Int,
offsetY: Int,
width: Int,
height: Int
) :
DrawableWrapper(drawable) {
private var mClip = false
private var mScale: RectF = RectF()
private var mTemp: Rect = Rect()

private final boolean mClip;
private RectF mScale;
private Rect mTemp;
// private int offsetX;
// private int offsetY;
// private int width;
// private int height;

public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) {
super(drawable);
// this.offsetX = offsetX;
// this.offsetY = offsetY;
// this.width = width;
// this.height = height;
float originWidth = drawable.getIntrinsicWidth();
float originHeight = drawable.getIntrinsicHeight();
init {
val originWidth = drawable.intrinsicWidth.toFloat()
val originHeight = drawable.intrinsicHeight.toFloat()
if (originWidth <= 0 || originHeight <= 0) {
// Can not clip
mClip = false;
mClip = false
} else {
mClip = true;
mScale = new RectF();
mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f));
mTemp = new Rect();
mClip = true
mScale = RectF()
mScale.set(
MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f)
)
mTemp = Rect()
}
}

@Override
protected void onBoundsChange(@NonNull Rect bounds) {
if (mClip&&!mScale.isEmpty()) {
mTemp.left = (int) ((mScale.left * bounds.right - mScale.right * bounds.left) /
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left)));
mTemp.right = (int) (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) /
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left)));
mTemp.top = (int) ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) /
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top)));
mTemp.bottom = (int) (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) /
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top)));
super.onBoundsChange(mTemp);
return;
override fun onBoundsChange(bounds: Rect) {
if (mClip && !mScale.isEmpty) {
mTemp.left = ((mScale.left * bounds.right - mScale.right * bounds.left) /
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))).toInt()
mTemp.right = (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) /
(mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))).toInt()
mTemp.top = ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) /
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))).toInt()
mTemp.bottom = (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) /
(mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))).toInt()
super.onBoundsChange(mTemp)
return
}
super.onBoundsChange(bounds);
super.onBoundsChange(bounds)
}


@Override
public int getIntrinsicWidth() {
if (mClip) {
return (int) (super.getIntrinsicWidth() * mScale.width());
override fun getIntrinsicWidth(): Int {
return if (mClip) {
(super.getIntrinsicWidth() * mScale.width()).toInt()
} else {
return super.getIntrinsicWidth();
super.getIntrinsicWidth()
}
}

@Override
public int getIntrinsicHeight() {
if (mClip) {
return (int) (super.getIntrinsicHeight() * mScale.height());
override fun getIntrinsicHeight(): Int {
return if (mClip) {
(super.getIntrinsicHeight() * mScale.height()).toInt()
} else {
return super.getIntrinsicHeight();
super.getIntrinsicHeight()
}
}

@Override
public void draw(@NonNull Canvas canvas) {
override fun draw(canvas: Canvas) {
if (mClip) {
if (!mScale.isEmpty()) {
Rect rect = getBounds();
canvas.clipRect(rect);
super.draw(canvas);
if (!mScale.isEmpty) {
val rect = bounds
canvas.clipRect(rect)
super.draw(canvas)
}
} else {
super.draw(canvas);
super.draw(canvas)
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/hippo/lib/image/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ class Image private constructor(
updateBitmap()
mBitmap!!
} else {
if (mObtainedDrawable==null){
return
}
(mObtainedDrawable as BitmapDrawable).bitmap
}
nativeTexImage(
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/java/com/hippo/widget/LoadImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ private Drawable getImageDrawable() {
}

private void clearDrawable() {
// // Recycle ImageDrawable
// ImageDrawable imageDrawable = getImageDrawable();
// if (imageDrawable != null) {
// imageDrawable.recycle();
// }

// Set drawable null
setImageDrawable(null);
Expand Down Expand Up @@ -278,21 +273,18 @@ public void onWait() {
public boolean onGetValue(@NonNull Image value, int source) {
Drawable drawable;
try {
Drawable.ConstantState state = value.getDrawable().getConstantState();
if (state!=null){
drawable = state.newDrawable();
}else {
drawable = value.getDrawable();
}
drawable = value.getDrawable();
} catch (Exception e) {
// The image might be recycled because it is removed from memory cache.
Log.d(TAG, "The image is recycled", e);
return false;
}

// clearDrawable();

if (Integer.MIN_VALUE != mOffsetX) {
Drawable.ConstantState state = value.getDrawable().getConstantState();
if (state!=null){
drawable = state.newDrawable();
}
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
}

Expand Down

0 comments on commit 2f4a4a9

Please sign in to comment.