【问题标题】:I need to draw a simple shape in android我需要在android中绘制一个简单的形状
【发布时间】:2014-11-05 12:43:31
【问题描述】:

我需要在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;
    }
}

【问题讨论】:

标签: android drawable shape


【解决方案1】:

下面的代码将完成这项工作。但是您可能无法在 Eclipse IDE 预览版中看到它。在设备/模拟器中运行并查看结果

 <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="0dp" 
        android:bottomRightRadius="0dp"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2017-02-03
    相关资源
    最近更新 更多