Android的PixelGrid OnDraw的检查/跟踪当前位置 [英] Android PixelGrid OnDraw Check/Track Current Position

查看:122
本文介绍了Android的PixelGrid OnDraw的检查/跟踪当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前按照此回答。从这个答案,它表明,如果它已经触及像素/电网将被检查。不过,我想检查网格为起点,并有控制/按钮在MainActivity来移动起点。例如,如果用户点击了前进按钮时,开始点将向前移动,检查新格和设置previous点,为白色。现在我于checkedCell [专栏] [行]在moveFor​​ward()得到的NullPointerException方法

所以,这里是我的code:

 公共类PixelGridView扩展视图{
私人诠释为numColumns,其行;
私人诠释cellWidth,cellHeight;
私人油漆blackPaint =新的油漆();
私人布尔[] [] cellChecked;公共PixelGridView(上下文的背景下)
{
    这(背景下,NULL);
}公共PixelGridView(上下文的背景下,ATTRS的AttributeSet)
{
    超(背景下,ATTRS);
    blackPaint.setStyle(Paint.Style.FILL_AND_STROKE);
}公共无效setNumColumns(INT为numColumns)
{
    this.numColumns =为numColumns;
    calculateDimensions();
}公众诠释getNumColumns()
{
    numColumns列返回;
}公共无效setNumRows(INT其行)
{
    this.numRows =其行;
    calculateDimensions();
}公众诠释getNumRows()
{
    返回其行;
}@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh)
{
    super.onSizeChanged(W,H,oldw,oldh);
    calculateDimensions();
}私人无效calculateDimensions()
{
    如果(为numColumns == 0 ||其行== 0)
        返回;    cellWidth =的getWidth()/为numColumns;
    cellHeight =的getHeight()/其行;    cellChecked =新的布尔[为numColumns] [其行]    无效();
}@覆盖
保护无效的onDraw(帆布油画)
{
    super.onDraw(画布);    canvas.drawColor(Color.WHITE);    如果(为numColumns == 0 ||其行== 0)
        返回;    INT宽度=的getWidth();
    INT高度=的getHeight();    的for(int i = 0; I<为numColumns;我++)
    {
        对于(INT J = 0; J<其行; J ++)
        {
            如果(cellChecked [I] [J]。)
            {
                canvas.drawRect(ⅰ* cellWidth,J * cellHeight,第(i + 1)* cellWidth,第(j + 1)* cellHeight,blackPaint);
            }
        }
    }    的for(int i = 1; I<为numColumns;我++)
    {
        canvas.drawLine(我* cellWidth,0,I * cellWidth,身高,blackPaint);
    }    的for(int i = 1; I<其行;我++)
    {
        canvas.drawLine(0,I * cellHeight,宽,我* cellHeight,blackPaint);
    }
}@覆盖
公共布尔onTouchEvent(MotionEvent事件)
{
    如果(event.getAction()!= MotionEvent.ACTION_DOWN)
        返回true;    INT列=(INT)(event.getX()/ cellWidth);
    INT行=(INT)(event.getY()/ cellHeight);    !cellChecked [专栏] [行] = cellChecked [专栏] [行]
    无效();    返回true;
}
公共无效moveFor​​ward(){
//设置列和行以对mainactivity [1] [1]在点击按钮进行检查
    INT列= 1;
    INT行= 1;
    !cellChecked [专栏] [行] = cellChecked [专栏] [行]
    无效();
}
}

和这里是我的活动调用moveFor​​ward()方法

 公共类MainActivity延伸活动{
PixelGridView PGV;
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    PGV =新PixelGridView(本);
    pgv.setNumColumns(20);
    pgv.setNumRows(15);}公共无效onBtnForward pressed(查看视图){
        pgv.moveFor​​ward();
    }
}

和这里的日志:

  05-10 00:36:33.388:E / AndroidRuntime(8414):显示java.lang.NullPointerException:产生的原因
05-10 00:36:33.388:E / AndroidRuntime(8414):在com.test.PixelGridView.moveFor​​ward(PixelGridView.java:131)
05-10 00:36:33.388:E / AndroidRuntime(8414):在com.test.MainActivity.onBtnForward pressed(MainActivity.java:281)

moveFor​​ward()方法移动机器人:

 如果(currentAngle中== 0){
        的for(int i = 0; I<为numColumns;我++){
            对于(INT J = 0; J<其行; J ++){
                如果(cellChecked [I] [J]){
                    列1 = I;
                    ROW1 = j的;                }
            }
        }
        !cellChecked [列1] [ROW1] = cellChecked [列1] [ROW1];
        cellChecked [COLUMN1 + 1] [ROW1] = cellChecked [列1 + 1] [ROW1]!;
        无效();
    }


