即使重复调用,Canvas也不会随更改而更新

时间:2016-07-14 07:07:52

标签: java android canvas ontouchevent ondraw

这是我的java类的代码,其中我绘制了三个塔,我放置了一定数量的环。在onTouchEvent函数中,我注意到activity并相应地增加了移动的数量,而这又是在屏幕上显示的。但是在我进行上一次活动并回来之前,它不会更新。

Number.java

package com.syncnlink.myapplication;

public class Number {
public static int tower1[] = new int[10];
public static int tower2[] = new int[10];
public static int tower3[] = new int[10];


public static void initialize() {
    for (int i = 0; i < 10; i++)
    {
        tower1[i] = i+1;
        tower2[i] = -1;
        tower3[i] = -1;
    }
}
public static int numelements(int arr[]) {
    int count = 0;
    for (int i = 0; i < 10; i++)
    {
        if (arr[i] > 0)
            count++;
        else
            continue;
    }
    return count;
}

public static void setit(int arr[], int a)
{
    arr[a]=-1;
}

public static void setit1(int a)
{
    tower1[a]=-1;
}

public static boolean isEmpty(int arr[])
{
    for(int i=0;i<5;i++)
        if(arr[i]>-1)
        {
            return false;
        }
    return true;
}

}

NewGame活动

package com.syncnlink.myapplication;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class NewGame extends Activity {

public CustomRectangle cr;
static float x,y,width, height;
public static int moves =0;
private static Bitmap background;
static Canvas canvas;
public int ff=0,initialized=0;
public static int t1,t2,t3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    background =    BitmapFactory.decodeResource(getResources(),R.drawable.background);
    super.onCreate(savedInstanceState);
    CustomRectangle cr = new CustomRectangle(this);
    setContentView(cr);
    Number.initialize();


    t1 = Number.numelements(Number.tower1);
    t2 = Number.numelements(Number.tower2);
    t3 = Number.numelements(Number.tower3);
}

public static class CustomRectangle extends View {

    CustomRectangle(Context c) {
        super(c);
        invalidate();
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        NewGame n = new NewGame();
        if(n.initialized == 0)
        {
            Number.initialize();
        }
        Paint p[]= new Paint[11];
        for(int i=0;i<11;i++)
            p[i] = new Paint();
        p[0].setColor(Color.YELLOW);
        p[1].setColor(Color.BLUE);
        p[2].setColor(Color.CYAN);
        p[3].setColor(Color.RED);
        p[4].setColor(Color.GRAY);
        p[5].setColor(Color.MAGENTA);
        p[6].setColor(Color.BLUE);
        p[7].setColor(Color.CYAN);
        p[8].setColor(Color.RED);
        p[9].setColor(Color.GRAY);
        p[10].setColor(Color.MAGENTA);
        canvas.drawBitmap(background,0,0,null);

        p[0].setStrokeWidth(24);

        height = getHeight();
        width = getWidth();
        float w = width/1794;
        float h = height/1008;

        canvas.drawLine(100*(width/1794),1000*(height/1008),500*(width/1794),1000*(height/1008),p[0]);
        canvas.drawLine(300*(width/1794),1000*(height/1008),300*(width/1794),400,p[0]);
        canvas.drawLine((100+600)*(width/1794),1000*(height/1008),(500+600)*(width/1794),1000*(height/1008),p[0]);
        canvas.drawLine((300+600)*(width/1794),1000*(height/1008),(300+600)*(width/1794),400*(height/1008),p[0]);
        canvas.drawLine((100+1200)*(width/1794),1000*(height/1008),(500+1200)*(width/1794),1000*(height/1008),p[0]);
        canvas.drawLine((300+1200)*(width/1794),1000*(height/1008),(300+1200)*(width/1794),400*(height/1008),p[0]);

        int j=0;
        for (int i = 0; i < 10; i++) {
            if(Number.tower1[i]<0)
            continue;
            else {
                canvas.drawRect((150 + (i * 14))*w, (942 - (j * 44))*h, (450 - (i * 14))*w, (987 - (j * 44))*h, p[i + 1]);
                j++;
            }
        }

        j=0;
        for (int i = 0; i < 10; i++) {
            if(Number.tower2[i]<0)
                continue;
            else {
                canvas.drawRect((150 + (i * 14) + 600)*w, (942 - (i * 44))*h, (450 + 600 - (i * 14))*w, (987 - (i * 44))*h, p[i + 1]);
                j++;
            }
        }

        j=0;
        for (int i = 0; i < 10; i++) {
            if(Number.tower3[i]<0)
                continue;
            else {
                canvas.drawRect((150 + 1200 + (i * 14))*w, (942 - (i * 44))*h, (450 + 1200 - (i * 14))*w, (987 - (i * 44))*h, p[i + 1]);
                j++;
            }
        }

        p[0].setTextSize(48);
        canvas.drawText(" No. of Moves  " + moves ,100,40,p[0]);
    }
}

