【问题标题】:How to set canvas size?如何设置画布大小?
【发布时间】:2012-04-19 13:27:06
【问题描述】:

我有一个名为 SeatsPanel 的类,我在 onDraw 方法中绘制座位(使用 drawRect)。 onDraw 方法使用 Canvas 作为参数,但是如何设置 Canvas 的大小呢?我问这个问题的原因是因为这个类在另一个类中被夸大了。我知道画布具有手机的默认高度和宽度,但我需要将其变小。我该怎么做?

任何帮助将不胜感激。

【问题讨论】:

  • 你想让画布变小是什么意思?通常画布大小由 Android 管理,并取决于 View 的测量大小。
  • 好的,但是我该如何改变视图的大小呢?我很困惑,我只想改变这个类的大小。
  • 为什么需要充气?任何原因
  • 因为我有另一个类,它扩展了 Activity 而这个扩展了 View 以在屏幕上绘图。

标签: java android android-canvas ondraw


【解决方案1】:

我尝试实现一个简单的应用程序,它在主要活动中绘制一个黑色矩形,即按下按钮绘制。比如在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);
}

main.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 方法中更改画布大小。
  • 当我使用这个方法时,屏幕变白了... :/ 我再也看不到onDraw方法的内容了。
  • 我试图实现一个小应用程序,它按下按钮,在 mainActivity 中的按钮下的 linearLayout 中绘制一个矩形。它对我有用...
  • 您能否也发布 main.xml 文件,以便我测试您的方法。谢谢
  • 我已经编辑了源代码。此应用程序从活动中更改画布大小,而不是从 onDraw...
【解决方案2】:

可能不适用于您的情况,但这对我有用

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

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

【讨论】:

  • 这没有用,我需要用更好的方法来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2021-12-21
  • 2016-10-27
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多