9

I have a class named SeatsPanel where I draw seats (using drawRect) in the onDraw method. The onDraw method uses Canvas as a parameter, but how do you set size of the Canvas? The reason why I'm asking this question is because this class is being inflated in another class. I know that the canvas has the default height and width of the phone, but I need to make it smaller. How can I do this?

Any help would be appreciated.

4

2 回答 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);
}

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>

于 2012-04-19T15:03:05.890 回答
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);

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

于 2012-04-19T13:42:01.703 回答