Skip to content

Commit 161a3ce

Browse files
author
teach
committed
图片剪裁优化,设置图片剪裁宽高比。
1 parent 039d018 commit 161a3ce

File tree

5 files changed

+48
-43
lines changed

5 files changed

+48
-43
lines changed

app/src/main/java/com/donkingliang/imageselectdemo/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void onClick(View v) {
8686
ImageSelector.builder()
8787
.useCamera(true) // 设置是否使用拍照
8888
.setCrop(true) // 设置是否使用图片剪切功能。
89+
.setCropRatio(1.0f) // 图片剪切的宽高比,默认1.0f。宽固定为手机屏幕的宽。
8990
.setSingle(true) //设置是否单选
9091
.canPreview(true) //是否点击放大图片查看,,默认为true
9192
.start(this, REQUEST_CODE); // 打开相册
@@ -102,6 +103,7 @@ public void onClick(View v) {
102103
//拍照并剪裁
103104
ImageSelector.builder()
104105
.setCrop(true) // 设置是否使用图片剪切功能。
106+
.setCropRatio(1.0f) // 图片剪切的宽高比,默认1.0f。宽固定为手机屏幕的宽。
105107
.onlyTakePhoto(true) // 仅拍照,不打开相册
106108
.start(this, REQUEST_CODE);
107109
break;

imageselector/src/main/java/com/donkingliang/imageselector/ClipImageActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ClipImageActivity extends Activity {
3131
private ClipImageView imageView;
3232
private int mRequestCode;
3333
private boolean isCameraImage;
34+
private float cropRatio;
3435

3536
@Override
3637
protected void onCreate(Bundle savedInstanceState) {
@@ -43,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
4344
mRequestCode = config.requestCode;
4445
config.isSingle = true;
4546
config.maxSelectCount = 0;
47+
cropRatio = config.cropRatio;
4648
setStatusBarColor();
4749
ImageSelectorActivity.openActivity(this, mRequestCode, config);
4850
initView();
@@ -79,6 +81,8 @@ public void onClick(View v) {
7981
finish();
8082
}
8183
});
84+
85+
imageView.setRatio(cropRatio);
8286
}
8387

8488
@Override

imageselector/src/main/java/com/donkingliang/imageselector/entry/RequestConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public class RequestConfig implements Parcelable {
1919
public boolean canPreview = true; // 是否可以点击图片预览
2020
public int maxSelectCount; //图片的最大选择数量,小于等于0时,不限数量,isSingle为false时才有用。
2121
public ArrayList<String> selected; //接收从外面传进来的已选择的图片列表。当用户原来已经有选择过图片,重新打开选择器,允许用户把先前选过的图片传进来,并把这些图片默认为选中状态。
22+
public float cropRatio = 1.0f; // 图片剪切的宽高比,宽固定为手机屏幕的宽。
2223
public int requestCode;
2324

25+
2426
@Override
2527
public int describeContents() {
2628
return 0;
@@ -35,6 +37,7 @@ public void writeToParcel(Parcel dest, int flags) {
3537
dest.writeByte(this.canPreview ? (byte) 1 : (byte) 0);
3638
dest.writeInt(this.maxSelectCount);
3739
dest.writeStringList(this.selected);
40+
dest.writeFloat(this.cropRatio);
3841
dest.writeInt(this.requestCode);
3942
}
4043

@@ -49,6 +52,7 @@ protected RequestConfig(Parcel in) {
4952
this.canPreview = in.readByte() != 0;
5053
this.maxSelectCount = in.readInt();
5154
this.selected = in.createStringArrayList();
55+
this.cropRatio = in.readFloat();
5256
this.requestCode = in.readInt();
5357
}
5458

imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageSelector.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public ImageSelectorBuilder setCrop(boolean isCrop) {
6464
return this;
6565
}
6666

67+
/**
68+
*
69+
* 图片剪切的宽高比,宽固定为手机屏幕的宽。
70+
* @param ratio
71+
* @return
72+
*/
73+
public ImageSelectorBuilder setCropRatio(float ratio){
74+
config.cropRatio = ratio;
75+
return this;
76+
}
77+
6778
/**
6879
* 是否单选
6980
*

imageselector/src/main/java/com/donkingliang/imageselector/view/ClipImageView.java

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.view.WindowManager;
2020

2121
public class ClipImageView extends AppCompatImageView {
22-
2322
private PointF mDownPoint;
2423
private PointF mMiddlePoint;
2524
private Matrix mMatrix;
@@ -46,6 +45,7 @@ public class ClipImageView extends AppCompatImageView {
4645
private float mCircleCenterX, mCircleCenterY;
4746
private float mCircleX, mCircleY;
4847
private boolean isCutImage;
48+
private float mRatio = 1.0f;
4949

5050
public ClipImageView(Context context) {
5151
super(context);
@@ -95,23 +95,23 @@ public void run() {
9595
@Override
9696
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9797
super.onLayout(changed, left, top, right, bottom);
98+
setRadius();
99+
}
100+
101+
private void setRadius() {
102+
mTargetWidth = getScreenWidth(getContext());
103+
mTargetHeight = (int) (mTargetWidth * mRatio);
98104
mCircleCenterX = getWidth() / 2;
99105
mCircleCenterY = getHeight() / 2;
100106
mCircleX = mCircleCenterX - mTargetWidth / 2;
101107
mCircleY = mCircleCenterY - mTargetHeight / 2;
102108
}
103109

104-
private void setRadius() {
105-
106-
int width = getScreenWidth(getContext());
107-
int height = getScreenHeight(getContext());
108-
109-
if (width > height) {
110-
mTargetWidth = height;
111-
mTargetHeight = height;
112-
} else {
113-
mTargetWidth = width;
114-
mTargetHeight = width;
110+
public void setRatio(float ratio) {
111+
if (mRatio != ratio) {
112+
mRatio = ratio;
113+
setRadius();
114+
invalidate();
115115
}
116116
}
117117

@@ -126,14 +126,17 @@ protected void onDraw(Canvas canvas) {
126126
rf = new RectF(r);
127127
}
128128
// 画入前景圆形蒙板层
129-
int sc = canvas.saveLayer(rf, null, Canvas.ALL_SAVE_FLAG);
129+
int sc = canvas.saveLayer(rf, null, Canvas.ALL_SAVE_FLAG);
130130
//画入矩形黑色半透明蒙板层
131131
canvas.drawRect(r, mFrontGroundPaint);
132132
//设置Xfermode,目的是为了去除矩形黑色半透明蒙板层和圆形的相交部分
133133
mFrontGroundPaint.setXfermode(mXfermode);
134134
//画入正方形
135-
canvas.drawRect(mCircleCenterX - mTargetWidth / 2, mCircleCenterY - mTargetHeight / 2,
136-
mCircleCenterX + mTargetWidth / 2, mCircleCenterY + mTargetHeight / 2, mFrontGroundPaint);
135+
float left = mCircleCenterX - mTargetWidth / 2;
136+
float top = mCircleCenterY - mTargetHeight / 2;
137+
float right = mCircleCenterX + mTargetWidth / 2;
138+
float bottom = mCircleCenterY + mTargetHeight / 2;
139+
canvas.drawRect(left, top, right, bottom, mFrontGroundPaint);
137140

138141
canvas.restoreToCount(sc);
139142
//清除Xfermode,防止影响下次画图
@@ -153,10 +156,11 @@ public Bitmap clipImage() {
153156
Bitmap targetBitmap = Bitmap.createBitmap(mTargetWidth, mTargetHeight,
154157
Bitmap.Config.ARGB_8888);
155158
Canvas canvas = new Canvas(targetBitmap);
156-
RectF dst = new RectF(-bitmap.getWidth() / 2 + mTargetWidth / 2, -getHeight()
157-
/ 2 + mTargetHeight / 2, bitmap.getWidth() / 2
158-
+ mTargetWidth / 2, getHeight() / 2 + mTargetHeight / 2);
159-
159+
int left = -bitmap.getWidth() / 2 + mTargetWidth / 2;
160+
int top = -getHeight() / 2 + mTargetHeight / 2;
161+
int right = bitmap.getWidth() / 2 + mTargetWidth / 2;
162+
int bottom = getHeight() / 2 + mTargetHeight / 2;
163+
RectF dst = new RectF(left, top, right, bottom);
160164
canvas.drawBitmap(bitmap, null, dst, paint);
161165
setDrawingCacheEnabled(false);
162166
bitmap.recycle();
@@ -300,33 +304,12 @@ private void midPoint(PointF point, MotionEvent event) {
300304
* 横向、纵向居中
301305
*/
302306
protected void center() {
303-
304307
float height = mBitmapHeight;
305308
float width = mBitmapWidth;
306-
float screenWidth = getWidth();
307-
float screenHeight = getHeight();
308-
float scale = 1f;
309-
if (width >= height) {
310-
scale = screenWidth / width;
311-
312-
if (scale * height < mTargetHeight) {
313-
scale = mTargetHeight / height;
314-
}
315-
316-
} else {
317-
if (height <= screenHeight) {
318-
scale = screenWidth / width;
319-
} else {
320-
scale = screenHeight / height;
321-
}
309+
float scale = Math.max(mTargetWidth / width,mTargetHeight / height);
322310

323-
if (scale * width < mTargetWidth) {
324-
scale = mTargetWidth / width;
325-
}
326-
}
327-
328-
float deltaX = (screenWidth - width * scale) / 2f;
329-
float deltaY = (screenHeight - height * scale) / 2f;
311+
float deltaX = -(width * scale - getWidth()) / 2.0f;
312+
float deltaY = -(height * scale - getHeight()) / 2.0f;
330313
mMatrix.postScale(scale, scale);
331314
mMatrix.postTranslate(deltaX, deltaY);
332315
setImageMatrix(mMatrix);
@@ -351,4 +334,5 @@ public static int getScreenHeight(Context context) {
351334
wm.getDefaultDisplay().getMetrics(outMetrics);
352335
return outMetrics.heightPixels;
353336
}
337+
354338
}

0 commit comments

Comments
 (0)