解决方案

PixelGridView 反对你与创建新不在活动的布局,因为你没有添加它。如果您已经定义了一个 PixelGridView activity_main 布局,那么你应该将其分配给 PGV 使用 findViewById()方法。如果你的意思是编程方式创建它,那么你就应该使用 addContentView()方法添加到活动的布局。

I am currently following this answer. As from this answer, it shows that the pixel/grid will be checked if it has been touched. However, I would like to check a grid as a start point and have controls/button at the mainactivity to move the start point around. For example, if user clicks on move forward button, the start point will move forward, checking the new grid and setting the previous point as white. Now i am getting nullpointerexception upon the checkedCell[column][row] in the moveForward() method

So, here is my code:

public class PixelGridView extends View {
private int numColumns, numRows;
private int cellWidth, cellHeight;
private Paint blackPaint = new Paint();
private boolean[][] cellChecked;

public PixelGridView(Context context)
{
    this(context, null);
}

public PixelGridView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    blackPaint.setStyle(Paint.Style.FILL_AND_STROKE);
}

public void setNumColumns(int numColumns)
{
    this.numColumns = numColumns;
    calculateDimensions();
}

public int getNumColumns()
{
    return numColumns;
}

public void setNumRows(int numRows)
{
    this.numRows = numRows;
    calculateDimensions();
}

public int getNumRows()
{
    return numRows;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    calculateDimensions();
}

private void calculateDimensions()
{
    if (numColumns == 0 || numRows == 0)
        return;

    cellWidth = getWidth() / numColumns;
    cellHeight = getHeight() / numRows;

    cellChecked = new boolean[numColumns][numRows];

    invalidate();
}

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

    canvas.drawColor(Color.WHITE);

    if (numColumns == 0 || numRows == 0)
        return;

    int width = getWidth();
    int height = getHeight();

    for (int i = 0; i < numColumns; i++)
    {
        for (int j = 0; j < numRows; j++)
        {
            if (cellChecked[i][j])
            {
                canvas.drawRect(i * cellWidth, j * cellHeight, (i + 1) * cellWidth, (j + 1) * cellHeight, blackPaint);
            }
        }
    }       

    for (int i = 1; i < numColumns; i++)
    {
        canvas.drawLine(i * cellWidth, 0, i * cellWidth, height, blackPaint);
    }

    for (int i = 1; i < numRows; i++)
    {
        canvas.drawLine(0, i * cellHeight, width, i * cellHeight, blackPaint);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        return true;

    int column = (int)(event.getX() / cellWidth);
    int row = (int)(event.getY() / cellHeight);

    cellChecked[column][row] = !cellChecked[column][row];
    invalidate();

    return true;
}
public void moveForward() {
//set column and row to be checked at [1][1] upon button click on mainactivity
    int column = 1;
    int row = 1;
    cellChecked[column][row] = !cellChecked[column][row];
    invalidate();
}
}

and here is my activity to call the moveForward() method

public class MainActivity extends Activity {
PixelGridView pgv;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    pgv = new PixelGridView(this);
    pgv.setNumColumns(20);
    pgv.setNumRows(15);

}

public void onBtnForwardPressed(View view) {
        pgv.moveForward();
    }
}

and here's the log:

05-10 00:36:33.388: E/AndroidRuntime(8414): Caused by: java.lang.NullPointerException
05-10 00:36:33.388: E/AndroidRuntime(8414):     at com.test.PixelGridView.moveForward(PixelGridView.java:131)
05-10 00:36:33.388: E/AndroidRuntime(8414):     at com.test.MainActivity.onBtnForwardPressed(MainActivity.java:281)

moveForward() method to move robot:

if (currentAngle == 0) {
        for (int i = 0; i < numColumns; i++) {
            for (int j = 0; j < numRows; j++) {
                if (cellChecked[i][j]) {
                    column1 = i;
                    row1 = j;

                }
            }
        } 
        cellChecked[column1][row1] = !cellChecked[column1][row1];           
        cellChecked[column1+1][row1] = !cellChecked[column1+1][row1];
        invalidate();
    }

解决方案

The PixelGridView object you're creating with new isn't in the Activity's layout, as you've not added it. If you've defined a PixelGridView in the activity_main layout, then you should assign it to pgv using the findViewById() method. If you meant to create it programmatically, then you should add it to the Activity's layout using the addContentView() method.

这篇关于Android的PixelGrid OnDraw的检查/跟踪当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