2012-04-19 15 views
6

Mam klasy o nazwie SeatsPanel, gdzie rysować miejsc (za pomocą drawRect) w metodzie onDraw. Metoda onDraw używa Canvas jako parametru, ale jak ustawić rozmiar płótna? Powodem, dla którego zadaję to pytanie, jest to, że ta klasa jest nadmuchiwana w innej klasie. Wiem, że płótno ma domyślną wysokość i szerokość telefonu, ale muszę go zmniejszyć. Jak mogę to zrobić?Jak ustawić rozmiar płótna?

Każda pomoc zostanie doceniona.

-Tolga-

+0

Co to znaczy, chcesz zrobić płótno mniejsze? Zwykle rozmiar płótna jest zarządzany przez Androida i zależy od zmierzonego rozmiaru widoku. – Greg

+0

OK, ale w jaki sposób mogę zmienić rozmiar widoku? Jestem zdezorientowany, chcę tylko zmienić rozmiar tej klasy. – Xarialon

+0

dlaczego musisz go nadmuchać? Dowolny powód – UVM

Odpowiedz

3

Próbowałam zaimplementuj prostą aplikację, która rysuje czarny rect w głównym działaniu, czyli narysowanym przez naciśnięcie przycisku. Na przykład, w 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; 
      } 

     } 

    }); 

} 

A w klasie 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

Podoba mi się ten, ale niestety nie rozwiązuje problemu, który mam teraz. Muszę zmienić rozmiar płótna w metodzie onDraw. – Xarialon

+0

Wypróbuj tę nową metodę ... – Ant4res

+0

Gdy używam tej metody, ekran zmienia kolor na biały ...:/Nie widzę już zawartości metody onDraw. – Xarialon

1

Może nie mieć zastosowania w Twoim przypadku, ale to działa na mnie

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

Ustawia płótno do wielkości bitmapy

+0

To nie pomoże, muszę użyć lepszej metody rozwiązania problemu. – Xarialon

Powiązane problemy