更改触摸画布颜色 [英] Change canvas color on touch

查看:142
本文介绍了更改触摸画布颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打了Android编码和我创建了使用所有屏幕尺寸的画布,我需要一些帮助在此画布上添加一个触摸事件为每个人接触它在画布上改变颜色的时间。

编辑2

我设法在我的布局中插入一个click事件,但我没有很好地工作,它唯一的管理作画的第一次。

 公共类Modetwo_paint扩展活动实现OnClickListener {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        ...
        LL =(的LinearLayout)findViewById(R.id.layout_paint);
        ll.setBackgroundColor(Color.BLACK);        ll.setOnClickListener(本);
        drawView =新DrawViewSingle(在此,宽度,高度,一个[0],R [0],G [0],B [0]); // A,R,G,B,它们是为int阵列
        ll.addView(drawView);} 公共无效的onClick(查看为arg0){
        字符串s;
        开关(arg0.getId()){
        案例R.id.layout_paint:
            点击+ = 1;
            如果(点击> =则为a.length){
                点击= 0;
            }
            S =阿泰斯特+点击;
            Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_SHORT).show();            drawView =新DrawViewSingle(这一点,宽度,高度,[点击],R [点击],G [点击],B [点击]);
            ll.addView(drawView);
            打破;
        默认:
            打破;
        }    }
}

我DrawViewSingle类

 公共类DrawViewSingle扩展视图{
    涂料粉刷=新的油漆();
    INT X;
    诠释Ÿ;
    int类型的;
    INT R;
    INT克;
    INT B:
    INT大小;
    公共DrawViewSingle(上下文的背景下,诠释的x,INT Y,int类型的,整数读,诠释克,INT B){
        超级(上下文);
        this.x = X;
        this.y = Y;
        this.a =一;
        this.r = R;
        this.g =克;
        this.b = B;
    }    @覆盖
    公共无效的onDraw(帆布油画){
        paint.setStrokeWidth(0);
        paint.setARGB(A,R,G,B);
        canvas.drawRect(0,0,X,Y,油漆);
    }
}


解决方案

所以,我的朋友告诉我,一旦我的画布使用屏幕大小来改变的LinearLayout的背景。现在,我不需要drawview类了

 公共无效的onClick(查看为arg0){
        开关(arg0.getId()){
        案例R.id.layout_paint:
            点击+ = 1;
            如果(点击> =则为a.length){
                点击= 0;
            }
            ll.setBackgroundColor(Color.argb(A [点击],R [点击],G [点击],B [点击]));            打破;
        默认:
            打破;
        }    }

I'm playing with android coding and I have created a canvas that use all the screen size and I need some help to add a touch event in this canvas for every time that someone touches it the canvas change the color.

EDIT 2

I managed to insert a click event in my layout, but i didn't work very well, its only managed to paint the first time.

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

    }
}

My DrawViewSingle Class

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

解决方案

So, my friend told me to change the LinearLayout background once my canvas use the screen size. Now i don´t need the drawview class anymore

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

    }

这篇关于更改触摸画布颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