如何创建虚线圆角ImageView?

时间:2017-11-13 14:15:14

标签: android

我想创建虚线圆ImageView。我想我应该使用Paint,但它不起作用。

private Paint dashPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
{
    dashPaint.setStyle(Paint.Style.STROKE);
    dashPaint.setColor(Color.WHITE);
    dashPaint.setAlpha(120);
    dashPaint.setPathEffect(new DashPathEffect(new float[]{
            5, 5
    }, 0.0f));
    dashPaint.setStrokeWidth(Easify.dp(1.2f));
} 

2 个答案:

答案 0 :(得分:1)

公共类DottedImageView扩展了ImageView {

public DottedImageView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}


private Paint dashPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
{
    dashPaint.setStyle(Paint.Style.STROKE);
    dashPaint.setColor(Color.WHITE);
    dashPaint.setAlpha(120);
    dashPaint.setPathEffect(new DashPathEffect(new float[]{15, 15
    }, 0f));
    dashPaint.setStrokeWidth(3);
}


@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    int rad=Math.min(canvas.getWidth(), canvas.getHeight())/2;
    rad-=dashPaint.getStrokeWidth();
    dashPaint.setColor(Color.WHITE);
    canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2,rad, dashPaint);
}

}

答案 1 :(得分:0)

您可以将此背景资源设置为ImageView:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#ffffff" />
            <stroke
                android:dashGap="10dp"
                android:dashWidth="10dp"
                android:width="1dp"
                android:color="#979797" />
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
           <corners
            android:bottomRightRadius="6dp"
            android:bottomLeftRadius="6dp"
            android:topLeftRadius="6dp"
            android:topRightRadius="6dp"/>
        </shape>
    </item>
</layer-list>