Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中更改画布上的特定矩形颜色?_Java_Android_Colors_Grid_Android Canvas - Fatal编程技术网

Java 如何在android中更改画布上的特定矩形颜色?

Java 如何在android中更改画布上的特定矩形颜色?,java,android,colors,grid,android-canvas,Java,Android,Colors,Grid,Android Canvas,当我触摸屏幕时,我一直在尝试更改一系列矩形中特定矩形的颜色,但它似乎不起作用,以下是我的代码: public Paint blue = new Paint(); RandomColorGen rc; ArrayList<Integer> colors = RandomColorGen.ColorList(5); Random rand = new Random(); int columns = 50; int rows = 50; Rect square[][] = new Rect

当我触摸屏幕时,我一直在尝试更改一系列矩形中特定矩形的颜色,但它似乎不起作用,以下是我的代码:

public Paint blue = new Paint();
RandomColorGen rc;
ArrayList<Integer> colors = RandomColorGen.ColorList(5);
Random rand = new Random();
int columns = 50;
int rows = 50;
Rect square[][] = new Rect[rows][columns];

public boolean isTouched;
public Canvas canvas;

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    this.canvas = canvas;

    for (int x = 0; x < rows; x++) {
        for (int y = 0; y < columns; y++) {

            square[x][y] = new Rect();

            blue.setColor(colors.get(rand.nextInt(colors.size())));
            blue.setStyle(Paint.Style.FILL);

            square[x][y].set(0, 0, (canvas.getWidth() - 10) / rows,
                    ((canvas.getHeight() - 100) / columns));
            square[x][y].offsetTo(x * ((canvas.getWidth() - 10) / rows), y
                    * ((canvas.getHeight() - 100) / columns));

            canvas.drawRect(square[x][y], blue);


        }
    }
    if(isTouched){
        blue.setColor(colors.get(rand.nextInt(colors.size())));
        blue.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.clipRect(square[1][1]);
        canvas.drawRect(square[1][1], blue);

        canvas.restore();

    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        isTouched = true;


        break;
    }

    return true;

}
public Paint blue=new Paint();
随机染色剂;
ArrayList colors=RandomColorGen.ColorList(5);
Random rand=新的Random();
int列=50;
int行=50;
矩形[][]=新矩形[行][列];
公共布尔值被触发;
公共画布;
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
this.canvas=画布;
对于(int x=0;x

colors.get()thingy是一个颜色数组列表。我是否采取了错误的方法?

在执行onTouch操作后,如何调用绘制函数


OnTouch操作是第一个操作,那么如何调用Paint()函数呢?

我刚刚测试了您的代码,它可以工作,但有几个值得一提的注意事项:

  • 在绘图期间分配对象是一种非常糟糕的行为(特别是在分配50*50时)!考虑将分配代码移到构造函数,并在“代码”> OnDRAW()/<代码>方法中更改矩形的位置,如果您想要实现与现在相同的行为。
  • 您对
    onTouchEvent()
    的使用不完整,只要用户没有举手,您就需要将isTouched设置为true,具体操作如下:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        isTouched = true;
    
    
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        isTouched = false;
        break;
    
    }
    invalidate();
    return true;
    
    }

  • 每次收到触摸事件时,也可以使用
    invalidate()


我尝试将其移动到构造函数中,我得到了一个nullpointerexception,其中显示为square[x][y]。设置(0,0,(canvas.getWidth()-10)/行,(canvas.getHeight()-100)/列);另外,我只想更改一个矩形的颜色,特别是方形[1][1]。