本文介绍了画布不会随着更改而更新,即使重复调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码到java类,其中我画三个塔,我放置一定数量的戒指。在onTouchEvent函数中,我注意到活动并相应地增加移动的数量,这反过来显示在屏幕上。但它不会更新,直到我前往上一个活动并回来。

This is my Code to the java class wherein I draw three towers onto which I place certain amount of rings. In the onTouchEvent function, I notice activity and increase the number of moves accordingly, which is, in turn being displayed on the screen. But it doesn't update until I go to the previous activity and come back.

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 Activity

NewGame Activity

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()中举杯,我可以看到被多次调用,但它不更新信息或不重新绘制新的画布上一个。
欢迎任何帮助。现在已经停留了几天了

So, I can see all the Toasts on the screen and also if I put up a toast in onDraw(), I can see that being called several times, but it isn't updating the information or not redrawing the new canvas on top of the previous one.Any help is welcome. Been stuck at it for a few days now

推荐答案

你可以通过在你的构造函数中修改无效值来修复它,将其更改为:

you can fix it by threading your invalidate in your constructor that will be fix by changing it to :

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

这篇关于画布不会随着更改而更新,即使重复调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:18