2012-09-01 59 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)”之后如何将动画作为背景动画添加到不同的布局?我想在屏幕底部从左到右遍历一个类似的动画。谢谢。 –