0

How Can I increase the radius of the circle after each a second? I searched about this issue in stackoverflow and I added the Thread.sleep(1000); to my code as follows:

 @Override

    public void onDraw(Canvas canvas) {

        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        for(int r=1;r<=70;r++)
            try {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, r, paint);
            Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } 

But I think that it is wrong to do this. Because when I run in my device it can not work well.

  • If you are worried with UI blocking then use a `Timer` or `Handler` to perform your task instead of looping. It will be more efficient. – Jibran Khan Sep 02 '15 at 06:32
  • I would not suggest to use the sleep method as it would make your app lack in responsive and would block UI , I would suggest you to use timer. – Coas Mckey Sep 02 '15 at 06:34
  • let me post you a answer , U can use it as you wanted – Coas Mckey Sep 02 '15 at 06:34
  • See here http://stackoverflow.com/questions/31219590/how-to-detect-no-touch-on-a-view-after-some-seconds/31219681#31219681 – Jibran Khan Sep 02 '15 at 06:34

4 Answers4

2

Please find custom view class to update view frequently..

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;


public class CustomView extends View {
    Paint paint;
    int radius = 0;

    public CustomView(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5f);
    }

    public void updateView(int radius) {
        this.radius = radius;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
    }
}

And call this method whenever you want to start animation :

public void startViewAnimation() {
        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (i < 70) { // Please change '70' according to how long you want to go
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int baseRadius=20; // base radius is basic radius of circle from which to start animation
                            customView.updateView(i+baseRadius);
                            i++;
                        }
                    });
                } else {
                    i = 0;
                }
            }
        }, 0, 500); // change '500' to milliseconds for how frequent you want to update radius
    }

And Here how to call it :

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rootView);
        customView = new CustomView(this);
        relativeLayout.addView(customView);
        startViewAnimation();

And here is defined variables :

private int i = 1;
CustomView customView;

Thanks..!!

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
0

You could do it with a timer. It might not be exactly a second between them (since it does some magic to save power), but it will probably work for your intended purpose.

I don't think pausing and making multiple draw calls inside the onDraw method is a good idea. Since you are pausing the GUI thread it probably won't like it (it might even wake up if someone does something to it).

Nekresh
  • 2,948
  • 23
  • 28
Astrogat
  • 1,617
  • 12
  • 24
0

I think you can use it here and gives time and intervals as you wanted

TimerTask doAsynchronousTask = new TimerTask() {
    @Override
    public void run() {

        //Perform background work here

        handler.post(new Runnable() {
            public void run() {
                //Perform GUI updation work here

                // increase radius with your method 
            }
        });
    }
};
timer.schedule(doAsynchronousTask, 1, 1000);
Coas Mckey
  • 701
  • 1
  • 13
  • 39
0

Check out this answer here It shows the usage of Handler class wich creates such behaviour

Community
  • 1
  • 1
Vect0rZ
  • 401
  • 2
  • 6