如何设置画布大小?

10

我有一个名为SeatsPanel的类,我在onDraw方法中使用drawRect绘制座位。onDraw方法以Canvas作为参数,但您如何设置Canvas的大小?我之所以问这个问题是因为这个类正在另一个类中膨胀。我知道画布具有手机的默认高度和宽度,但我需要将其缩小。我该怎么做?

任何帮助都将不胜感激。


你是说你想要缩小画布?通常情况下,画布的大小由Android管理,并且取决于视图的测量大小。 - Greg
好的,但是我如何改变视图的大小呢?我有点困惑,我只想改变这个类的大小。 - Aerial
为什么需要对其进行膨胀?有什么原因吗? - UVM
因为我有另一个类,它扩展了Activity,而这个类则扩展了View以在屏幕上绘制。 - Aerial
2个回答

4

我尝试实现一个简单的应用程序,在主活动中绘制一个黑色矩形,通过按下按钮来实现。例如,在MainActivity中:

    private Button button1;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button1=(Button)findViewById(R.id.button);
    button1.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
             switch(v.getId()){
                case R.id.button:

                     LinearLayout ll=(LinearLayout)findViewById(R.id.linearLayout1);
                     System.out.println(ll.getWidth()+" "+ll.getHeight());
                     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ll.getWidth(),ll.getHeight());
                     YourView yourView = new YourView(getBaseContext());
                     yourView.setBackgroundColor(Color.WHITE);
                     ll.addView(yourView,params);
                    break;
             }

        }

    });

}

YourView类中:

    private Bitmap savedBitmap;
public YourView(Context context) {
    super(context);
}
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    System.out.println(canvas.getWidth()+" "+canvas.getHeight());

    Paint textPaint = new Paint();
    textPaint.setARGB(255, 0, 0, 0);
    textPaint.setTextAlign(Paint.Align.RIGHT);
    textPaint.setTextSize(11);
    textPaint.setTypeface(Typeface.DEFAULT);

    canvas.drawColor(Color.WHITE);
    System.out.println(canvas.getWidth());
    System.out.println(canvas.getHeight());

    canvas.drawRect(200, 20, 500, 100, textPaint);
}

主要的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Push the button and draw a Rect" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />




<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</LinearLayout>


我喜欢这个,但不幸的是它不能解决我现在遇到的问题。我需要在onDraw方法中更改画布大小。 - Aerial
当我使用这个方法时,屏幕变成了白色... :/ 我再也看不到onDraw方法的内容了。 - Aerial
我尝试实现一个小应用程序,按下按钮后,在主活动中的linearLayout下绘制一个矩形。对我来说它有效... - Ant4res
你能否也发布main.xml文件,这样我就可以测试你的方法。谢谢。 - Aerial
我已经编辑了源代码。这个应用程序从活动中更改画布大小,而不是从onDraw中。 - Ant4res
显示剩余2条评论

3
可能对你来说不适用,但对我有效。
Bitmap animation = BitmapFactory.decodeResource(mContext.getResources(), resourceId, mBitmapOptions); //Get a bitmap from a image file

// Create a bitmap for the part of the screen that needs updating.
Bitmap bitmap = Bitmap.createBitmap(animation.getWidth(), animation.getHeight(), BITMAP_CONFIG);
bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
Canvas canvas = new Canvas(bitmap);

这将把画布的大小设置为位图的大小


这样做没有帮助,我需要使用更好的方法来解决问题。 - Aerial

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