Java – how to set the canvas size?

I have a class called seatspanel. I draw seats in the OnDraw method (using drawRect) The OnDraw method uses canvas as a parameter, but how to set the size of canvas? The reason I ask this question is because this class is exaggerated in another class I know the canvas has the default height and width of the phone, but I need to reduce it How can I do this?

Any help will be greatly appreciated

-Tolga-

Solution

I tried to implement a simple application, draw a black rectangle in the main activity and press the button For example, in 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;
             }

        }

    });

}

In the yourview class:

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);
    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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>