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

}

這里我的代碼我想要的是當我握住手指時,位圖在隨機位置x y開始每3秒開始繪制一次。 一切正常,但問題圖像像瘋了一樣畫到每個位置,我無法想象如何做到這一點我嘗試在ACTION_DOWN上調用invalidate()但現在我的圓圈每3秒畫一次我觸摸的位置。 任何幫助都很感激。

我不相信我完全理解你想要的東西,但代碼的問題如下: onDraw經常被調用。 在每個調用中,運行r2的延遲為3秒。 實際上, r2onDraw一樣頻繁運行,但是第一次延遲了3秒。

我認為你想要做的是在一個單獨的線程中運行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