2012-02-29 67 views
0

我需要在畫布上畫一些東西,然後舔上每個。我做了一個類android畫布點擊失敗T_T

public class PlayGameView extends SurfaceView implements SurfaceHolder.Callback 

在這個類我的onDraw方法

@Override 
public void onDraw(Canvas canvas) {  
     Paint paint = new Paint(); 
     Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench); 
     canvas.drawColor(Color .BLACK); 
     for(int i = 0; i < 4; i++) { 
      for(int j = 0; j < 4; j++) { 
       canvas.drawBitmap(wrench, canvas.getWidth()/2 - wrench.getWidth()*2 + i*wrench.getWidth(), 0 + j*wrench.getHeight(), null); 
      } 
     } 

} 

和內Thread類。 所以,在XML文件中添加此視圖這樣

<com.cerbertek.PlayGameView 
    android:id="@+id/play_field_surface_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

,並綁在活動課

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.play_game); 

    playField = (PlayGameView) findViewById(R.id.play_field_surface_view); 
    playField.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
       Toast.makeText(PlayGameActivity.this, "ОХУЕТЬ", 2000);   
     } 
    }); 
} 

但沒有任何反應添加clicklistener。沒有烤麪包。當我打電話給getWidth方法時,它向我展示了ZERO!但我看到圖像)

我該如何解決我的問題?

回答

0

你忘了show它:

Toast.makeText(PlayGameActivity.this, "ОХУЕТЬ", 2000).show(); 
+0

OO該死的,你是對的!謝謝)) – user1128677 2012-02-29 16:31:20