Переглянути джерело

Revert "feat:修改图片裁剪方法"

This reverts commit 65e9762b47c7022e3b200c02117ead0550719963.
XiaodongLin 7 місяців тому
батько
коміт
ecf4eddfc9

+ 1 - 1
app/src/main/java/com/adealink/weparty/imageselect/clip/ClipImageActivity.java

@@ -103,7 +103,7 @@ public class ClipImageActivity extends BaseActivity implements View.OnClickListe
         if (id == R.id.clip_image_save) {
             Bitmap clippedBitmap;
             if (!mHightQuality) {
-                clippedBitmap= mClipIv.clipImage(mImagePath);
+                clippedBitmap= mClipIv.clipImage();
             } else {
                 clippedBitmap = mClipIv.clipImage2(mImagePath);
             }

+ 48 - 38
app/src/main/java/com/adealink/weparty/imageselect/clip/ClipImageView.java

@@ -215,56 +215,66 @@ public class ClipImageView extends androidx.appcompat.widget.AppCompatImageView
         super.setImageBitmap(bm);
     }
 
-    public Bitmap clipImage(String mImagePath) {
-        // 获取描边宽度,用于避免边缘越界
-        int strokeWidth = (int) mOutlinePaint.getStrokeWidth();
+    @Override
+    public void onDraw(Canvas canvas) {
+        try {
+            if (mBitmap == null ||mBitmap.isRecycled()) {
+                return;
+            }
+            if (mMatrix == null) {
+                init();
+            }
+            canvas.drawBitmap(mBitmap, mMatrix, null);
+            canvas.drawRect(mGrayTopRect, mGrayPaint);
+            canvas.drawRect(mGrayBottomRect, mGrayPaint);
+            canvas.drawRect(mGrayLeftRect, mGrayPaint);
+            canvas.drawRect(mGrayRightRect, mGrayPaint);
+            canvas.drawPath(mOutlinePath, mOutlinePaint);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-        // 计算裁剪区域的左上角坐标
-        int clipLeft = mOutlineRect.left + strokeWidth;
-        int clipTop = mOutlineRect.top + strokeWidth;
+    @Deprecated
+    public Bitmap clipImage() {
 
-        // 计算裁剪区域的宽高)
-        int bmWidth = mOutlineRect.width() - strokeWidth * 2;
-        int bmHeight = mOutlineRect.height() - strokeWidth * 2;
+        printMatrix(mMatrix);
 
-        // 如果宽高非法,直接返回 null
-        if (bmWidth <= 0 || bmHeight <= 0 || clipLeft < 0 || clipTop < 0) {
-            return null;
-        }
+        int strokeWidth = (int) mOutlinePaint.getStrokeWidth();
 
-        Bitmap output = null;
-        try {
-            // 创建一个仅用于绘制目标区域大小的Bitmap,减少内存占用
-            output = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
-            Canvas canvas = new Canvas(output);
+        // 裁剪框的长宽
+        int bmWidth = (mOutlineRect.width() - strokeWidth * 2);
+        int bmHeight = (mOutlineRect.height() - strokeWidth * 2);
 
-            // 平移画布,让目标区域对齐到 (0, 0),避免绘制整张 View
-            canvas.translate(-clipLeft, -clipTop);
+        // 取drawingCache的src部分绘制到新的bitmap的dstRect去
+        Rect srcRect = new Rect();
+        srcRect.set(mOutlineRect.left + strokeWidth, mOutlineRect.top + strokeWidth,
+                mOutlineRect.right - strokeWidth, mOutlineRect.bottom - strokeWidth);
+        if(bmHeight<=0||bmWidth<=0){
+            return null;
+        }
 
-            // 防止越界绘制
-            canvas.clipRect(clipLeft, clipTop, clipLeft + bmWidth, clipTop + bmHeight);
+        // 创建一个裁剪框大小的位图
+        savedBitmap = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
 
-            // 将裁剪区域的内容绘制到目标canvas
-            draw(canvas);
+        Canvas cv = new Canvas(savedBitmap);
+        setDrawingCacheEnabled(false);
+        setWillNotCacheDrawing(false);
+        setDrawingCacheEnabled(true);
 
-            // 如果绘制后的 Bitmap 被回收,则触发兜底
-            if (output.isRecycled()) {
-                return clipImage2(mImagePath);
-            }
+        Rect dstRect = new Rect();
+        dstRect.set(0, 0, bmWidth, bmHeight);
 
-        } catch (OutOfMemoryError e) {
-            // 内存溢出
-            Log.e(TAG, "clipImage OOM!", e);
-            return clipImage2(mImagePath);
-        } catch (Exception e) {
-            Log.e(TAG, "clipImage Clip Error", e);
-            return clipImage2(mImagePath);
+        Bitmap drawingCache = getDrawingCache(); // 取drawingCache
+        if (drawingCache != null) {
+            cv.drawBitmap(drawingCache, srcRect, dstRect, null);
+        } else {
+            savedBitmap = null;
         }
-
-        // 返回裁剪成功的 Bitmap
-        return output;
+        return savedBitmap;
     }
 
+
     /**
      * @param mImagePath
      * @return