2012-09-01 60 views
3

我正在學習動態壁紙。但首先,我必須學習畫布動畫。我在這裏有一個代碼可以正常工作。它使用了一個math.random()。但我想要的是這樣的一些。 enter image description here帆布隨機運動

在這裏,我想圖像移動就像上面說明的路徑。我的想法是對x和y使用math.random。但使用它的問題是圖像將出現在屏幕的任何地方。我想要圖像,創建一個隨機路徑。我知道它關於x和y座標,但我不知道路徑將如何隨機觸發?我真的不能解釋它。但是如果你知道彈跳球,那麼這個球有一個隨機編碼。就這樣。這是我的代碼。

public class MainActivity extends Activity { 

    private Game game; 
    public Handler updateHandler = new Handler(){ 
      /** Gets called on every message that is received */ 
      // @Override 
      public void handleMessage(Message msg) { 
       game.update(); 
       game.invalidate(); 
       super.handleMessage(msg); 
      } 
     }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     game = new Game(this); 
     setContentView(game); 

     Thread myThread = new Thread(new UpdateThread()); 
     myThread.start(); 
    } 

    public class UpdateThread implements Runnable { 

     @Override 
     public void run() { 

      while(true){ 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       MainActivity.this.updateHandler.sendEmptyMessage(0); 
      } 
     } 

    } 

} 


public class Game extends View { 

    private Bitmap image; 
    private Paint paint; 
    private int x=0; 

    public Game(Context context) { 
     super(context); 
     image = Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher)); 
     paint = new Paint(); 
    } 

    // called every Frame 
    protected void onDraw(Canvas canvas) { 

     canvas.drawBitmap(image, x, 0, paint); 
    } 

    // called by thread 
    public void update() { 
      x = 1; 
      x += Math.random() * 100; 

    } 

} 

我希望你能幫助我在這裏..謝謝。

+0

ahm。貝塞爾曲線?你有鏈接關於如何使用或做到這一點?謝謝 – thenewbie

+0

學習[Cubic Hermite樣條曲線](http://en.wikipedia.org/wiki/Cubic_Hermite_spline)可能會提供最簡單的方法來生成與您之後的曲線相似的平滑路徑。 – harism

+0

其數學.. :)拋開光滑的曲線。 Ahm你知道如何隨機觸發x和y值去不同的方向嗎?除了例如直線移動? – thenewbie

回答

4

下面是無限線性插值循環的代碼(您可能想稍後將其更改爲平滑曲線);

PointF mImagePos = new PointF(); 
PointF mImageSource = new PointF(); 
PointF mImageTarget = new PointF(); 
long mInterpolateTime; 

protected void onDraw(Canvas canvas) { 
    canvas.drawBitmap(image, mImagePos.x, mImagePos.y, paint); 
} 

public void update() { 
    final long INTERPOLATION_LENGTH = 2000; 
    long time = SystemClock.uptimeMillis(); 
    if (time - mInterpolateTime > INTERPOLATION_LENGTH) { 
     mImageSource.set(mImageTarget); 
     mImageTarget.x = (float)(Math.random() * 100); 
     mImageTarget.y = (float)(Math.random() * 100); 
     mInterpolateTime = time; 
    } 

    float t = (float)(time - mInterpolateTime)/INTERPOLATION_LENGTH; 
    // For some smoothness uncomment this line; 
    // t = t * t * (3 - 2 * t); 

    mImagePox.x = mImageSource.x + (mImageTarget.x - mImageSource.x) * t; 
    mImagePos.y = mImageSource.y + (mImageTarget.y - mImageSource.y) * t; 
} 
+0

非常感謝。 :) – thenewbie

+0

+1。任何想法在設置「setContentView(R.layout.MyLayout)」之後如何將動畫作爲背景動畫添加到不同的佈局?我想在屏幕底部從左到右遍歷一個類似的動畫。謝謝。 –