Skip to content

Commit

Permalink
fix: npe in setStroke
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinliu committed Oct 14, 2021
1 parent 6f5ffba commit 6e3b824
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
31 changes: 11 additions & 20 deletions app/src/main/res/layout/activity_setting.xml
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<cn.gavinliu.android.lib.shapedimageview.ShapedImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/an"
app:shape_mode="circle"
app:stroke_color="#009688"
app:stroke_width="3dp"/>

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="300dp"
android:layout_height="300dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stroke"
android:text="stroke"/>
android:text="stroke" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="strokeWidth"
android:text="stroke_width"/>
android:text="stroke_width" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="strokeColor"
android:text="stroke_color"/>
android:text="stroke_color" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="round"
android:text="round"/>
android:text="round" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="circle"
android:text="circle"/>
android:text="circle" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radius"
android:text="Radius"/>
android:text="Radius" />

</LinearLayout>

0 comments on commit 6e3b824

Please sign in to comment.