我正在尝试使用Android中的Canavs
在另一个分段圈内设置分段圈。我有点使用draw.Arc()
,但我需要使用较低的API。我使用ArchShape
创建一个弧,大部分都可以工作,但是当在第二个圆上设置填充时,填充不会生效。
我尝试在第二个重新调整大小时填充填充,并且填充不起作用。但你可以看到两者都被绘制出来。
这是我在重新调整大小时使用偏移填充的代码。
Bitmap b;
Canvas c;
@Override
protected void onStart() {
super.onStart();
b = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
c = new Canvas(b);
Paint p1 = new Paint();
Paint p2 = new Paint();
p1.setFlags(Paint.ANTI_ALIAS_FLAG);
p2.setFlags(Paint.ANTI_ALIAS_FLAG);
p1.setColor(Color.BLACK);
p2.setColor(Color.RED);
SegmentCircle(0, p1, p2);
p1.setColor(Color.GREEN);
p2.setColor(Color.BLUE);
SegmentCircle(50, p1, p2);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(b);
}
private final static int SIZE = 500;
private void SegmentCircle(int padding, Paint p1, Paint p2){
for (int i = 0; i < 20; i++) {
if ((i % 2) == 0){
ArcShape shape = new ArcShape(i * 18, 18);
ShapeDrawable d = new ShapeDrawable(shape);
shape.resize(SIZE - (padding / 2), SIZE - (padding / 2));
d.setPadding(padding,padding,padding,padding);
shape.draw(c, p1);
}else{
ArcShape shape = new ArcShape(i * 18, 18);
ShapeDrawable d = new ShapeDrawable(shape);
shape.resize(SIZE - (padding / 2), SIZE - (padding / 2));
d.setPadding(padding, padding, padding, padding);
shape.draw(c, p2);
}
}
}
我认为这可能与我绘制形状的事实有关,而不是ShapeDrawable
,但ShapeDrawable.Draw()
在参数中不接受Paint,所以我认为形状会引用ShapeDrawable
。
答案 0 :(得分:0)
我设法修复它,我不需要ShapeDrawable
或填充。我使用translate来偏移画布位置。
Bitmap b;
Canvas c;
@Override
protected void onStart() {
super.onStart();
b = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
c = new Canvas(b);
Paint p1 = new Paint();
Paint p2 = new Paint();
p1.setFlags(Paint.ANTI_ALIAS_FLAG);
p2.setFlags(Paint.ANTI_ALIAS_FLAG);
p1.setColor(Color.BLACK);
p2.setColor(Color.RED);
SegmentCircle(0, p1, p2);
p1.setColor(Color.GREEN);
p2.setColor(Color.BLUE);
SegmentCircle(50, p1, p2);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(b);
}
private final static int SIZE = 500;
private void SegmentCircle(int padding, Paint p1, Paint p2){
for (int i = 0; i < 20; i++) {
ArcShape shape = new ArcShape(i * 18, 18);
shape.resize(SIZE - padding, SIZE - padding );
c.translate(padding / 2, padding / 2);
if ((i % 2) == 0){
shape.draw(c, p1);
}else{
shape.draw(c, p2);
}
c.translate(-(padding / 2), -(padding / 2));
}
}