Example usage for android.view.animation Animation setInterpolator

List of usage examples for android.view.animation Animation setInterpolator

Introduction

In this page you can find the example usage for android.view.animation Animation setInterpolator.

Prototype

public void setInterpolator(Interpolator i) 

Source Link

Document

Sets the acceleration curve for this animation.

Usage

From source file:com.mobility.android.ui.widget.MaterialProgressDrawable.java

private void setupAnimators() {
    Ring ring = mRing;//w  ww  . j av a 2  s. c  om
    Animation animation = new Animation() {
        @Override
        public void applyTransformation(float interpolatedTime, Transformation t) {
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                float minProgressArc = getMinProgressArc(ring);
                float startingEndTrim = ring.getStartingEndTrim();
                float startingTrim = ring.getStartingStartTrim();
                float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    float scaledTime = interpolatedTime / (1.0f - START_TRIM_DURATION_OFFSET);
                    float startTrim = startingTrim + (MAX_PROGRESS_ARC - minProgressArc)
                            * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime);
                    ring.setStartTrim(startTrim);
                }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    float minArc = MAX_PROGRESS_ARC - minProgressArc;
                    float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET)
                            / (1.0f - START_TRIM_DURATION_OFFSET);
                    float endTrim = startingEndTrim
                            + minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime);
                    ring.setEndTrim(endTrim);
                }

                float rotation = startingRotation + 0.25f * interpolatedTime;
                ring.setRotation(rotation);

                float groupRotation = FULL_ROTATION / NUM_POINTS * interpolatedTime
                        + FULL_ROTATION * mRotationCount / NUM_POINTS;
                setRotation(groupRotation);
            }
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    animation.setRepeatMode(Animation.RESTART);
    animation.setInterpolator(LINEAR_INTERPOLATOR);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mRotationCount = 0;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
                ring.setShowArrow(false);
            } else {
                mRotationCount = (mRotationCount + 1) % NUM_POINTS;
            }
        }
    });
    mAnimation = animation;
}

From source file:com.xfzbd.cqi.widget.srl.ShyaringanProgressDrawable.java

private void setupAnimators() {
    final Shyaringan ring = mRing;
    final Animation animation = new Animation() {
        @Override/*from  w  w  w  .jav  a 2  s. c o  m*/
        public void applyTransformation(float interpolatedTime, Transformation t) {
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                final float minProgressArc = getMinProgressArc(ring);
                final float startingEndTrim = ring.getStartingEndTrim();
                final float startingTrim = ring.getStartingStartTrim();
                final float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                // if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                // scale the interpolatedTime so that the full
                // transformation from 0 - 1 takes place in the
                // remaining time
                // final float scaledTime = (interpolatedTime) / (1.0f - START_TRIM_DURATION_OFFSET);
                // final float startTrim = startingTrim + ((MAX_PROGRESS_ARC - minProgressArc)
                // * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                // ring.setStartTrim(startTrim);
                // }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                // if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                // scale the interpolatedTime so that the full
                // transformation from 0 - 1 takes place in the
                // remaining time
                // final float minArc = MAX_PROGRESS_ARC - minProgressArc;
                // float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET) / (1.0f - START_TRIM_DURATION_OFFSET);
                // final float endTrim = startingEndTrim + (minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                // ring.setEndTrim(endTrim);
                // }

                final float rotation = (startingRotation + (500f * interpolatedTime)) % 360f;
                XLog.e("srl --> rotation - %f - setRotation - shyaringan", rotation);
                ring.setRotation(rotation);

                // float groupRotation = ((FULL_ROTATION / NUM_POINTS) * interpolatedTime) + (FULL_ROTATION * (mRotationCount / NUM_POINTS));
                // setRotation(groupRotation);
            }
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    animation.setRepeatMode(Animation.RESTART);
    animation.setInterpolator(LINEAR_INTERPOLATOR);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            XLog.e("srl --> refresh 1 - " + System.currentTimeMillis());
            mRotationCount = 0;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
            } else {
                mRotationCount = (mRotationCount + 1) % (NUM_POINTS);
            }
        }
    });
    mAnimation = animation;
}

From source file:com.congxiaoyao.xber_admin.widget.MaterialProgressDrawable.java

private void setupAnimators() {
    final MaterialProgressDrawable.Ring ring = mRing;
    final Animation animation = new Animation() {
        @Override/*from   www.jav  a2s. c  o m*/
        public void applyTransformation(float interpolatedTime, Transformation t) {
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                final float minProgressArc = getMinProgressArc(ring);
                final float startingEndTrim = ring.getStartingEndTrim();
                final float startingTrim = ring.getStartingStartTrim();
                final float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float scaledTime = (interpolatedTime) / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float startTrim = startingTrim + ((MAX_PROGRESS_ARC - minProgressArc)
                            * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setStartTrim(startTrim);
                }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float minArc = MAX_PROGRESS_ARC - minProgressArc;
                    float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET)
                            / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float endTrim = startingEndTrim
                            + (minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setEndTrim(endTrim);
                }

                final float rotation = startingRotation + (0.25f * interpolatedTime);
                ring.setRotation(rotation);

                float groupRotation = ((FULL_ROTATION / NUM_POINTS) * interpolatedTime)
                        + (FULL_ROTATION * (mRotationCount / NUM_POINTS));
                setRotation(groupRotation);
            }
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    animation.setRepeatMode(Animation.RESTART);
    animation.setInterpolator(LINEAR_INTERPOLATOR);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mRotationCount = 0;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
                ring.setShowArrow(false);
            } else {
                mRotationCount = (mRotationCount + 1) % (NUM_POINTS);
            }
        }
    });
    mAnimation = animation;
}

From source file:cn.spinsoft.wdq.widget.swipe.MaterialProgressDrawable.java

private void setupAnimators() {
    final Ring ring = mRing;
    final Animation animation = new Animation() {
        @Override//from   w  w  w .j  a  va  2  s. c  om
        public void applyTransformation(float interpolatedTime, Transformation t) {
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                final float minProgressArc = getMinProgressArc(ring);
                final float startingEndTrim = ring.getStartingEndTrim();
                final float startingTrim = ring.getStartingStartTrim();
                final float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float scaledTime = (interpolatedTime) / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float startTrim = startingTrim + ((MAX_PROGRESS_ARC - minProgressArc)
                            * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setStartTrim(startTrim);
                }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float minArc = MAX_PROGRESS_ARC - minProgressArc;
                    float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET)
                            / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float endTrim = startingEndTrim
                            + (minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setEndTrim(endTrim);
                }

                final float rotation = startingRotation + (0.25f * interpolatedTime);
                ring.setRotation(rotation);

                float groupRotation = ((FULL_ROTATION / NUM_POINTS) * interpolatedTime)
                        + (FULL_ROTATION * (mRotationCount / NUM_POINTS));
                setRotation(groupRotation);
            }
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    animation.setRepeatMode(Animation.RESTART);
    animation.setInterpolator(LINEAR_INTERPOLATOR);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mRotationCount = 0;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
                ring.setShowArrow(false);
            } else {
                mRotationCount = (mRotationCount + 1) % (NUM_POINTS);
            }
        }
    });
    mAnimation = animation;
}

From source file:com.androidhuman.circlerefreshlayout.MaterialProgressDrawable.java

private void setupAnimators() {
    final Ring ring = mRing;
    final Animation animation = new Animation() {
        @Override/*from  w  ww .  ja  va2 s.co m*/
        public void applyTransformation(float interpolatedTime, Transformation t) {
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                final float minProgressArc = getMinProgressArc(ring);
                final float startingEndTrim = ring.getStartingEndTrim();
                final float startingTrim = ring.getStartingStartTrim();
                final float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float scaledTime = interpolatedTime / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float startTrim = startingTrim + (MAX_PROGRESS_ARC - minProgressArc)
                            * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime);
                    ring.setStartTrim(startTrim);
                }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float minArc = MAX_PROGRESS_ARC - minProgressArc;
                    float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET)
                            / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float endTrim = startingEndTrim
                            + (minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setEndTrim(endTrim);
                }

                final float rotation = startingRotation + (0.25f * interpolatedTime);
                ring.setRotation(rotation);

                float groupRotation = (FULL_ROTATION / NUM_POINTS) * interpolatedTime
                        + FULL_ROTATION * (mRotationCount / NUM_POINTS);
                setRotation(groupRotation);
            }
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    animation.setRepeatMode(Animation.RESTART);
    animation.setInterpolator(LINEAR_INTERPOLATOR);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mRotationCount = 0;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
                ring.setShowArrow(false);
            } else {
                mRotationCount = (mRotationCount + 1) % NUM_POINTS;
            }
        }
    });
    mAnimation = animation;
}

From source file:com.diegocarloslima.byakugallery.TouchImageView.java

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    if (ANIMATION_DURATION == 0)
        ANIMATION_DURATION = context.getResources().getInteger(android.R.integer.config_shortAnimTime);

    final TouchGestureDetector.OnTouchGestureListener listener = new TouchGestureDetector.OnTouchGestureListener() {

        @Override// ww  w. ja v  a  2s . c o  m
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public void onLongPress(MotionEvent e) {
            performLongClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            loadMatrixValues();

            // 3 stage scaling
            float targetScale = mCropScale;
            if (mScale == mMaxScale)
                targetScale = mMinScale;
            else if (mScale >= mCropScale)
                targetScale = mMaxScale;

            // First, we try to keep the focused point in the same position when the animation ends
            final float desiredTranslationX = e.getX() - (e.getX() - mTranslationX) * (targetScale / mScale);
            final float desiredTranslationY = e.getY() - (e.getY() - mTranslationY) * (targetScale / mScale);

            // Here, we apply a correction to avoid unwanted blank spaces
            final float targetTranslationX = desiredTranslationX + computeTranslation(getMeasuredWidth(),
                    mDrawableIntrinsicWidth * targetScale, desiredTranslationX, 0);
            final float targetTranslationY = desiredTranslationY + computeTranslation(getMeasuredHeight(),
                    mDrawableIntrinsicHeight * targetScale, desiredTranslationY, 0);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(ANIMATION_DURATION);
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    -distanceX);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    -distanceY);
            mMatrix.postTranslate(dx, dy);

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float horizontalSideFreeSpace = (getMeasuredWidth() - mDrawableIntrinsicWidth * mScale) / 2F;
            final float minTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace
                    : getMeasuredWidth() - mDrawableIntrinsicWidth * mScale;
            final float maxTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace : 0;

            final float verticalSideFreeSpace = (getMeasuredHeight() - mDrawableIntrinsicHeight * mScale) / 2F;
            final float minTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace
                    : getMeasuredHeight() - mDrawableIntrinsicHeight * mScale;
            final float maxTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace : 0;

            // Using FlingScroller here. The results were better than the Scroller class
            // https://android.googlesource.com/platform/packages/apps/Gallery2/+/master/src/com/android/gallery3d/ui/FlingScroller.java
            mFlingScroller.fling(Math.round(mTranslationX), Math.round(mTranslationY), Math.round(velocityX),
                    Math.round(velocityY), Math.round(minTranslationX), Math.round(maxTranslationX),
                    Math.round(minTranslationY), Math.round(maxTranslationY));

            clearAnimation();
            final Animation animation = new FlingAnimation();
            animation.setDuration(mFlingScroller.getDuration());
            animation.setInterpolator(new LinearInterpolator());
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mLastFocusX = null;
            mLastFocusY = null;

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            loadMatrixValues();

            float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float focusX = computeFocus(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    detector.getFocusX());
            final float focusY = computeFocus(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    detector.getFocusY());

            // Here, we provide the ability to scroll while scaling
            if (mLastFocusX != null && mLastFocusY != null) {
                final float dx = computeScaleTranslation(getMeasuredWidth(), currentDrawableWidth,
                        mTranslationX, focusX - mLastFocusX);
                final float dy = computeScaleTranslation(getMeasuredHeight(), currentDrawableHeight,
                        mTranslationY, focusY - mLastFocusY);

                if (dx != 0 || dy != 0) {
                    mMatrix.postTranslate(dx, dy);
                }
            }

            final float scale = computeScale(mMinScale, mMaxScale, mScale, detector.getScaleFactor());
            mMatrix.postScale(scale, scale, focusX, focusY);

            mLastFocusX = focusX;
            mLastFocusY = focusY;

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX, 0);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY, 0);

            if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
                return;
            }

            final float targetTranslationX = mTranslationX + dx;
            final float targetTranslationY = mTranslationY + dy;

            float targetScale = MathUtils.clamp(mScale, mMinScale, mMaxScale);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(ANIMATION_DURATION);
            startAnimation(animation);

            mIsAnimatingBack = true;
        }
    };

    mTouchGestureDetector = new TouchGestureDetector(context, listener);

    super.setScaleType(ScaleType.MATRIX);
}

From source file:org.mariotaku.twidere.view.TouchImageView.java

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    final TouchGestureDetector.OnTouchGestureListener listener = new TouchGestureDetector.OnTouchGestureListener() {

        @Override//from   w  w w . j  ava 2 s. c o  m
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public void onLongPress(MotionEvent e) {
            performLongClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            loadMatrixValues();

            final float minScale = getMinScale();
            // If we have already zoomed in, we should return to our initial scale value (minScale). Otherwise, scale to full size
            final boolean shouldZoomOut = mScale > minScale;
            final float targetScale = shouldZoomOut ? minScale : mMaxScale;

            // First, we try to keep the focused point in the same position when the animation ends
            final float desiredTranslationX = e.getX() - (e.getX() - mTranslationX) * (targetScale / mScale);
            final float desiredTranslationY = e.getY() - (e.getY() - mTranslationY) * (targetScale / mScale);

            // Here, we apply a correction to avoid unwanted blank spaces
            final float targetTranslationX = desiredTranslationX + computeTranslation(getMeasuredWidth(),
                    mDrawableIntrinsicWidth * targetScale, desiredTranslationX, 0);
            final float targetTranslationY = desiredTranslationY + computeTranslation(getMeasuredHeight(),
                    mDrawableIntrinsicHeight * targetScale, desiredTranslationY, 0);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(DOUBLE_TAP_ANIMATION_DURATION);
            startAnimation(animation);

            if (mZoomListener != null) {
                if (shouldZoomOut) {
                    mZoomListener.onZoomOut();
                } else {
                    mZoomListener.onZoomIn();
                }
            }
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    -distanceX);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    -distanceY);
            mMatrix.postTranslate(dx, dy);

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float horizontalSideFreeSpace = (getMeasuredWidth() - mDrawableIntrinsicWidth * mScale) / 2F;
            final float minTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace
                    : getMeasuredWidth() - mDrawableIntrinsicWidth * mScale;
            final float maxTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace : 0;

            final float verticalSideFreeSpace = (getMeasuredHeight() - mDrawableIntrinsicHeight * mScale) / 2F;
            final float minTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace
                    : getMeasuredHeight() - mDrawableIntrinsicHeight * mScale;
            final float maxTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace : 0;

            // Using FlingScroller here. The results were better than the Scroller class
            // https://android.googlesource.com/platform/packages/apps/Gallery2/+/master/src/com/android/gallery3d/ui/FlingScroller.java
            mFlingScroller.fling(Math.round(mTranslationX), Math.round(mTranslationY), Math.round(velocityX),
                    Math.round(velocityY), Math.round(minTranslationX), Math.round(maxTranslationX),
                    Math.round(minTranslationY), Math.round(maxTranslationY));

            clearAnimation();
            final Animation animation = new FlingAnimation();
            animation.setDuration(mFlingScroller.getDuration());
            animation.setInterpolator(new LinearInterpolator());
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mLastFocusX = null;
            mLastFocusY = null;

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            loadMatrixValues();

            float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float focusX = computeFocus(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    detector.getFocusX());
            final float focusY = computeFocus(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    detector.getFocusY());

            // Here, we provide the ability to scroll while scaling
            if (mLastFocusX != null && mLastFocusY != null) {
                final float dx = computeScaleTranslation(getMeasuredWidth(), currentDrawableWidth,
                        mTranslationX, focusX - mLastFocusX);
                final float dy = computeScaleTranslation(getMeasuredHeight(), currentDrawableHeight,
                        mTranslationY, focusY - mLastFocusY);

                if (dx != 0 || dy != 0) {
                    mMatrix.postTranslate(dx, dy);
                }
            }

            final float scale = computeScale(getMinScale(), mMaxScale, mScale, detector.getScaleFactor());
            mMatrix.postScale(scale, scale, focusX, focusY);

            mLastFocusX = focusX;
            mLastFocusY = focusY;

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX, 0);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY, 0);

            if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
                return;
            }

            final float targetTranslationX = mTranslationX + dx;
            final float targetTranslationY = mTranslationY + dy;

            clearAnimation();
            final Animation animation = new TouchAnimation(mScale, targetTranslationX, targetTranslationY);
            animation.setDuration(SCALE_END_ANIMATION_DURATION);
            startAnimation(animation);

            mIsAnimatingBack = true;
        }
    };

    mTouchGestureDetector = new TouchGestureDetector(context, listener);

    super.setScaleType(ScaleType.MATRIX);
}

From source file:cw.kop.autobackground.sources.SourceListFragment.java

/**
 * Shows LocalImageFragment to view images
 *
 * @param view  source card which was selected
 * @param index position of source in listAdapter
 *///from w  w w .  ja va2 s .co m
private void showViewImageFragment(final View view, final int index) {
    sourceList.setOnItemClickListener(null);
    sourceList.setEnabled(false);

    listAdapter.saveData();
    Source item = listAdapter.getItem(index);
    String type = item.getType();
    String directory;
    if (type.equals(AppSettings.FOLDER)) {
        directory = item.getData().split(AppSettings.DATA_SPLITTER)[0];
    } else {
        directory = AppSettings.getDownloadPath() + "/" + item.getTitle() + " " + AppSettings.getImagePrefix();
    }

    Log.i(TAG, "Directory: " + directory);

    final RelativeLayout sourceContainer = (RelativeLayout) view.findViewById(R.id.source_container);
    final ImageView sourceImage = (ImageView) view.findViewById(R.id.source_image);
    final View imageOverlay = view.findViewById(R.id.source_image_overlay);
    final EditText sourceTitle = (EditText) view.findViewById(R.id.source_title);
    final ImageView deleteButton = (ImageView) view.findViewById(R.id.source_delete_button);
    final ImageView viewButton = (ImageView) view.findViewById(R.id.source_view_image_button);
    final ImageView editButton = (ImageView) view.findViewById(R.id.source_edit_button);
    final LinearLayout sourceExpandContainer = (LinearLayout) view.findViewById(R.id.source_expand_container);

    final float viewStartHeight = sourceContainer.getHeight();
    final float viewStartY = view.getY();
    final float overlayStartAlpha = imageOverlay.getAlpha();
    final float listHeight = sourceList.getHeight();
    Log.i(TAG, "listHeight: " + listHeight);
    Log.i(TAG, "viewStartHeight: " + viewStartHeight);

    final LocalImageFragment localImageFragment = new LocalImageFragment();
    Bundle arguments = new Bundle();
    arguments.putString("view_path", directory);
    localImageFragment.setArguments(arguments);

    Animation animation = new Animation() {

        private boolean needsFragment = true;

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            if (needsFragment && interpolatedTime >= 1) {
                needsFragment = false;
                getFragmentManager().beginTransaction()
                        .add(R.id.content_frame, localImageFragment, "image_fragment").addToBackStack(null)
                        .setTransition(FragmentTransaction.TRANSIT_NONE).commit();
            }
            ViewGroup.LayoutParams params = sourceContainer.getLayoutParams();
            params.height = (int) (viewStartHeight + (listHeight - viewStartHeight) * interpolatedTime);
            sourceContainer.setLayoutParams(params);
            view.setY(viewStartY - interpolatedTime * viewStartY);
            deleteButton.setAlpha(1.0f - interpolatedTime);
            viewButton.setAlpha(1.0f - interpolatedTime);
            editButton.setAlpha(1.0f - interpolatedTime);
            sourceTitle.setAlpha(1.0f - interpolatedTime);
            imageOverlay.setAlpha(overlayStartAlpha - overlayStartAlpha * (1.0f - interpolatedTime));
            sourceExpandContainer.setAlpha(1.0f - interpolatedTime);
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (needsListReset) {
                Parcelable state = sourceList.onSaveInstanceState();
                sourceList.setAdapter(null);
                sourceList.setAdapter(listAdapter);
                sourceList.onRestoreInstanceState(state);
                sourceList.setOnItemClickListener(SourceListFragment.this);
                sourceList.setEnabled(true);
                needsListReset = false;
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    ValueAnimator cardColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
            AppSettings.getDialogColor(appContext),
            getResources().getColor(AppSettings.getBackgroundColorResource()));
    cardColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            sourceContainer.setBackgroundColor((Integer) animation.getAnimatedValue());
        }

    });

    DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(1.5f);

    animation.setDuration(INFO_ANIMATION_TIME);
    cardColorAnimation.setDuration(INFO_ANIMATION_TIME);

    animation.setInterpolator(decelerateInterpolator);
    cardColorAnimation.setInterpolator(decelerateInterpolator);

    needsListReset = true;
    cardColorAnimation.start();
    view.startAnimation(animation);

}

From source file:cw.kop.autobackground.sources.SourceListFragment.java

@Override
public void onClick(final View v) {

    switch (v.getId()) {

    case R.id.set_button:
        setWallpaper();//from ww w. j  a  va  2  s  . co m
        break;
    case R.id.floating_button_icon:
        final GradientDrawable circleDrawable = (GradientDrawable) getResources()
                .getDrawable(R.drawable.floating_button_circle);
        final float scale = (float) ((Math.hypot(addButtonBackground.getX(), addButtonBackground.getY())
                + addButtonBackground.getWidth()) / addButtonBackground.getWidth() * 2);

        Animation animation = new Animation() {

            private boolean needsFragment = true;
            private float pivot;

            @Override
            public void initialize(int width, int height, int parentWidth, int parentHeight) {
                super.initialize(width, height, parentWidth, parentHeight);

                pivot = resolveSize(RELATIVE_TO_SELF, 0.5f, width, parentWidth);
            }

            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {

                if (needsFragment && interpolatedTime >= 1) {
                    needsFragment = false;
                    showSourceAddFragment();
                } else {
                    float scaleFactor = 1.0f + ((scale - 1.0f) * interpolatedTime);
                    t.getMatrix().setScale(scaleFactor, scaleFactor, pivot, pivot);
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }

        };

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                circleDrawable.setColor(getResources().getColor(R.color.ACCENT_OPAQUE));
                addButtonBackground.setImageDrawable(circleDrawable);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (needsButtonReset) {
                            addButton.setOnClickListener(SourceListFragment.this);
                            addButtonBackground.setScaleX(1.0f);
                            addButtonBackground.setScaleY(1.0f);
                            addButtonBackground.clearAnimation();
                            circleDrawable.setColor(getResources().getColor(R.color.ACCENT_OPAQUE));
                            addButtonBackground.setImageDrawable(circleDrawable);
                            addButton.setVisibility(View.VISIBLE);
                        }
                    }
                }, 100);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

        });

        ValueAnimator buttonColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
                getResources().getColor(R.color.ACCENT_OPAQUE),
                getResources().getColor(AppSettings.getBackgroundColorResource()));
        buttonColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                circleDrawable.setColor((Integer) animation.getAnimatedValue());
                addButtonBackground.setImageDrawable(circleDrawable);
            }

        });

        DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();

        animation.setDuration(ADD_ANIMATION_TIME);
        buttonColorAnimation.setDuration((long) (ADD_ANIMATION_TIME * 0.9));
        buttonColorAnimation.setInterpolator(decelerateInterpolator);
        animation.setInterpolator(decelerateInterpolator);

        // Post a delayed Runnable to ensure reset even if animation is interrupted
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (needsButtonReset) {
                    addButtonBackground.setScaleX(1.0f);
                    addButtonBackground.setScaleY(1.0f);
                    addButtonBackground.clearAnimation();
                    circleDrawable.setColor(getResources().getColor(R.color.ACCENT_OPAQUE));
                    addButtonBackground.setImageDrawable(circleDrawable);
                    addButton.setVisibility(View.VISIBLE);
                    needsButtonReset = false;
                }
            }
        }, (long) (ADD_ANIMATION_TIME * 1.1f));

        needsButtonReset = true;
        addButton.setVisibility(View.GONE);
        buttonColorAnimation.start();
        addButtonBackground.startAnimation(animation);
        break;
    default:
    }
}

From source file:cw.kop.autobackground.sources.SourceListFragment.java

private void startEditFragment(final View view, final int position) {
    sourceList.setOnItemClickListener(null);
    sourceList.setEnabled(false);//  w  ww .j a  va  2 s.c om
    listAdapter.saveData();

    Source dataItem = listAdapter.getItem(position);
    final SourceInfoFragment sourceInfoFragment = new SourceInfoFragment();
    sourceInfoFragment.setImageDrawable(((ImageView) view.findViewById(R.id.source_image)).getDrawable());
    Bundle arguments = new Bundle();
    arguments.putInt("position", position);
    arguments.putString("type", dataItem.getType());
    arguments.putString("title", dataItem.getTitle());
    arguments.putString("data", dataItem.getData());
    arguments.putInt("num", dataItem.getNum());
    arguments.putBoolean("use", dataItem.isUse());
    arguments.putBoolean("preview", dataItem.isPreview());
    String imageFileName = dataItem.getImageFile().getAbsolutePath();
    if (imageFileName != null && imageFileName.length() > 0) {
        arguments.putString("image", imageFileName);
    } else {
        arguments.putString("image", "");
    }

    arguments.putBoolean("use_time", dataItem.isUseTime());
    arguments.putString("time", dataItem.getTime());

    sourceInfoFragment.setArguments(arguments);

    final RelativeLayout sourceContainer = (RelativeLayout) view.findViewById(R.id.source_container);
    final CardView sourceCard = (CardView) view.findViewById(R.id.source_card);
    final View imageOverlay = view.findViewById(R.id.source_image_overlay);
    final EditText sourceTitle = (EditText) view.findViewById(R.id.source_title);
    final ImageView deleteButton = (ImageView) view.findViewById(R.id.source_delete_button);
    final ImageView viewButton = (ImageView) view.findViewById(R.id.source_view_image_button);
    final ImageView editButton = (ImageView) view.findViewById(R.id.source_edit_button);
    final LinearLayout sourceExpandContainer = (LinearLayout) view.findViewById(R.id.source_expand_container);

    final float cardStartShadow = sourceCard.getPaddingLeft();
    final float viewStartHeight = sourceContainer.getHeight();
    final float viewStartY = view.getY();
    final int viewStartPadding = view.getPaddingLeft();
    final float textStartX = sourceTitle.getX();
    final float textStartY = sourceTitle.getY();
    final float textTranslationY = sourceTitle.getHeight(); /*+ TypedValue.applyDimension(
                                                            TypedValue.COMPLEX_UNIT_DIP,
                                                            8,
                                                            getResources().getDisplayMetrics());*/

    Animation animation = new Animation() {

        private boolean needsFragment = true;

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            if (needsFragment && interpolatedTime >= 1) {
                needsFragment = false;
                getFragmentManager().beginTransaction()
                        .add(R.id.content_frame, sourceInfoFragment, "source_info_fragment")
                        .addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_NONE).commit();
            }
            int newPadding = Math.round(viewStartPadding * (1 - interpolatedTime));
            int newShadowPadding = (int) (cardStartShadow * (1.0f - interpolatedTime));
            sourceCard.setShadowPadding(newShadowPadding, 0, newShadowPadding, 0);
            ((LinearLayout.LayoutParams) sourceCard.getLayoutParams()).topMargin = newShadowPadding;
            ((LinearLayout.LayoutParams) sourceCard.getLayoutParams()).bottomMargin = newShadowPadding;
            ((LinearLayout.LayoutParams) sourceCard.getLayoutParams()).leftMargin = newShadowPadding;
            ((LinearLayout.LayoutParams) sourceCard.getLayoutParams()).rightMargin = newShadowPadding;
            view.setPadding(newPadding, 0, newPadding, 0);
            view.setY(viewStartY - interpolatedTime * viewStartY);
            ViewGroup.LayoutParams params = sourceContainer.getLayoutParams();
            params.height = (int) (viewStartHeight + (screenHeight - viewStartHeight) * interpolatedTime);
            sourceContainer.setLayoutParams(params);
            sourceTitle.setY(textStartY + interpolatedTime * textTranslationY);
            sourceTitle.setX(textStartX + viewStartPadding - newPadding);
            deleteButton.setAlpha(1.0f - interpolatedTime);
            viewButton.setAlpha(1.0f - interpolatedTime);
            editButton.setAlpha(1.0f - interpolatedTime);
            sourceExpandContainer.setAlpha(1.0f - interpolatedTime);
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (needsListReset) {
                Parcelable state = sourceList.onSaveInstanceState();
                sourceList.setAdapter(null);
                sourceList.setAdapter(listAdapter);
                sourceList.onRestoreInstanceState(state);
                sourceList.setOnItemClickListener(SourceListFragment.this);
                sourceList.setEnabled(true);
                needsListReset = false;
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    ValueAnimator cardColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
            AppSettings.getDialogColor(appContext),
            getResources().getColor(AppSettings.getBackgroundColorResource()));
    cardColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            sourceContainer.setBackgroundColor((Integer) animation.getAnimatedValue());
        }

    });

    ValueAnimator titleColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
            sourceTitle.getCurrentTextColor(), getResources().getColor(R.color.BLUE_OPAQUE));
    titleColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            sourceTitle.setTextColor((Integer) animation.getAnimatedValue());
        }

    });

    ValueAnimator titleShadowAlphaAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
            AppSettings.getColorFilterInt(appContext), getResources().getColor(android.R.color.transparent));
    titleShadowAlphaAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            sourceTitle.setShadowLayer(4, 0, 0, (Integer) animation.getAnimatedValue());
        }
    });

    ValueAnimator imageOverlayAlphaAnimation = ValueAnimator.ofFloat(imageOverlay.getAlpha(), 0f);
    imageOverlayAlphaAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            imageOverlay.setAlpha((Float) animation.getAnimatedValue());
        }
    });

    int transitionTime = INFO_ANIMATION_TIME;

    DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(1.5f);

    animation.setDuration(transitionTime);
    cardColorAnimation.setDuration(transitionTime);
    titleColorAnimation.setDuration(transitionTime);
    titleShadowAlphaAnimation.setDuration(transitionTime);

    animation.setInterpolator(decelerateInterpolator);
    cardColorAnimation.setInterpolator(decelerateInterpolator);
    titleColorAnimation.setInterpolator(decelerateInterpolator);
    titleShadowAlphaAnimation.setInterpolator(decelerateInterpolator);

    if (imageOverlay.getAlpha() > 0) {
        imageOverlayAlphaAnimation.start();
    }

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (needsListReset) {
                Parcelable state = sourceList.onSaveInstanceState();
                sourceList.setAdapter(null);
                sourceList.setAdapter(listAdapter);
                sourceList.onRestoreInstanceState(state);
                sourceList.setOnItemClickListener(SourceListFragment.this);
                sourceList.setEnabled(true);
                needsListReset = false;
            }
        }
    }, (long) (transitionTime * 1.1f));

    needsListReset = true;
    view.startAnimation(animation);
    cardColorAnimation.start();
    titleColorAnimation.start();
    titleShadowAlphaAnimation.start();
}