2013-07-11 140 views
0

我正在玩android編碼,並且我創建了一個使用所有屏幕大小的畫布,我需要一些幫助在此畫布上添加觸摸事件,以便每次有人觸摸畫布時更改顏色。更改畫布顏色觸摸

EDIT 2

我設法在我的佈局中插入一個click事件,但我沒有很好地工作,其只設法畫的第一次。

public class Modetwo_paint extends Activity implements OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     ll = (LinearLayout) findViewById(R.id.layout_paint); 
     ll.setBackgroundColor(Color.BLACK);   

     ll.setOnClickListener(this);      
     drawView = new DrawViewSingle(this ,width, height, a[0], r[0], g[0], b[0]); //a, r, g, b, they are arrays of int 
     ll.addView(drawView);     

} 

public void onClick(View arg0) { 
     String s; 
     switch (arg0.getId()) { 
     case R.id.layout_paint: 
      clicks+=1; 
      if (clicks >= a.length) { 
       clicks = 0; 
      } 
      s = "Teste " + clicks; 
      Toast.makeText(getApplicationContext(), s , Toast.LENGTH_SHORT).show(); 

      drawView = new DrawViewSingle(this ,width, height, a[clicks], r[clicks], g[clicks], b[clicks]); 
      ll.addView(drawView); 
      break; 
     default: 
      break; 
     } 

    } 
} 

我DrawViewSingle類

public class DrawViewSingle extends View { 
    Paint paint = new Paint(); 
    int x; 
    int y; 
    int a; 
    int r; 
    int g; 
    int b; 
    int size; 


    public DrawViewSingle(Context context, int x, int y, int a , int r, int g, int b) { 
     super(context);   
     this.x = x; 
     this.y = y; 
     this.a = a; 
     this.r = r; 
     this.g = g; 
     this.b = b; 
    } 

    @Override 
    public void onDraw(Canvas canvas) {   
     paint.setStrokeWidth(0);   
     paint.setARGB(a, r, g, b); 
     canvas.drawRect(0, 0, x, y, paint); 
    } 
} 
+1

請補充一點,你有沒有 –

+1

任何代碼的代碼,我不確定你試圖完成什麼,也許這將幫助:HTTP://開發商.android.com/guide/topics/graphics/2d-graphics.html – buczek

+0

嘿傢伙,我添加了一些代碼給我的問題,有人可以幫助我嗎? –

回答

0

所以,我的朋友告訴我,一旦我的畫布使用屏幕大小來改變的LinearLayout的背景。現在,我不需要的drawView函數類了

public void onClick(View arg0) { 
     switch (arg0.getId()) { 
     case R.id.layout_paint: 
      clicks+=1; 
      if (clicks >= a.length) { 
       clicks = 0; 
      }  
      ll.setBackgroundColor(Color.argb(a[clicks], r[clicks], g[clicks], b[clicks])); 

      break; 
     default: 
      break; 
     } 

    }