Code example for ObjectAnimator

Methods: cancel, isRunning

0
     * Force a stop to all pie motion. Called when the user taps during a fling. 
     */ 
    private void stopScrolling() { 
        mScroller.forceFinished(true);
        if (Build.VERSION.SDK_INT >= 11) {
            mAutoCenterAnimator.cancel();
        } 
 
        onScrollFinished(); 
    } 
 
    /** 
     * Called when the user finishes a scroll action. 
     */ 
    private void onScrollFinished() { 
        if (mAutoCenterInSlice) {
            centerOnCurrentItem(); 
        } else { 
            mPieView.decelerate();
        } 
    } 
 
    /** 
     * Kicks off an animation that will result in the pointer being centered in the 
     * pie slice of the currently selected item. 
     */ 
    private void centerOnCurrentItem() { 
        Item current = mData.get(getCurrentItem());
        int targetAngle = current.mStartAngle + (current.mEndAngle - current.mStartAngle) / 2;
        targetAngle -= mCurrentItemAngle;
        if (targetAngle < 90 && mPieRotation > 180) targetAngle += 360;
 
        if (Build.VERSION.SDK_INT >= 11) {
            // Fancy animated version 
            mAutoCenterAnimator.setIntValues(targetAngle);
            mAutoCenterAnimator.setDuration(AUTOCENTER_ANIM_DURATION).start();
        } else { 
            // Dull non-animated version 
            //mPieView.rotateTo(targetAngle); 
        } 
    } 
 
    /** 
     * Internal child class that draws the pie chart onto a separate hardware layer 
     * when necessary. 
     */ 
    private class PieView extends View {
        // Used for SDK < 11 
        private float mRotation = 0;
        private Matrix mTransform = new Matrix(); 
        private PointF mPivot = new PointF();
 
        /** 
         * Construct a PieView 
         * 
         * @param context 
         */ 
        public PieView(Context context) {
            super(context);
        } 
 
        /** 
         * Enable hardware acceleration (consumes memory) 
         */ 
        public void accelerate() { 
            setLayerToHW(this);
        } 
 
        /** 
         * Disable hardware acceleration (releases memory) 
         */ 
        public void decelerate() { 
            setLayerToSW(this);
        } 
 
        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
 
            if (Build.VERSION.SDK_INT < 11) {
                mTransform.set(canvas.getMatrix());
                mTransform.preRotate(mRotation, mPivot.x, mPivot.y);
                canvas.setMatrix(mTransform);
            } 
 
            for (Item it : mData) {
                mPiePaint.setShader(it.mShader);
                canvas.drawArc(mBounds,
                        360 - it.mEndAngle,
                        it.mEndAngle - it.mStartAngle,
                        true, mPiePaint);
            } 
        } 
 
        @Override 
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            mBounds = new RectF(0, 0, w, h);
        } 
 
        RectF mBounds;
 
        public void rotateTo(float pieRotation) {
            mRotation = pieRotation;
            if (Build.VERSION.SDK_INT >= 11) {
                setRotation(pieRotation);
            } else { 
                invalidate();
            } 
        } 
 
        public void setPivot(float x, float y) {
            mPivot.x = x;
            mPivot.y = y;
            if (Build.VERSION.SDK_INT >= 11) {
                setPivotX(x);
                setPivotY(y);
            } else { 
                invalidate();
            } 
        } 
    } 
 
    /** 
     * View that draws the pointer on top of the pie chart 
     */ 
    private class PointerView extends View {
 
        /** 
         * Construct a PointerView object 
         * 
         * @param context 
         */ 
        public PointerView(Context context) {
            super(context);
        } 
 
        @Override 
        protected void onDraw(Canvas canvas) {
            canvas.drawLine(mTextX, mPointerY, mPointerX, mPointerY, mTextPaint);
            canvas.drawCircle(mPointerX, mPointerY, mPointerRadius, mTextPaint);
        } 
    } 
 
    /** 
     * Maintains the state for a data item. 
     */ 
    private class Item { 
        public String mLabel;
        public float mValue;
        public int mColor;
 
        // computed values 
        public int mStartAngle;
        public int mEndAngle;
 
        public int mHighlight;
        public Shader mShader;
    } 
 
    /** 
     * Extends {@link GestureDetector.SimpleOnGestureListener} to provide custom gesture 
     * processing. 
     */ 
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override 
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Set the pie rotation directly. 
            float scrollTheta = vectorToScalarScroll(
                    distanceX,
                    distanceY,
                    e2.getX() - mPieBounds.centerX(),
                    e2.getY() - mPieBounds.centerY());
            setPieRotation(getPieRotation() - (int) scrollTheta / FLING_VELOCITY_DOWNSCALE);
            return true; 
        } 
 
        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // Set up the Scroller for a fling 
            float scrollTheta = vectorToScalarScroll(
                    velocityX,
                    velocityY,
                    e2.getX() - mPieBounds.centerX(),
                    e2.getY() - mPieBounds.centerY());
            mScroller.fling(
                    0, 
                    (int) getPieRotation(), 
                    0, 
                    (int) scrollTheta / FLING_VELOCITY_DOWNSCALE,
                    0, 
                    0, 
                    Integer.MIN_VALUE,
                    Integer.MAX_VALUE);
 
            // Start the animator and tell it to animate for the expected duration of the fling. 
            if (Build.VERSION.SDK_INT >= 11) {
                mScrollAnimator.setDuration(mScroller.getDuration());
                mScrollAnimator.start();
            } 
            return true; 
        } 
 
        @Override 
        public boolean onDown(MotionEvent e) {
            // The user is interacting with the pie, so we want to turn on acceleration 
            // so that the interaction is smooth. 
            mPieView.accelerate();
            if (isAnimationRunning()) { 
                stopScrolling(); 
            } 
            return true; 
        } 
    } 
 
    private boolean isAnimationRunning() { 
        return !mScroller.isFinished() || (Build.VERSION.SDK_INT >= 11 && mAutoCenterAnimator.isRunning());
    } 
 
    /** 
     * Helper method for translating (x,y) scroll vectors into scalar rotation of the pie. 
     *