触摸时更改画布颜色

时间:2013-07-11 19:41:51

标签: android canvas touch

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

编辑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 个答案:

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

    }