2013-05-31 18 views
3

我對android動畫有點新。我正在製作一個項目,該項目將一個球的圖片放置在一個隨機位置,之後它將循環移動。到目前爲止,我已經取得了成功,但現在我想在不同的隨機座標上不斷繪製新形狀。我想過使用一個線程來繪製形狀每隔幾秒鐘,但我似乎無法實現它,而不是把所有東西搞砸。Android - 連續繪製形狀到隨機位置

有誰知道我該如何解決這個問題?另外我知道我每次都必須不斷重置我的隨機座標。有誰知道我該怎麼做?謝謝你的幫助。我的代碼如下:

public class DrawingTheBall extends View { 

Bitmap bball; 
int randX, randY; 
double theta; 


public DrawingTheBall(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball); 
    randX = 1 + (int)(Math.random()*500); 
    randY = 1 + (int)(Math.random()*500); 
    theta = 45; 
} 

public void onDraw(Canvas canvas){ 
    super.onDraw(canvas); 
    //Radius, angle, and coordinates for circle motion 
    float a = 50; 
    float b = 50; 
    float r = 50; 
    int x = 0; 
    int y = 0; 
    theta = theta + Math.toRadians(2); 

    //move ball in circle 
    if(x < canvas.getWidth()){ 
     x = randX + (int) (a +r*Math.cos(theta)); 
    }else{ 
     x = 0; 
    } 
    if(y < canvas.getHeight()){ 
     y = randY + (int) (b +r*Math.sin(theta)); 
    }else{ 
     y = 0; 
    } 
    Paint p = new Paint(); 

    //Implement Thread here 
     thread = new Thread(new Runnable(){ 

        @Override 
        public void run(){ 

        for(int j = 0; j <= 60; j++){ 

          //It tells me to change variables to Final 
         //But if I do that it messes up my if statements above 
          canvas.drawBitmap(bball, x, y, p); 

         } 
         }; 
         try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } //wait one second 
        } 
        } 

       }); 
       thread.start(); 

    //canvas.drawBitmap(bball, x, y, p); 
    invalidate(); 
} 

}

回答

2

理念:

  1. 實現Runnable和處理
  2. 創建通過隨機隨機座標。

    public class DrawingTheBall extends View implements Runnable { 
    
    final Bitmap bball; 
    Random randX, randY; 
    double theta; 
    Handler handler = new Handler(){ 
        public void handleMessage(android.os.Message msg) { 
         invalidate(); 
         System.out.println("redraw"); 
        }; 
    }; 
    
    public DrawingTheBall(Context context) { 
        super(context); 
        bball = BitmapFactory.decodeResource(getResources(), 
          R.drawable.ic_launcher); 
        randX = new Random(); 
        randY = new Random(); 
        theta = 45; 
        new Thread(this).start(); 
    } 
    
    public void onDraw(Canvas canvas) { 
        super.onDraw(canvas); 
        // Radius, angle, and coordinates for circle motion 
        float a = 50; 
        float b = 50; 
        float r = 50; 
        int x = 0; 
        int y = 0; 
        theta = theta + Math.toRadians(2); 
    
        // move ball in circle 
        if (x < canvas.getWidth()) { 
         x = randX.nextInt(100) + (int) (a + r * Math.cos(theta)); // create 
                       // randX 
                       // integer 
        } else { 
         x = 0; 
        } 
        if (y < canvas.getHeight()) { 
         y = randY.nextInt(100) + (int) (b + r * Math.sin(theta));// create 
                       // randX 
                       // integer 
        } else { 
         y = 0; 
        } 
        Paint p = new Paint(); 
        canvas.drawBitmap(bball, x, y, p); 
    } 
    
    public void run() { 
        while (true) { 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         handler.sendEmptyMessage(0); 
        } 
    } 
    

    }

+0

謝謝,但睡眠(1000)給我一個錯誤,它說我需要創建一個名爲睡眠方法。我如何解決它?我知道這可能很簡單。 – David

+0

現在它工作100%。請投票!^_ ^ –