从活动中失效自定义视图

时间:2013-12-02 04:05:37

标签: java android android-custom-view

单击菜单按钮时,我正在尝试重绘自定义视图。我知道invalidate强制再次调用onDraw,但这似乎并没有起作用。

这里有一些代码,有400多行,所以我把它简化为我认为重要的代码。

public class Project2Activity extends Activity {
    private DrawViewProject2 dvp2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new DrawViewProject2(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.project2, menu);
            return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
            switch(item.getItemId()){
            case R.id.new_game:

                    newGame();
                    break;
            case R.id.game_stats:

                    gameStats();
                    break;
            }
            return false;
    }

    public void newGame(){
            randomPosition = 0;
            randomValue = 0;
            xLocationForX.clear();
            yLocationForX.clear();
            xLocationForO.clear();
            yLocationForO.clear();
            winCheck = false;
            tieCount = 0;

            for(int i = 0; i < squares.length; i++){
                    squares[i] = 0;
            }

            dvp2 = new DrawViewProject2(getApplicationContext());

            /**** THIS IS WHERE IM TRYING TO INVALIDATE IT ****/
            dvp2.invalidate();
    }

    class DrawViewProject2 extends View implements OnTouchListener{
            /**
            * Constructor method.
            * @param context
            */
            public DrawViewProject2(Context context){
                    super(context);
                    this.setOnTouchListener(this);
            }
            @Override
            protected void onDraw(Canvas canvas){
                    // Set application background color.
                    canvas.drawColor(Color.rgb(0, 139, 103));

                    // Set Square color and draw squares.
                    paint.setColor(Color.rgb(0, 220, 0));
                    drawAllSquares(15, 15, 251, 251, canvas);


                    xReDrawAll(canvas);
                    oReDrawAll(canvas);
                // ....some code here
            }

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                    // .... some code here
                    return true;
            }

            /**
            * Used for Re drawing all X's and adding a new X
            * using the x and y coordinates.
            * @param x
            * @param y
            * @param canvas
            */
            public void xReDrawAll(float x, float y, Canvas canvas){
                    //Holds past X coordinates and redraws them all.
                    xLocationForX.add(x);
                    yLocationForX.add(y);
                    for(int i = 0, e = 0; i <xLocationForX.size(); i++){
                            //X shape for x player
                            paint.setColor(Color.BLACK);
                            paint.setTextSize(300f);
                            canvas.drawText("X", xLocationForX.get(i), yLocationForX.get(i), paint);
                    }
            }
            /**
            * Used for Re drawing all O's
            * @param canvas
            */
            public void xReDrawAll(Canvas canvas){
                    for(int i = 0, e = 0; i <xLocationForX.size(); i++){
                            //X shape for x player
                            paint.setColor(Color.BLACK);
                            paint.setTextSize(300f);
                            canvas.drawText("X", xLocationForX.get(i), yLocationForX.get(i), paint);
                    }
            }
            // .... lot more code down here.

我也尝试了postInvalidate,也没有做任何事情。

我的应用程序运行正常,当有人点击菜单中的新游戏时,我只需要重新绘制。

0 个答案:

没有答案