可以在android中调整画布大小吗?

时间:2011-07-20 21:43:43

标签: android

[机器人] 我有一个包含2个视图的LinearLayout,第一个是imageView,第二个是canvas。 如果我做mLinearLayout.addView(i);然后mLinearLayout.addView(c);它显示了两者,但如果我在反向操作(mLinearLayout.addView(c)然后mLinearLayout.addView(i)),它只显示画布。 我想在这两个视图之间共享屏幕。有人可以帮我吗?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLinearLayout = new LinearLayout(this);

    mLinearLayout.setBackgroundColor(0xff74AC23);
    ImageView i = new ImageView(this);
    View c = new DrawView(this);

    i.setImageResource(R.drawable.bol_groen);
    i.setAdjustViewBounds(true); 

    mLinearLayout.addView(i);
    mLinearLayout.addView(c);
    setContentView(mLinearLayout);

}

}

公共类DrawView扩展了View {

private ShapeDrawable mDrawable;

public DrawView(Context context) {
    super(context);
    setFocusable(true); //necessary for getting the touch events

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

}

@Override protected void onDraw(Canvas canvas) {

    canvas.drawColor(0xFFCCCCCC);
    mDrawable.draw(canvas);

}

1 个答案:

答案 0 :(得分:0)

尝试以下内容。

将你的绘制语句从ondraw中取出,然后调用DrawView或某种形状或者任何形状。

//some shape// 

此外,我不会看到你的X和Y,或身高或宽度。

public DrawView(Context context) {     
    super(context);
    setFocusable (true); //necessary for getting the touch events
     mDrawable = new ShapeDrawable(new OvalShape());
     mDrawable.getPaint().setColor(0xff74AC23);

     mDrawable.setBounds(x, y, x + width, y + height);  
     mDrawable.draw(canvas); 
}