1/* 2* Copyright 2013 The Android Open Source Project 3* 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16 17package com.example.android.batchstepsensor.cardstream; 18 19import android.animation.ObjectAnimator; 20import android.animation.PropertyValuesHolder; 21import android.annotation.TargetApi; 22import android.content.Context; 23import android.graphics.Point; 24import android.os.Build; 25import android.view.View; 26import android.view.WindowManager; 27import android.view.animation.BounceInterpolator; 28 29class DefaultCardStreamAnimator extends CardStreamAnimator { 30 31 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 32 @Override 33 public ObjectAnimator getDisappearingAnimator(Context context){ 34 35 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(new Object(), 36 PropertyValuesHolder.ofFloat("alpha", 1.f, 0.f), 37 PropertyValuesHolder.ofFloat("scaleX", 1.f, 0.f), 38 PropertyValuesHolder.ofFloat("scaleY", 1.f, 0.f), 39 PropertyValuesHolder.ofFloat("rotation", 0.f, 270.f)); 40 41 animator.setDuration((long) (200 * mSpeedFactor)); 42 return animator; 43 } 44 45 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 46 @Override 47 public ObjectAnimator getAppearingAnimator(Context context){ 48 49 final Point outPoint = new Point(); 50 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 51 wm.getDefaultDisplay().getSize(outPoint); 52 53 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(new Object(), 54 PropertyValuesHolder.ofFloat("alpha", 0.f, 1.f), 55 PropertyValuesHolder.ofFloat("translationY", outPoint.y / 2.f, 0.f), 56 PropertyValuesHolder.ofFloat("rotation", -45.f, 0.f)); 57 58 animator.setDuration((long) (200 * mSpeedFactor)); 59 return animator; 60 } 61 62 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 63 @Override 64 public ObjectAnimator getInitalAnimator(Context context){ 65 66 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(new Object(), 67 PropertyValuesHolder.ofFloat("alpha", 0.5f, 1.f), 68 PropertyValuesHolder.ofFloat("rotation", 60.f, 0.f)); 69 70 animator.setDuration((long) (200 * mSpeedFactor)); 71 return animator; 72 } 73 74 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 75 @Override 76 public ObjectAnimator getSwipeInAnimator(View view, float deltaX, float deltaY){ 77 78 float deltaXAbs = Math.abs(deltaX); 79 80 float fractionCovered = 1.f - (deltaXAbs / view.getWidth()); 81 long duration = Math.abs((int) ((1 - fractionCovered) * 200 * mSpeedFactor)); 82 83 // Animate position and alpha of swiped item 84 85 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, 86 PropertyValuesHolder.ofFloat("alpha", 1.f), 87 PropertyValuesHolder.ofFloat("translationX", 0.f), 88 PropertyValuesHolder.ofFloat("rotationY", 0.f)); 89 90 animator.setDuration(duration).setInterpolator(new BounceInterpolator()); 91 92 return animator; 93 } 94 95 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 96 @Override 97 public ObjectAnimator getSwipeOutAnimator(View view, float deltaX, float deltaY){ 98 99 float endX; 100 float endRotationY; 101 102 float deltaXAbs = Math.abs(deltaX); 103 104 float fractionCovered = 1.f - (deltaXAbs / view.getWidth()); 105 long duration = Math.abs((int) ((1 - fractionCovered) * 200 * mSpeedFactor)); 106 107 endX = deltaX < 0 ? -view.getWidth() : view.getWidth(); 108 if (deltaX > 0) 109 endRotationY = -15.f; 110 else 111 endRotationY = 15.f; 112 113 // Animate position and alpha of swiped item 114 return ObjectAnimator.ofPropertyValuesHolder(view, 115 PropertyValuesHolder.ofFloat("alpha", 0.f), 116 PropertyValuesHolder.ofFloat("translationX", endX), 117 PropertyValuesHolder.ofFloat("rotationY", endRotationY)).setDuration(duration); 118 119 } 120 121} 122