Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve circle dimen layer in 19+ devices #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions ucrop/src/main/java/com/yalantis/ucrop/view/OverlayView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yalantis.ucrop.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
Expand Down Expand Up @@ -55,7 +56,9 @@ public class OverlayView extends View {
private boolean mShowCropFrame, mShowCropGrid;
private boolean mCircleDimmedLayer;
private int mDimmedColor;
private Path mDimmedPath = new Path();
private Path mCircularPath = new Path();
private Paint mDimmedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mDimmedStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mCropGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mCropFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Expand Down Expand Up @@ -255,6 +258,11 @@ private void updateGridPoints() {
mCropGridCorners = RectUtils.getCornersFromRect(mCropViewRect);
mCropGridCenter = RectUtils.getCenterFromRect(mCropViewRect);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mDimmedPath.reset();
mDimmedPath.addRect(mCropViewRect, Path.Direction.CW);
}

mGridPoints = null;
mCircularPath.reset();
mCircularPath.addCircle(mCropViewRect.centerX(), mCropViewRect.centerY(),
Expand Down Expand Up @@ -445,17 +453,28 @@ private int getCurrentTouchIndex(float touchX, float touchY) {
*
* @param canvas - valid canvas object
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
protected void drawDimmedLayer(@NonNull Canvas canvas) {
canvas.save();
if (mCircleDimmedLayer) {
canvas.clipPath(mCircularPath, Region.Op.DIFFERENCE);
boolean isPre19 = Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;

if (isPre19) {
canvas.save();
if (mCircleDimmedLayer) {
canvas.clipPath(mCircularPath, Region.Op.DIFFERENCE);
} else {
canvas.clipRect(mCropViewRect, Region.Op.DIFFERENCE);
}
canvas.drawColor(mDimmedColor);
canvas.restore();
} else {
canvas.clipRect(mCropViewRect, Region.Op.DIFFERENCE);
if (mCircleDimmedLayer) {
mDimmedPath.op(mCircularPath, Path.Op.DIFFERENCE);
}
canvas.drawPath(mDimmedPath, mDimmedPaint);
}
canvas.drawColor(mDimmedColor);
canvas.restore();

if (mCircleDimmedLayer) { // Draw 1px stroke to fix antialias
// Draw 1px stroke to fix antialias, don't need to draw 1px in 19+ devices anymore.
if (mCircleDimmedLayer && isPre19) {
canvas.drawCircle(mCropViewRect.centerX(), mCropViewRect.centerY(),
Math.min(mCropViewRect.width(), mCropViewRect.height()) / 2.f, mDimmedStrokePaint);
}
Expand Down Expand Up @@ -524,6 +543,8 @@ protected void processStyledAttributes(@NonNull TypedArray a) {
mCircleDimmedLayer = a.getBoolean(R.styleable.ucrop_UCropView_ucrop_circle_dimmed_layer, DEFAULT_CIRCLE_DIMMED_LAYER);
mDimmedColor = a.getColor(R.styleable.ucrop_UCropView_ucrop_dimmed_color,
getResources().getColor(R.color.ucrop_color_default_dimmed));
mDimmedPaint.setColor(mDimmedColor);
mDimmedPaint.setStyle(Paint.Style.FILL);
mDimmedStrokePaint.setColor(mDimmedColor);
mDimmedStrokePaint.setStyle(Paint.Style.STROKE);
mDimmedStrokePaint.setStrokeWidth(1);
Expand Down