2016-07-14 22 views
2

這是我對Java類的代碼,其中繪製了三個塔,其中放置了一定數量的環。在onTouchEvent函數中,我注意到了活動並相應地增加了移動的次數,這反過來又顯示在屏幕上。但是,直到我進入上一個活動並返回時它纔會更新。畫布並未隨更改而更新,即使反覆調用

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

所以,我可以看到所有的屏幕上的祝酒詞也,如果我提出了一個土司的onDraw() ,我可以看到被多次調用,但不會更新信息,也不會重新繪製上一個畫布上的新畫布。 歡迎任何幫助。一直停留在這幾天,現在

+0

http://stackoverflow.com/q/2865315/5733853 – HPbyP

回答

0

我認爲你需要在你的CustomRectangle.class

public void setSomething(Arguments) 
{ 
    // do your changes here 
    invalidate(); 
} 

創建一些setter和調用制定者改變您的onTounch內繪製狀態。

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

當您調用setter時,他們將在調用invalidate後更新canvas的狀態。

我希望這能幫到你!

+0

我已經使用了setter方法在「onTouchEvent」函數中。所以我想它應該更新狀態,如果我的畫布在無效呼叫之後。? 是否有必要在CustomRectangle類中做到這一點? @AdrianoAlves –

+0

你在你的setters中調用invalidate()嗎?它一定會成爲你製造者的最後一行 –

0

你可以通過你的線程在構造函數中無效修復它,這將是被它改變來解決:

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

謝謝,作品像一個魅力! :D @HPbyP –

+1

您的歡迎,但它更好地做投票而不是發表評論。 – HPbyP

+1

http://stackoverflow.com/q/2865315/5733853 – HPbyP