Skip to content

Commit

Permalink
fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Othershe committed Dec 8, 2018
1 parent f10b95d commit 7c34555
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ allprojects {
**Step 2. 添加项目依赖**
``` gradle
dependencies {
implementation 'com.github.SheHuan:NiceImageView:1.0.3'
implementation 'com.github.SheHuan:NiceImageView:1.0.5'
}
```
**Step 3. 在布局文件中添加CornerLabelView**
Expand Down
4 changes: 1 addition & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.othershe.niceimageview.test"
applicationId "com.shehuan.niv.test"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -26,5 +25,4 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation project(':niceimageview')
implementation 'pub.devrel:easypermissions:0.4.2'
}
3 changes: 0 additions & 3 deletions niceimageview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ android {
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
Expand Down
27 changes: 22 additions & 5 deletions niceimageview/src/main/java/com/shehuan/niv/NiceImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
Expand Down Expand Up @@ -46,8 +47,9 @@ public class NiceImageView extends AppCompatImageView {
private RectF srcRectF; // 图片占的矩形区域
private RectF borderRectF; // 边框的矩形区域

private Path path = new Path();
private Paint paint = new Paint();
private Paint paint;
private Path path; // 用来裁剪图片的ptah
private Path srcPath; // 图片区域大小的path

public NiceImageView(Context context) {
this(context, null);
Expand Down Expand Up @@ -99,7 +101,15 @@ public NiceImageView(Context context, @Nullable AttributeSet attrs, int defStyle
borderRectF = new RectF();
srcRectF = new RectF();

xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
paint = new Paint();
path = new Path();

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
} else {
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
srcPath = new Path();
}

calculateRadii();
clearInnerBorderWidth();
Expand All @@ -122,7 +132,7 @@ protected void onDraw(Canvas canvas) {
if (!isCoverSrc) {
float sx = 1.0f * (width - 2 * borderWidth - 2 * innerBorderWidth) / width;
float sy = 1.0f * (height - 2 * borderWidth - 2 * innerBorderWidth) / height;
// 缩小画布,使图片内容不被border、padding覆盖
// 缩小画布,使图片内容不被borders覆盖
canvas.scale(sx, sy, width / 2.0f, height / 2.0f);
}
super.onDraw(canvas);
Expand All @@ -137,7 +147,14 @@ protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setXfermode(xfermode);
canvas.drawPath(path, paint);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
canvas.drawPath(path, paint);
} else {
srcPath.addRect(srcRectF, Path.Direction.CCW);
// 计算tempPath和path的差集
srcPath.op(path, Path.Op.DIFFERENCE);
canvas.drawPath(srcPath, paint);
}
paint.setXfermode(null);

// 绘制遮罩
Expand Down

0 comments on commit 7c34555

Please sign in to comment.