Custom View not drawing properly

心已入冬 提交于 2020-01-17 02:51:06

问题


I am making an Android game which has a board with some squares (similar to a chess board). Each square has some colors and I made a custom View class in which I overrided the onDraw() method in order to draw the square in the way I want to. Each square is composed of four triangles, as you can see in the next picture, in which a board with 3x3 squares is shown: https://dl.dropboxusercontent.com/u/48529299/Impossible%20Puzzle/ic_launcher.png Each triangle can be of one of three colors: purple, pink or yellow. My onDraw() method draws each of the triangles and the issue is that sometimes I see white or black triangles. That should be impossible as my custom View class only can draw with the three colors I mentioned earlier. It has an static array called availableColors which I am sure does not contain either black or white color, so if this happens is because I am doing something bad on the onDraw() method. Can you tell me what am I doing wrong? Here is my custom View subclass:

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

来源:https://stackoverflow.com/questions/30097173/custom-view-not-drawing-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!