ObjectAnimator inside thread

1k views Asked by At

I am quite new to animations and was trying to implement objectAnimator inside thread.The animation is to create a blinking effect (Like RippleEffect) which is in infinite loop.

private void hidePressedRing() {
        pressedAnimator.setFloatValues(pressedRingWidth, 0f);
        pressedAnimator.start();
    }

    private void showPressedRing() {
        pressedAnimator.setFloatValues(animationProgress, pressedRingWidth);
        pressedAnimator.start();
}

The below snippet is inside a thread handler() inside run() method.

if (pressed) {
            showPressedRing();
            pressed=false;
        } else {
            hidePressedRing();
            pressed=true;
        }

how should i implement blinking effect on a circle using objectAnimator in a loop;

1

There are 1 answers

0
Chandrakanth On BEST ANSWER

Change below code according to your requirement...

public class ProgressCircle extends View {

    private Paint paint;
    private int width;
    private int height;
    private int radius;
    private int cx;
    private int cy;
    private float tempRadius;

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

    public ProgressCircle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public ProgressCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = right - left;
        height = bottom - top;

        cx=width/2;
        cy=height/2;

        radius=Math.min(width,height)/2;
    }

    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            tempRadius++;
            if(tempRadius==radius){
                tempRadius=0;
            }
            invalidate();
            handler.postDelayed(this,50);
        }
    };

    private Handler handler=new Handler();

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(runnable);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(runnable);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(cx,cy,tempRadius,paint);
    }
}