【问题标题】:Is it possible to resize the canvas in android?是否可以在android中调整画布的大小?
【发布时间】:2012-10-11 02:43:33
【问题描述】:

[机器人] 我有一个包含 2 个视图的 LinearLayout,第一个是 imageView,第二个是画布。 如果我做 mLinearLayout.addView(i);然后是 MLinearLayout.addView(c);它显示了两者,但如果我以相反的方式进行(mLinearLayout.addView(c)然后是mLLinearLayout.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 扩展视图 {

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);

}

【问题讨论】:

  • 你的意思是它只显示画布? DrawView 占据了整个屏幕,还是大小合适但 ImageView 不显示?还有你不在 xml 中这样做的原因吗?
  • 请看这篇文章;希望对你有所帮助-stackoverflow.com/questions/11071258/…

标签: android


【解决方案1】:

试试下面的。

从 ondraw 中取出你的 draw 语句,然后调用 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); 
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 2016-12-02
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多