From 6e3b824aef1c6db08f7cd608232429b1b66b0416 Mon Sep 17 00:00:00 2001 From: Gavin Liu Date: Thu, 14 Oct 2021 16:20:27 +0800 Subject: [PATCH] fix: npe in setStroke --- .../lib/shapedimageview/ShapedImageView.java | 13 +++++--- .../shapedimageview/demo/SettingActivity.java | 13 +++++--- app/src/main/res/layout/activity_setting.xml | 31 +++++++------------ 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/ShapedImageView/src/main/java/cn/gavinliu/android/lib/shapedimageview/ShapedImageView.java b/ShapedImageView/src/main/java/cn/gavinliu/android/lib/shapedimageview/ShapedImageView.java index c004b06..afecbe7 100644 --- a/ShapedImageView/src/main/java/cn/gavinliu/android/lib/shapedimageview/ShapedImageView.java +++ b/ShapedImageView/src/main/java/cn/gavinliu/android/lib/shapedimageview/ShapedImageView.java @@ -231,11 +231,14 @@ public void setStroke(int strokeColor, float strokeWidth) { if (mStrokeWidth != strokeWidth) { mStrokeWidth = strokeWidth; - int width = getMeasuredWidth(); - int height = getMeasuredHeight(); - mStrokeShape.resize(width - mStrokeWidth * 2, height - mStrokeWidth * 2); - - postInvalidate(); + if (mStrokeShape == null) { + requestLayout(); + } else { + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + mStrokeShape.resize(width - mStrokeWidth * 2, height - mStrokeWidth * 2); + postInvalidate(); + } } if (mStrokeColor != strokeColor) { diff --git a/app/src/main/java/cn/gavinliu/android/lib/shapedimageview/demo/SettingActivity.java b/app/src/main/java/cn/gavinliu/android/lib/shapedimageview/demo/SettingActivity.java index a5b3cbd..649d0c4 100644 --- a/app/src/main/java/cn/gavinliu/android/lib/shapedimageview/demo/SettingActivity.java +++ b/app/src/main/java/cn/gavinliu/android/lib/shapedimageview/demo/SettingActivity.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; +import android.widget.FrameLayout; import cn.gavinliu.android.lib.shapedimageview.ShapedImageView; @@ -20,7 +21,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); - mShapedImageView = (ShapedImageView) findViewById(R.id.image1); + mShapedImageView = new ShapedImageView(this); + mShapedImageView.setImageResource(R.drawable.an); + mShapedImageView.setStroke(0xFF9999FF, 35); + + FrameLayout frameLayout = findViewById(R.id.frameLayout); + frameLayout.addView(mShapedImageView); } public void stroke(View view) { @@ -35,13 +41,12 @@ public void strokeColor(View view) { mShapedImageView.setStrokeColor(getResources().getColor(R.color.colorAccent)); } - public void round(View view) { - mShapedImageView.setShape(ShapedImageView.SHAPE_MODE_ROUND_RECT, 50,50); + mShapedImageView.setShape(ShapedImageView.SHAPE_MODE_ROUND_RECT, 50, 50); } public void circle(View view) { - mShapedImageView.setShape(ShapedImageView.SHAPE_MODE_CIRCLE, 50,50); + mShapedImageView.setShape(ShapedImageView.SHAPE_MODE_CIRCLE, 50, 50); } public void radius(View view) { diff --git a/app/src/main/res/layout/activity_setting.xml b/app/src/main/res/layout/activity_setting.xml index 41b7b88..495ab28 100644 --- a/app/src/main/res/layout/activity_setting.xml +++ b/app/src/main/res/layout/activity_setting.xml @@ -1,60 +1,51 @@ - - - +