香港專業教育學院一直在努力改變特定rects的顏色rects數組當我觸摸屏幕,但它似乎是工作,我的繼承人代碼:我如何更改Android中畫布上的特定矩形顏色?
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;
}
的colors.get()啄是顏色的arraylist。我採取了錯誤的方法?
我想它移動到構造器和即時得到一個NullPointerException那裏說平方[X] [Y] .SET(0,0,(canvas.getWidth() - 10)/行, \t \t \t \t \t \t((canvas.getHeight() - 100)/列)); – 2013-02-24 13:53:12
也,我只想改變一個rects顏色,特別是方形[1] [1]。 – 2013-02-24 14:03:32