2012-04-19 9 views
6

私はonDrawメソッドで(drawRectを使用して)シートを描画するSeatsPanelという名前のクラスを持っています。 onDrawメソッドはCanvasをパラメータとして使用しますが、どのようにCanvasのサイズを設定しますか?私がこの質問をしているのは、このクラスが別のクラスで膨らんでいるからです。私はキャンバスに電話のデフォルトの高さと幅があることを知っていますが、それを小さくする必要があります。これどうやってするの?キャンバスサイズを設定するには?

ご協力いただければ幸いです。

-Tolga-

+0

あなたが作りたいものを意味していますキャンバスは小さいですか?通常、キャンバスのサイズはAndroidによって管理され、測定されたビューのサイズに依存します。 – Greg

+0

しかし、ビューのサイズを変更するにはどうすればよいですか?私は混乱している、私はちょうどこのクラスのサイズを変更したい。 – Xarialon

+0

なぜそれを膨らませる必要がありますか?何らかの理由で – UVM

答えて

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

これが私の作品メインアクティビティ内に黒い矩形を描画する単純なアプリケーションを実装します。これはボタンを押すことによって描画されます。例えば、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

+0

この新しいメソッドを試してみてください... – Ant4res

+0

このメソッドを使用すると、画面が白くなります。:/もうonDrawメソッドの内容は表示されません。 – Xarialon

1

は、あなたの場合には適用されないかもしれないが、これは私がしようとしたビットマップ

+0

これは役に立ちませんが、私は問題を解決するためのより良い方法を使用する必要があります。 – Xarialon

関連する問題