Android 自定义视图未正确绘制

Android 自定义视图未正确绘制,android,canvas,view,path,ondraw,Android,Canvas,View,Path,Ondraw,我正在制作一个Android游戏,它有一个带有一些方块的棋盘(类似于一个国际象棋棋盘)。每个正方形都有一些颜色,我创建了一个自定义的视图类,在该类中我重写了onDraw()方法,以便以我想要的方式绘制正方形。每个正方形由四个三角形组成,如下图所示,其中显示了一个包含3x3个正方形的板: 每个三角形可以是三种颜色之一:紫色、粉色或黄色。 我的onDraw()方法绘制每个三角形,问题是有时我会看到白色或黑色三角形。这应该是不可能的,因为我的自定义视图类只能使用我前面提到的三种颜色进行绘制。它有一个名

我正在制作一个Android游戏,它有一个带有一些方块的棋盘(类似于一个国际象棋棋盘)。每个正方形都有一些颜色,我创建了一个自定义的
视图
类,在该类中我重写了
onDraw()
方法,以便以我想要的方式绘制正方形。每个正方形由四个三角形组成,如下图所示,其中显示了一个包含3x3个正方形的板: 每个三角形可以是三种颜色之一:紫色、粉色或黄色。 我的
onDraw()
方法绘制每个三角形,问题是有时我会看到白色或黑色三角形。这应该是不可能的,因为我的自定义
视图
类只能使用我前面提到的三种颜色进行绘制。它有一个名为
availableColors
的静态数组,我确信它既不包含黑色也不包含白色,因此如果发生这种情况,是因为我在
onDraw()方法上做了一些不好的事情。你能告诉我我做错了什么吗?
这是我的自定义
视图
子类:

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;
    }
}
包bembbre.impossiblepuzzle.ui;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.graphics.Path.FillType;
导入android.graphics.drawable.drawable;
导入android.view.view;
import bembbre.impossiblepuzzle.R;
导入bembibre.impossiblepuzzle.logic.models.Square;
公共类视图扩展视图{
私人油漆=新油漆();
私有路径路径=新路径();
专用静态int[]可用颜色;
私人广场;
公共视图(上下文){
超级(上下文);
此.setWillNotDraw(false);
if(ImpossiblePuzzleView.availableColors==null){
ImpossiblePuzzleView.availableColors=context.getResources().getIntArray(R.array.available_colors);
}
this.paint.setFlags(paint.ANTI_别名_标志);
this.paint.setStyle(paint.Style.FILL);
this.path.setFillType(FillType.WINDING);
}
/**
*颜色的格式是什么
*有争议的案件将被驳回。
* 
*@param index unúmero entero que 0。
* 
*@return un color en formato#RRGGBB。
*/
公共整数getColor(整数索引){
int结果;
索引--;
如果((索引>0)和&(索引<不可能的拼图视图.可用颜色.长度)){
结果=不可能的拼图视图。可用颜色[索引];
}否则{
结果=不可能的拼图视图。可用颜色[0];
}
返回结果;
}
@SuppressLint(“NewApi”)@SuppressWarnings(“弃用”)
@凌驾
公共空白onDraw(画布){
如果(this.square!=null){
开关(this.square.getType()){
正常情况:
int full=this.getMeasuredWidth();
int半=满/2;
int[]status=this.square.getStatus();
这个.path.reset();
this.paint.setColor(this.getColor(状态[0]);
这个.path.moveTo(一半,一半);
this.path.lineTo(0,0);
this.path.lineTo(0,完整);
这个.path.lineTo(一半,一半);
this.path.close();
canvas.drawPath(this.path,this.paint);
这个.path.reset();
this.paint.setColor(this.getColor(状态[1]);
这个.path.moveTo(一半,一半);
this.path.lineTo(0,0);
this.path.lineTo(完整,0);
这个.path.lineTo(一半,一半);
this.path.close();
canvas.drawPath(this.path,this.paint);
这个.path.reset();
this.paint.setColor(this.getColor(状态[2]);
这个.path.moveTo(一半,一半);
this.path.lineTo(完整,0);
this.path.lineTo(full,full);
这个.path.lineTo(一半,一半);
this.path.close();
canvas.drawPath(this.path,this.paint);
这个.path.reset();
this.paint.setColor(this.getColor(状态[3]);
这个.path.moveTo(一半,一半);
this.path.lineTo(full,full);
this.path.lineTo(0,完整);
这个.path.lineTo(一半,一半);
this.path.close();
canvas.drawPath(this.path,this.paint);
打破
不可触及的情况:
违约:
可绘制背景;
if(android.os.Build.VERSION.SDK_INT<22){
后台=this.getContext().getResources().getDrawable(R.drawable.untouchable);
}否则{
后台=this.getContext().getResources().getDrawable(R.drawable.Untoucable,null);
}
if(android.os.Build.VERSION.SDK_INT<16){
本图为背景图;
}否则{
这是挫折(背景);
}
打破
}
}
}
/**
*这是一个很好的前景,因为它是一个很好的选择,
*这是一个很好的例子。
* 
*@param square objeto que indica esta vista cómo se tiene que pintar。
*/
公共空隙固定方格(方形){
这个正方形=正方形;
}
}

我可能错了,但我建议您稍微修改一下代码,它可能会解决您的问题。通过添加4个顶点来构建三角形,可以重叠点,因此调用close而不添加最后一个顶点(与第一个顶点冗余)。并始终使用相同的旋转顺序,例如顺时针。请注意,您绘制的第一个三角形是逆时针方向的。您确定所有颜色都是颜色而不是黑色或alpha吗?我将删除每个三角形的最后一个垂直