invalidate()不会立即更改自定义视图的内容

时间:2012-04-22 03:31:55

标签: android canvas dialog invalidation custom-view

我有一个包含自定义视图的xml布局:

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_weight="50" 
    >
        <org.example.sudoku.PuzzleView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/puzzlebackground"
            android:id="@+id/puzzleId"/>

    </LinearLayout>

我有一个活动将此布局设置为其内容,调用用户输入的Dialog,然后调用PuzzleView中的函数以最终更改PuzzleView的内容。问题是,在Dialog解雇后,没有立即重新绘制更改。相反,它会在用户的下一个其他输入上重新绘制。 以下是一些可能对您有帮助的代码片段:

Game.java:

// ...
public void showKeypadOrError(int x, int y)
{
    int tiles[] = getUsedTiles(x,y);
    if (tiles.length == 9)
    {
        Toast toast = Toast.makeText(this, R.string.no_moves_label, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }else
    {
        Log.d(TAG, "showKeypad: used=" + toPuzzleString(tiles));
        Dialog v = new Keypad(this, this.puzzleView, x, y);
        v.show();
    }
}
public void setTile(int x, int y, int value) {
    puzzle[y * 9 + x] = value;
}

Keypad.java:

private void setListeners()
{
    for (int i = 0; i < keys.length; i++)
    {
        final int t = i + 1;
        keys[i].setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) 
            {
                returnResult(t);
            }
        });
    }
}

private void returnResult(int tile)
{   
    puzzleView.setSelectedTile(selX, selY, tile);
    puzzleView.invalidate();
    dismiss();
}

最后,PuzzleView.java

public void setSelectedTile(int X, int Y, int tile) {
    game.setTile(X, Y, tile);
    //this.invalidate();// may change hints
}

我发现了一个与我的问题类似的问题,但我无法弄清楚解决方案是什么:

How to invalidate() on return from a dialog?

非常感谢你的帮助! 编辑:添加更多代码 - 希望此帮助: PuzzleView.java

// Handle input in touch mode
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    }

    // Allow player to choose the tile that defined by game
    // But only allow the user to modify the tiles that are blank
    select((int) (event.getX() / width),
            (int) (event.getY() / height));

    int[] predefined = new int[81];
    // Get the tile that are not blank (predefined by game)
    predefined = game.getPredefinedTileFromPuzzle();
    // Check if the selected tile is whether predefined or not
    if (predefined[selY * 9 + selX] == 1) {
        return true;
    }
    //Call the Dialog input from Game.java
    game.showKeypadOrError(selX, selY);
    Log.i(TAG, "Right before terminate onTouchEvent()");
    return true;
}

2 个答案:

答案 0 :(得分:1)

解除对话框后,您应该无效。

实施DialogInterface.OnDismissListener界面并在OnDismiss回调中失效。

    Dialog v = new Keypad(this, this.puzzleView, x, y);

    v.setOnDismissListener(new OnDismissListener() {
    @Override
        public void onDismiss(final DialogInterface arg0) {
            puzzleView.postInvalidate ();
        }
    });

     v.show();

答案 1 :(得分:0)

只做puzzleView.requestLayout();