1
public class MyGameView extends View implements OnTouchListener{

    static int x, y;
    final static int radius = 25;
    private int androidX;
    private int androidY;
    Paint paint;
    Bitmap android;
    boolean touch = false;
    Handler handler = new Handler();

    private Runnable r = new Runnable() {

        @Override
        public void run() {
            invalidate();

        }


    };

    private Runnable r2 = new Runnable() {

        @Override
        public void run() {
            androidX = (int) Math.ceil(Math.random() * (MyGameView.this.getWidth()-android.getWidth()));
            androidY = (int) Math.ceil(Math.random() * (MyGameView.this.getHeight()-android.getHeight()));
        }
    };

    public MyGameView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true); // smooth rendering
        android = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        setFocusable(true);
        setOnTouchListener(this);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        if(touch == true){
            paint.setARGB(255, 222, 78, 112);
            canvas.drawBitmap(android, androidX, androidY, null);
            canvas.drawCircle(x,y,radius,paint);
        }
        handler.postDelayed(r, 1000);
        handler.postDelayed(r2, 3000);
        super.onDraw(canvas);
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) {
        x=(int)event.getX()-(radius/2);      
        y=(int)event.getY()-(radius/2);
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
                touch = true;
                break;

        case MotionEvent.ACTION_UP:
                touch = false;
                break;
        }
        invalidate();  
        return true;
    }

}

ここで私のコード私が欲しいのは、ビットマップがランダムな位置x yで3秒ごとに描画を開始する指を保持することです。すべてが機能しますが、問題の画像はすべての位置に狂ったように描画され、その方法がわかりません。ACTION_DOWN で invalidate() を呼び出してみましたが、タッチした位置に 3 秒ごとに円が描画されます。どんな助けでも大歓迎です。

4

1 に答える 1