我需要在android中绘制一个简单的形状
形状需要呈圆形,如ImageView可绘制,创建一个 ShapeDrawable使用画布
我尝试了以下但不能正常工作
public class MyButton extends ImageButton {
private Drawable iconDrawable;
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setImageDrawable(getIconDrawable());
}
/**
* just draw a simple circle
*
* @return
*/
public Drawable getIconDrawable() {
Shape shape = new Shape() {
@Override public void draw(Canvas canvas, Paint paint) {
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(0, 0, 20, paint);
}
};
ShapeDrawable drawable = new ShapeDrawable(shape);
return drawable;
}
}
答案 0 :(得分:0)
下面的代码将完成这项工作。但是你可能无法在eclipse IDE预览中看到它。在设备/模拟器中运行它并检查结果
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>