@Override
public boolean onTouchEvent( MotionEvent event1) {
    try {

        switch (event1.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                x=event1.getX();
                y=event1.getY();

                if(x<width/3)
                {

                    if(!Number.isEmpty(Number.tower1)) {
                        Toast.makeText(this, "Pop Tower1   " + t1--, Toast.LENGTH_SHORT).show();
                        ff=0;
                        moves++;
                        Number.setit1(t1);
                        cr.invalidate();
                    }
                    else {
                        Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
                        ff = 1;
                    }
                }
                else
                if(x<width*2/3) {
                    if (!Number.isEmpty(Number.tower2)) {
                        Toast.makeText(this, "Pop Tower2   " + t2--, Toast.LENGTH_SHORT).show();
                        Number.setit(Number.tower2, t2);
                        ff=0;
                        moves++;
                        cr.invalidate();

                    } else {
                        ff = 1;
                        Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    if(!Number.isEmpty(Number.tower3)) {
                        Number.setit(Number.tower3, t3);
                        Toast.makeText(this, "Pop Tower3   " + t3--, Toast.LENGTH_SHORT).show();
                        moves++;
                        cr.invalidate();
                        ff=0;
                    }
                    else {
                        Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
                        ff = 1;
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP: {
                x=event1.getX();
                y=event1.getY();
                if(x<width/3)
                {
                    if(ff==0)
                    {
                    Number.setit2(Number.tower1,++t1);
                    Toast.makeText(this, "Push Tower1    " + t1, Toast.LENGTH_SHORT).show();
                }
                }
                else
                if(x<width*2/3)
                {
                    if(ff==0)
                    {
                    Number.setit2(Number.tower2,++t2);
                    Toast.makeText(this, "Push Tower2    " + t2, Toast.LENGTH_SHORT).show();
                }
                }

                else
                {
                    if(ff==0) {
                        Number.setit2(Number.tower3, ++t3);
                        Toast.makeText(this, "Push Tower3    " + t3, Toast.LENGTH_SHORT).show();
                    }
                }

                break;
            }

        }
    }
    catch(Exception e)
    {

    }
    return true;
}
}

所以,我可以在屏幕上看到所有Toasts,如果我在onDraw()中放了一个toast,我可以看到被多次调用,但是它没有更新信息或者没有重新绘制在前一个上面的新画布。 欢迎任何帮助。现在已经坚持了几天

2 个答案:

答案 0 :(得分:0)

我认为您需要在CustomRectangle.class

上创建一些setter
public void setSomething(Arguments)
{
    // do your changes here 
    invalidate();
}

并调用setter来改变onTounch中绘制的状态。

case:MotionEvent.ACTION_DOWN:
    cr.setSomething(Arguments);
....
....

当您调用setter时,他们将在调用invalidate后更新画布的状态。

我希望这可以帮到你!

答案 1 :(得分:0)

您可以通过在构造函数中线程化您的invalidate来修复它,将其修改为:

   CustomRectangle(Context c) {
            super(c);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true) {
                        try {
                            Thread.sleep(1000);
                            postInvalidate();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            });

        thread.start();
        }