0

我正在玩android编码,我创建了一个使用所有屏幕尺寸的画布,我需要一些帮助来在这个画布中添加一个触摸事件,每次有人触摸它时,画布会改变颜色。

编辑 2

我设法在我的布局中插入了一个点击事件,但我工作得不是很好,它只是第一次设法绘制。

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

1 回答 1

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

    }
于 2013-07-12T14:04:52.893 回答