如何在Android中使用XML布局进行绘图

3

我正在尝试学习开发者页面上给出的一个安卓示例。它提供了两种在画布上绘制的方法。第一种方法是使用一个叫做CustomDrawableView的类,代码如下:

public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;

public CustomDrawableView(Context context) {
super(context);

int x = 10;
int y = 10;
int width = 300;
int height = 50;

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}

protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}

接下来由主类调用:

CustomDrawableView mCustomDrawableView;
protected void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
mCustomDrawableView = new CustomDrawableView(this);        
setContentView(mCustomDrawableView);
}

但是教程指出:
如果你想从XML布局中绘制这个自定义的drawable而不是从Activity中绘制,那么CustomDrawable类必须重写View(Context,AttributeSet)构造函数,它在通过XML膨胀实例化视图时被调用。然后像这样向XML添加一个CustomDrawable元素:
<com.example.shapedrawable.CustomDrawableView
   android:layout_width="fill_parent"     
android:layout_height="wrap_content"     
/>

我已经成功运行了第一个示例,但如果我想采用第二个选项,我不知道我的代码应该是什么样子的。可能我要从@ Override View开始,但是我卡住了。

我想这样做的原因是因为我想创建一个只覆盖屏幕一部分的画布,这样我就可以在另一部分放置按钮和文本。

2个回答

4
如果这个教程是正确的,那么你只需要添加那个构造函数。你可能想把可绘制的初始化代码移到一个单独的方法中。
public CustomDrawableView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initDrawable();
}

XML(可扩展标记语言)
<com.example.shapedrawable.CustomDrawableView
   android:id="+id/custom_drawable_view"
   android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
/>

您可以这样获得视图。

CustomDrawableView mCustomDrawableView;

protected void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.main); // assuming your layout is named main.xml
    mCustomDrawableView = (CustomDrawableView) findViewById(R.id.custom_drawable_view);
}

谢谢Robby。我不想试探你的耐心,但现在我该如何调用这个活动呢?之前是这样的:mCustomDrawableView = new CustomDrawableView(this);setContentView(mCustomDrawableView); - Steblo

0

public CustomDrawableView(Context v, AttributeSet as){ super(v,as); drawShape(as); } public void drawShape(AttributeSet ast) {
int x =0; int y=0 ; int width=0; int height=0 ;

            x=Integer.parseInt(ast.getAttributeValue(null, "x"));       
    y=Integer.parseInt(ast.getAttributeValue(null, "y"));
    width=Integer.parseInt(ast.getAttributeValue(null, "width"));
    height= Integer.parseInt(ast.getAttributeValue(null, "height"));

    mDrawable = new ShapeDrawable(new OvalShape());
    mDrawable.getPaint().setColor(0xff74AC23);
    mDrawable.setBounds(x, y, x + width, y + height);

}

MainActivity.java{ CustomDrawableView mCustomDrawableView;

主活动.java{ CustomDrawableView mCustomDrawableView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }
public View.OnClickListener cc = (new View.OnClickListener() {
    public void onClick(View view) {
                    setContentView(R.id.myid);}
       });       

这个方法可以正常工作...(不确定这是否是最佳实践...)


<main.xml片段> <com.check.layoutcheck.CustomDrawableView android:id="@+id/myid" x='10' y='10' width='300' height='50'></com.check.layoutcheck.CustomDrawableView> </main.xml片段> - Sora

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接