ライブ壁紙を勉強していました。でもまずはキャンバスアニメーションを勉強しなきゃ。ここに正常に動作するコードがあります。math.random() を使用します。しかし、私が欲しいのはこのようなものです。
ここでは、上に示したパスのように画像を移動させたいと考えています。私の考えは、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;
}
}
ここで私を助けてくれることを願っています..ありがとう。