android 画圆圈背景,android – 在视图的背景画一个半圆

你可以实现你自己的Drawable.但是,这不能从XML中膨胀.您需要使用View.setBackgroundDrawable()从代码设置drawable.

看到我的示例实现使用drawable绘制一个半圆.

public class SemiCircleDrawable extends Drawable {

private Paint paint;

private RectF rectF;

private int color;

private Direction angle;

public enum Direction

{

LEFT,

RIGHT,

TOP,

BOTTOM

}

public SemiCircleDrawable() {

this(Color.BLUE, Direction.LEFT);

}

public SemiCircleDrawable(int color, Direction angle) {

this.color = color;

this.angle = angle;

paint = new Paint();

paint.setColor(color);

paint.setStyle(Style.FILL);

rectF = new RectF();

}

public int getColor() {

return color;

}

/**

* A 32bit color not a color resources.

* @param color

*/

public void setColor(int color) {

this.color = color;

paint.setColor(color);

}

@Override

public void draw(Canvas canvas) {

canvas.save();

Rect bounds = getBounds();

if(angle == Direction.LEFT || angle == Direction.RIGHT)

{

canvas.scale(2, 1);

if(angle == Direction.RIGHT)

{

canvas.translate(-(bounds.right / 2), 0);

}

}

else

{

canvas.scale(1, 2);

if(angle == Direction.BOTTOM)

{

canvas.translate(0, -(bounds.bottom / 2));

}

}

rectF.set(bounds);

if(angle == Direction.LEFT)

canvas.drawArc(rectF, 90, 180, true, paint);

else if(angle == Direction.TOP)

canvas.drawArc(rectF, -180, 180, true, paint);

else if(angle == Direction.RIGHT)

canvas.drawArc(rectF, 270, 180, true, paint);

else if(angle == Direction.BOTTOM)

canvas.drawArc(rectF, 0, 180, true, paint);

}

@Override

public void setAlpha(int alpha) {

// Has no effect

}

@Override

public void setColorFilter(ColorFilter cf) {

// Has no effect

}

@Override

public int getOpacity() {

// Not Implemented

return 0;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值