2012-04-19 155 views
6

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

任何帮助,将不胜感激。

-Tolga-

+0

你的意思是你想画布更小?通常,画布大小由Android管理,并且取决于视图的测量大小。 – Greg 2012-04-19 13:32:25

+0

好的,但是如何改变视图的大小呢?我很困惑,我只想改变这个班级的规模。 – Xarialon 2012-04-19 13:36:24

+0

你为什么需要膨胀它?任何原因 – UVM 2012-04-19 13:46:26

回答

3

我试图位图的大小实现一个简单的应用程序,在主要活动中绘制一个黑色矩形,即按下按钮。例如,在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> 

+0

我喜欢这个,但遗憾的是它并不能解决我现在遇到的问题。我需要在onDraw方法中更改画布大小。 – Xarialon 2012-04-19 18:45:51

+0

试试这个新方法... – Ant4res 2012-04-19 19:17:08

+0

当我使用这种方法时,屏幕变成白色......:/我看不到onDraw方法的内容了。 – Xarialon 2012-04-19 19:43:27

1

可能不适用你的情况,但是这对我的作品

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

此设置画布

+0

这不会帮助,我需要使用更好的方法来解决问题。 – Xarialon 2012-04-19 14:21:03