要自定義ShapeDrawable的形狀,您可以通過繼承Shape類并實(shí)現(xiàn)自己的形狀來實(shí)現(xiàn)。以下是一個(gè)示例代碼,展示如何創(chuàng)建一個(gè)自定義的圓形ShapeDrawable:
public class CustomShape extends Shape {
private float radius;
public CustomShape(float radius) {
this.radius = radius;
}
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawCircle(radius, radius, radius, paint);
}
}
public class CustomShapeDrawable extends ShapeDrawable {
public CustomShapeDrawable(float radius) {
super(new CustomShape(radius));
}
}
// 在您的代碼中使用自定義的ShapeDrawable
CustomShapeDrawable customShapeDrawable = new CustomShapeDrawable(50);
customShapeDrawable.getPaint().setColor(Color.RED);
imageView.setBackground(customShapeDrawable);
在這個(gè)示例中,我們創(chuàng)建了一個(gè)CustomShape類來定義一個(gè)圓形形狀,并在CustomShapeDrawable類中使用這個(gè)自定義形狀來創(chuàng)建一個(gè)ShapeDrawable對(duì)象。最后,我們可以將這個(gè)自定義的ShapeDrawable對(duì)象設(shè)置為ImageView的背景。您可以根據(jù)需要實(shí)現(xiàn)不同的自定義形狀來創(chuàng)建不同的ShapeDrawable。