Skip to content

Commit

Permalink
图片剪裁优化,设置图片剪裁宽高比。
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Nov 1, 2019
1 parent 039d018 commit 161a3ce
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void onClick(View v) {
ImageSelector.builder()
.useCamera(true) // 设置是否使用拍照
.setCrop(true) // 设置是否使用图片剪切功能。
.setCropRatio(1.0f) // 图片剪切的宽高比,默认1.0f。宽固定为手机屏幕的宽。
.setSingle(true) //设置是否单选
.canPreview(true) //是否点击放大图片查看,,默认为true
.start(this, REQUEST_CODE); // 打开相册
Expand All @@ -102,6 +103,7 @@ public void onClick(View v) {
//拍照并剪裁
ImageSelector.builder()
.setCrop(true) // 设置是否使用图片剪切功能。
.setCropRatio(1.0f) // 图片剪切的宽高比,默认1.0f。宽固定为手机屏幕的宽。
.onlyTakePhoto(true) // 仅拍照,不打开相册
.start(this, REQUEST_CODE);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ClipImageActivity extends Activity {
private ClipImageView imageView;
private int mRequestCode;
private boolean isCameraImage;
private float cropRatio;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -43,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
mRequestCode = config.requestCode;
config.isSingle = true;
config.maxSelectCount = 0;
cropRatio = config.cropRatio;
setStatusBarColor();
ImageSelectorActivity.openActivity(this, mRequestCode, config);
initView();
Expand Down Expand Up @@ -79,6 +81,8 @@ public void onClick(View v) {
finish();
}
});

imageView.setRatio(cropRatio);
}

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


@Override
public int describeContents() {
return 0;
Expand All @@ -35,6 +37,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte(this.canPreview ? (byte) 1 : (byte) 0);
dest.writeInt(this.maxSelectCount);
dest.writeStringList(this.selected);
dest.writeFloat(this.cropRatio);
dest.writeInt(this.requestCode);
}

Expand All @@ -49,6 +52,7 @@ protected RequestConfig(Parcel in) {
this.canPreview = in.readByte() != 0;
this.maxSelectCount = in.readInt();
this.selected = in.createStringArrayList();
this.cropRatio = in.readFloat();
this.requestCode = in.readInt();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public ImageSelectorBuilder setCrop(boolean isCrop) {
return this;
}

/**
*
* 图片剪切的宽高比,宽固定为手机屏幕的宽。
* @param ratio
* @return
*/
public ImageSelectorBuilder setCropRatio(float ratio){
config.cropRatio = ratio;
return this;
}

/**
* 是否单选
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.view.WindowManager;

public class ClipImageView extends AppCompatImageView {

private PointF mDownPoint;
private PointF mMiddlePoint;
private Matrix mMatrix;
Expand All @@ -46,6 +45,7 @@ public class ClipImageView extends AppCompatImageView {
private float mCircleCenterX, mCircleCenterY;
private float mCircleX, mCircleY;
private boolean isCutImage;
private float mRatio = 1.0f;

public ClipImageView(Context context) {
super(context);
Expand Down Expand Up @@ -95,23 +95,23 @@ public void run() {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
setRadius();
}

private void setRadius() {
mTargetWidth = getScreenWidth(getContext());
mTargetHeight = (int) (mTargetWidth * mRatio);
mCircleCenterX = getWidth() / 2;
mCircleCenterY = getHeight() / 2;
mCircleX = mCircleCenterX - mTargetWidth / 2;
mCircleY = mCircleCenterY - mTargetHeight / 2;
}

private void setRadius() {

int width = getScreenWidth(getContext());
int height = getScreenHeight(getContext());

if (width > height) {
mTargetWidth = height;
mTargetHeight = height;
} else {
mTargetWidth = width;
mTargetHeight = width;
public void setRatio(float ratio) {
if (mRatio != ratio) {
mRatio = ratio;
setRadius();
invalidate();
}
}

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

canvas.restoreToCount(sc);
//清除Xfermode,防止影响下次画图
Expand All @@ -153,10 +156,11 @@ public Bitmap clipImage() {
Bitmap targetBitmap = Bitmap.createBitmap(mTargetWidth, mTargetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
RectF dst = new RectF(-bitmap.getWidth() / 2 + mTargetWidth / 2, -getHeight()
/ 2 + mTargetHeight / 2, bitmap.getWidth() / 2
+ mTargetWidth / 2, getHeight() / 2 + mTargetHeight / 2);

int left = -bitmap.getWidth() / 2 + mTargetWidth / 2;
int top = -getHeight() / 2 + mTargetHeight / 2;
int right = bitmap.getWidth() / 2 + mTargetWidth / 2;
int bottom = getHeight() / 2 + mTargetHeight / 2;
RectF dst = new RectF(left, top, right, bottom);
canvas.drawBitmap(bitmap, null, dst, paint);
setDrawingCacheEnabled(false);
bitmap.recycle();
Expand Down Expand Up @@ -300,33 +304,12 @@ private void midPoint(PointF point, MotionEvent event) {
* 横向、纵向居中
*/
protected void center() {

float height = mBitmapHeight;
float width = mBitmapWidth;
float screenWidth = getWidth();
float screenHeight = getHeight();
float scale = 1f;
if (width >= height) {
scale = screenWidth / width;

if (scale * height < mTargetHeight) {
scale = mTargetHeight / height;
}

} else {
if (height <= screenHeight) {
scale = screenWidth / width;
} else {
scale = screenHeight / height;
}
float scale = Math.max(mTargetWidth / width,mTargetHeight / height);

if (scale * width < mTargetWidth) {
scale = mTargetWidth / width;
}
}

float deltaX = (screenWidth - width * scale) / 2f;
float deltaY = (screenHeight - height * scale) / 2f;
float deltaX = -(width * scale - getWidth()) / 2.0f;
float deltaY = -(height * scale - getHeight()) / 2.0f;
mMatrix.postScale(scale, scale);
mMatrix.postTranslate(deltaX, deltaY);
setImageMatrix(mMatrix);
Expand All @@ -351,4 +334,5 @@ public static int getScreenHeight(Context context) {
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}

}

0 comments on commit 161a3ce

Please sign in to comment.