当前位置: 动力学知识库 > 问答 > 编程问答 >

drawable - I need to draw a simple shape in android

问题描述:

I need to draw a simple shape in android

The shape needs to be in circle shape as ImageView drawable, create a

ShapeDrawable use canvas

I tried the following but not working

 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;

}

}

网友答案:

below code will do the job. But you may fail to see it in the eclipse IDE preview. Run it in device/emulator and check the result

 <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="0dp" 
        android:bottomRightRadius="0dp"/>
分享给朋友:
您可能感兴趣的文章:
随机阅读: