简体   繁体   English

android如何在触摸事件中每隔3秒随机xy绘制位图

[英]android how to draw Bitmap in random x y every 3 second in touch event

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;
    }

}

Here my code What i want is when i hold my finger the bitmap start drawing every 3 second in random position x y. 这里我的代码我想要的是当我握住手指时,位图在随机位置x y开始每3秒开始绘制一次。 Everything work but the problem image draw like crazy to every position and i can't figure how to do that i try calling invalidate() on ACTION_DOWN but now my circle draw every 3 second to the position i touch. 一切正常,但问题图像像疯了一样画到每个位置,我无法想象如何做到这一点我尝试在ACTION_DOWN上调用invalidate()但现在我的圆圈每3秒画一次我触摸的位置。 Any help is appreciate. 任何帮助都很感激。

I'm not convinced that I totally understand what you want, but the problem with your code is the following: onDraw is called very often. 我不相信我完全理解你想要的东西,但代码的问题如下: onDraw经常被调用。 In each of those calls, you run r2 with a delay of 3 seconds. 在每个调用中,运行r2的延迟为3秒。 So effectively, r2 is just run as often as onDraw , but the first time is delayed for 3 seconds. 实际上, r2onDraw一样频繁运行,但是第一次延迟了3秒。

What I think you want to do instead is run r2 in a separate thread and continuously update the position every 3 seconds, as in: 我认为你想要做的是在一个单独的线程中运行r2并每3秒不断更新一次位置,如:

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;

    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);

        new Thread(r2).start(); // run r2 in a separate thread
    }

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

    @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;
        }
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM