ObjectAnimator里面的线程

时间:2015-06-22 09:20:08

标签: android animation objectanimator

我对动画很新,并试图在线程内部实现objectAnimator。动画是创建一个无限循环的闪烁效果(如RippleEffect)。

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

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

下面的代码片段位于run()方法内的线程处理程序()中。

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

如何在循环中使用objectAnimator在圆上实现闪烁效果;

1 个答案:

答案 0 :(得分:2)

根据您的要求更改以下代码......

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