我正在制作一款Android游戏,其中有一些带有一些正方形的棋盘(类似于棋盘)。每个正方形都有一些颜色,我制作了一个自定义的View
类,我在其中覆盖了onDraw()
方法,以便按照我想要的方式绘制正方形。每个正方形由四个三角形组成,如下图所示,其中显示了一个3x3正方形的板:
https://dl.dropboxusercontent.com/u/48529299/Impossible%20Puzzle/ic_launcher.png
每个三角形可以是三种颜色中的一种:紫色,粉红色或黄色。
我的onDraw()
方法绘制了每个三角形,问题是我有时看到白色或黑色三角形。这应该是不可能的,因为我的自定义View
类只能用我前面提到的三种颜色绘制。它有一个名为availableColors
的静态数组,我确信它不包含黑色或白色,所以如果发生这种情况是因为我在onDraw()
方法上做了一些不好的事情。你能告诉我我做错了什么吗?
这是我的自定义View
子类:
package bembibre.impossiblepuzzle.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.FillType;
import android.graphics.drawable.Drawable;
import android.view.View;
import bembibre.impossiblepuzzle.R;
import bembibre.impossiblepuzzle.logic.models.Square;
public class ImpossiblePuzzleView extends View {
private Paint paint = new Paint();
private Path path = new Path();
private static int[] availableColors;
private Square square;
public ImpossiblePuzzleView(Context context) {
super(context);
this.setWillNotDraw(false);
if (ImpossiblePuzzleView.availableColors == null) {
ImpossiblePuzzleView.availableColors = context.getResources().getIntArray(R.array.available_colors);
}
this.paint.setFlags(Paint.ANTI_ALIAS_FLAG);
this.paint.setStyle(Paint.Style.FILL);
this.path.setFillType(FillType.WINDING);
}
/**
* Devuelve un color en formato #RRGGBB de entre los colores que están
* disponibles en el juego para pintar los cuadrados del tablero.
*
* @param index un número entero mayor que 0.
*
* @return un color en formato #RRGGBB.
*/
public int getColor(int index) {
int result;
index--;
if ((index > 0) && (index < ImpossiblePuzzleView.availableColors.length)) {
result = ImpossiblePuzzleView.availableColors[index];
} else {
result = ImpossiblePuzzleView.availableColors[0];
}
return result;
}
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
public void onDraw(Canvas canvas) {
if (this.square != null) {
switch(this.square.getType()) {
case NORMAL:
int full = this.getMeasuredWidth();
int half = full / 2;
int[] status = this.square.getStatus();
this.path.reset();
this.paint.setColor(this.getColor(status[0]));
this.path.moveTo(half, half);
this.path.lineTo(0, 0);
this.path.lineTo(0, full);
this.path.lineTo(half, half);
this.path.close();
canvas.drawPath(this.path, this.paint);
this.path.reset();
this.paint.setColor(this.getColor(status[1]));
this.path.moveTo(half, half);
this.path.lineTo(0, 0);
this.path.lineTo(full, 0);
this.path.lineTo(half, half);
this.path.close();
canvas.drawPath(this.path, this.paint);
this.path.reset();
this.paint.setColor(this.getColor(status[2]));
this.path.moveTo(half, half);
this.path.lineTo(full, 0);
this.path.lineTo(full, full);
this.path.lineTo(half, half);
this.path.close();
canvas.drawPath(this.path, this.paint);
this.path.reset();
this.paint.setColor(this.getColor(status[3]));
this.path.moveTo(half, half);
this.path.lineTo(full, full);
this.path.lineTo(0, full);
this.path.lineTo(half, half);
this.path.close();
canvas.drawPath(this.path, this.paint);
break;
case UNTOUCHABLE:
default:
Drawable background;
if (android.os.Build.VERSION.SDK_INT < 22) {
background = this.getContext().getResources().getDrawable(R.drawable.untouchable);
} else {
background = this.getContext().getResources().getDrawable(R.drawable.untouchable, null);
}
if (android.os.Build.VERSION.SDK_INT < 16) {
this.setBackgroundDrawable(background);
} else {
this.setBackground(background);
}
break;
}
}
}
/**
* Indica a esta vista cuál es el cuadrado que tiene que mostrar, es decir,
* qué es lo que tiene que pintar.
*
* @param square objeto que indica a esta vista cómo se tiene que pintar.
*/
public void setSquare(Square square) {
this.square = square;
}
}