用不同的边框颜色Android绘制圆圈

前端之家收集整理的这篇文章主要介绍了用不同的边框颜色Android绘制圆圈前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public static Bitmap drawCircle(int width,int height,int borderWidth) {
    Bitmap canvasBitmap = Bitmap.createBitmap( width,height,Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(canvasBitmap,TileMode.CLAMP,TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
    paint.setShader(null);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(borderWidth);  
    Canvas canvas = new Canvas(canvasBitmap);
    float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
    canvas.drawCircle(width / 2,height / 2,radius - borderWidth / 2,paint);
    return canvasBitmap;
}

简单的这个代码绘制一个带有白色边框的圆圈,但是我希望边框的一部分是黑色而另一部分是白色的. 40%的黑色,60%的白色

如何才能做到这一点?

解决方法

试试这个代码
class MyView extends View
{
    private Paint paint;

    public MyView(Context context,int x,int y)
    {
        super(context);
        paint = new Paint();
        // PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);


        paint.setAlpha(255);
        // paint.setXfermode(xfermode);
        paint.setAntiAlias(true);
        // setBackgroundColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawCircle(100,100,50,paint);
    }
}

猜你在找的Android相关文章