Filipe | 80a5f4c | 2015-08-31 21:28:37 +0000 | [diff] [blame] | 1 | package com.fairphone.fplauncher3.oobe.animation; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.graphics.Canvas; |
| 5 | import android.graphics.Paint; |
| 6 | import android.support.v4.content.ContextCompat; |
| 7 | import android.util.AttributeSet; |
| 8 | import android.util.DisplayMetrics; |
| 9 | import android.view.WindowManager; |
| 10 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 11 | import android.view.animation.AccelerateInterpolator; |
| 12 | import android.view.animation.DecelerateInterpolator; |
| 13 | import android.widget.FrameLayout; |
| 14 | |
| 15 | import com.fairphone.fplauncher3.DynamicGrid; |
| 16 | import com.fairphone.fplauncher3.R; |
| 17 | import com.fairphone.fplauncher3.oobe.utils.KWAnimation; |
| 18 | import com.fairphone.fplauncher3.oobe.utils.KWAnimation.KWValueAnimation; |
| 19 | import com.fairphone.fplauncher3.oobe.utils.KWAnimation.KWValueType; |
| 20 | import com.fairphone.fplauncher3.oobe.utils.KWAnimationGroup; |
| 21 | import com.fairphone.fplauncher3.oobe.utils.KWAnimationGroup.KWAnimationGroupListener; |
| 22 | import com.fairphone.fplauncher3.oobe.utils.KWAnimationManager; |
| 23 | import com.fairphone.fplauncher3.oobe.utils.KWSprite; |
| 24 | |
| 25 | /** |
| 26 | * Created by kwamecorp on 7/22/15. |
| 27 | */ |
| 28 | public class CameraTutorialAnimationView extends FrameLayout { |
| 29 | |
Filipe | 80a5f4c | 2015-08-31 21:28:37 +0000 | [diff] [blame] | 30 | public interface CameraTutorialAnimationViewListener { |
| 31 | public void OnAnimationFinished(CameraTutorialAnimationView view); |
| 32 | } |
| 33 | |
| 34 | private int SCREEN_WIDTH = 540; |
| 35 | private int SCREEN_HEIGHT = 960; |
| 36 | |
| 37 | Paint spritePaint; |
| 38 | |
| 39 | KWSprite spriteRoot; |
| 40 | |
| 41 | KWSprite spriteArrow; |
| 42 | KWSprite spriteCircle; |
| 43 | KWSprite spriteCameraIcon; |
| 44 | |
| 45 | KWAnimationGroup animationGroupCircle; |
| 46 | KWAnimationGroup animationGroupArrowPoint; |
| 47 | KWAnimationManager animationManager; |
| 48 | |
| 49 | private DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(); |
| 50 | private AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator(); |
| 51 | private AccelerateDecelerateInterpolator accelerateDecelerateInterpolator = new AccelerateDecelerateInterpolator(); |
| 52 | |
| 53 | CameraTutorialAnimationViewListener listener; |
| 54 | private DisplayMetrics mDisplayMetrics; |
| 55 | |
| 56 | public CameraTutorialAnimationView(Context context) { |
| 57 | super(context); |
| 58 | init(); |
| 59 | } |
| 60 | |
| 61 | public CameraTutorialAnimationView(Context context, AttributeSet attrs) { |
| 62 | super(context, attrs); |
| 63 | init(); |
| 64 | } |
| 65 | |
| 66 | public CameraTutorialAnimationView(Context context, AttributeSet attrs, int defStyle) { |
| 67 | super(context, attrs, defStyle); |
| 68 | init(); |
| 69 | } |
| 70 | |
| 71 | void setCameraTutorialAnimationViewListener(CameraTutorialAnimationViewListener listener) { |
| 72 | this.listener = listener; |
| 73 | } |
| 74 | |
| 75 | private void init() { |
| 76 | |
| 77 | WindowManager windowMananger = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE); |
| 78 | mDisplayMetrics = new DisplayMetrics(); |
| 79 | windowMananger.getDefaultDisplay().getMetrics(mDisplayMetrics); |
| 80 | |
| 81 | SCREEN_HEIGHT=mDisplayMetrics.heightPixels; |
| 82 | SCREEN_WIDTH=mDisplayMetrics.widthPixels; |
| 83 | |
| 84 | if (getBackground() == null) { |
| 85 | setBackgroundColor(0x00000000); |
| 86 | } |
| 87 | |
| 88 | spritePaint = new Paint(); |
| 89 | spritePaint.setColor(0xffffffff); |
| 90 | |
| 91 | spriteRoot = new KWSprite(); |
| 92 | spriteArrow = new KWSprite(); |
| 93 | spriteCameraIcon = new KWSprite(); |
| 94 | spriteCircle = new KWSprite(); |
| 95 | |
| 96 | spriteArrow.drawable = ContextCompat.getDrawable(getContext(), R.drawable.tutorial_hw_button_arrow); |
| 97 | spriteArrow.applySizeFromDrawable(); |
| 98 | spriteCircle.drawable = ContextCompat.getDrawable(getContext(), R.drawable.tutorial_camera_bg_circle); |
| 99 | spriteCircle.applySizeFromDrawable(); |
| 100 | spriteCameraIcon.drawable = ContextCompat.getDrawable(getContext(), R.drawable.tutorial_camera_icon); |
| 101 | spriteCameraIcon.applySizeFromDrawable(); |
| 102 | |
| 103 | spriteRoot.addChild(spriteArrow); |
| 104 | spriteRoot.addChild(spriteCircle); |
| 105 | spriteRoot.addChild(spriteCameraIcon); |
| 106 | |
| 107 | spriteRoot.alpha = 0; |
| 108 | |
| 109 | spriteCameraIcon.alpha = 0; |
| 110 | spriteArrow.alpha = 0; |
| 111 | |
| 112 | |
| 113 | animationManager = new KWAnimationManager(); |
| 114 | animationGroupCircle = new KWAnimationGroup(animationManager); |
| 115 | animationGroupArrowPoint = new KWAnimationGroup(animationManager); |
| 116 | |
Filipe | 80a5f4c | 2015-08-31 21:28:37 +0000 | [diff] [blame] | 117 | setupCircleAnimation(); |
| 118 | setupArrowAnimation(); |
| 119 | } |
| 120 | |
| 121 | public void playCircleAnimation() { |
| 122 | animationGroupCircle.start(); |
| 123 | } |
| 124 | |
| 125 | public void playArrowAnimation() { |
| 126 | animationGroupArrowPoint.start(); |
| 127 | } |
| 128 | |
| 129 | public void stopAnimations() { |
| 130 | animationGroupCircle.stop(); |
| 131 | animationGroupArrowPoint.stop(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 136 | super.onLayout(changed, left, top, right, bottom); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected void onDraw(Canvas canvas) { |
| 141 | spriteRoot.resetMatrix(); |
| 142 | spriteArrow.draw(canvas, spritePaint); |
| 143 | spriteCameraIcon.draw(canvas, spritePaint); |
| 144 | spriteCircle.draw(canvas, spritePaint); |
| 145 | animationManager.update(); |
| 146 | postInvalidate(); |
| 147 | } |
| 148 | |
| 149 | private void setupCircleAnimation() { |
| 150 | KWAnimation rootAnimation = new KWAnimation(spriteRoot); |
| 151 | |
| 152 | KWValueAnimation alphaAnim = rootAnimation.addValueAnimation(KWValueType.Alpha); |
| 153 | alphaAnim.addKeyframe(0, 0, null); |
| 154 | alphaAnim.addKeyframe(1, 500, decelerateInterpolator); |
| 155 | |
| 156 | KWAnimation circleAnimation = new KWAnimation(spriteCircle); |
| 157 | KWValueAnimation scaleAnim = circleAnimation.addValueAnimation(KWValueType.Scale); |
| 158 | scaleAnim.addKeyframe(0f, 700, null); |
| 159 | scaleAnim.addKeyframe(1.1f, 1000, decelerateInterpolator); |
| 160 | scaleAnim.addKeyframe(1.0f, 1150, decelerateInterpolator); |
| 161 | alphaAnim = circleAnimation.addValueAnimation(KWValueType.Alpha); |
| 162 | alphaAnim.addKeyframe(0, 0, null); |
| 163 | alphaAnim.addKeyframe(1.0f, 650, null); |
| 164 | |
| 165 | KWAnimation cameraIconAnimation = new KWAnimation(spriteCameraIcon); |
| 166 | scaleAnim = cameraIconAnimation.addValueAnimation(KWValueType.Scale); |
| 167 | scaleAnim.addKeyframe(0, 1200, null); |
| 168 | scaleAnim.addKeyframe(1.1f, 1400, decelerateInterpolator); |
| 169 | scaleAnim.addKeyframe(1.0f, 1550, decelerateInterpolator); |
| 170 | alphaAnim = cameraIconAnimation.addValueAnimation(KWValueType.Alpha); |
| 171 | //alphaAnim.addKeyframe(0, 1600, null); |
| 172 | alphaAnim.addKeyframe(1.0f, 1200, null); |
| 173 | |
| 174 | KWAnimation arrowSwipeAnimation = new KWAnimation(spriteArrow); |
| 175 | KWValueAnimation widthAnim = arrowSwipeAnimation.addValueAnimation(KWValueType.Width); |
| 176 | alphaAnim = arrowSwipeAnimation.addValueAnimation(KWValueType.Alpha); |
| 177 | //alphaAnim.addKeyframe(0, 2000, null); |
| 178 | alphaAnim.addKeyframe(1.0f, 1700, null); |
| 179 | |
| 180 | widthAnim.addKeyframe((0), 1800, null); |
| 181 | widthAnim.addKeyframe((spriteArrow.width + 10), 2200, accelerateDecelerateInterpolator); |
| 182 | widthAnim.addKeyframe((spriteArrow.width - 110), 2500, accelerateDecelerateInterpolator); |
| 183 | widthAnim.addKeyframe((spriteArrow.width - 100), 2600, accelerateDecelerateInterpolator); |
| 184 | |
| 185 | |
| 186 | animationGroupCircle.addAnimation(rootAnimation); |
| 187 | animationGroupCircle.addAnimation(circleAnimation); |
| 188 | animationGroupCircle.addAnimation(cameraIconAnimation); |
| 189 | animationGroupCircle.addAnimation(arrowSwipeAnimation); |
| 190 | |
| 191 | animationGroupCircle.setAnimationGroupListener(new KWAnimationGroupListener() { |
| 192 | @Override |
| 193 | public void onAnimationGroupStarted(KWAnimationGroup group) { |
| 194 | spriteRoot.clearTransform(true); |
| 195 | spriteRoot.alpha = 0; |
| 196 | spriteArrow.alpha = 0; |
| 197 | spriteCircle.alpha = 0; |
| 198 | spriteCameraIcon.alpha = 0; |
| 199 | |
| 200 | spriteArrow.y = (SCREEN_HEIGHT - 650); |
Filipe | 50b0171 | 2015-12-01 16:31:59 +0000 | [diff] [blame] | 201 | spriteArrow.x = (SCREEN_WIDTH / 2) + (spriteCircle.drawable.getIntrinsicWidth()/2); |
Filipe | 80a5f4c | 2015-08-31 21:28:37 +0000 | [diff] [blame] | 202 | spriteArrow.pivotY = 0.5f; |
| 203 | |
| 204 | spriteCircle.y = (SCREEN_HEIGHT - 650); |
| 205 | spriteCircle.x = SCREEN_WIDTH / 2; |
| 206 | spriteCircle.pivotX = 0.5f; |
| 207 | spriteCircle.pivotY = 0.5f; |
| 208 | |
| 209 | spriteCameraIcon.y = (SCREEN_HEIGHT - 650); |
| 210 | spriteCameraIcon.x = SCREEN_WIDTH / 2; |
| 211 | spriteCameraIcon.pivotX = 0.5f; |
| 212 | spriteCameraIcon.pivotY = 0.5f; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void onAnimationGroupFinished(KWAnimationGroup group) { |
| 217 | if (listener != null) { |
| 218 | listener.OnAnimationFinished(CameraTutorialAnimationView.this); |
| 219 | } |
| 220 | } |
| 221 | }); |
| 222 | |
| 223 | } |
| 224 | |
| 225 | private void setupArrowAnimation() { |
| 226 | |
| 227 | KWAnimation arrowSwipeAnimation = new KWAnimation(spriteArrow); |
| 228 | KWValueAnimation widthAnim = arrowSwipeAnimation.addValueAnimation(KWValueType.Width); |
| 229 | widthAnim.addKeyframe((spriteArrow.width - 100), 2100, null); |
| 230 | widthAnim.addKeyframe((spriteArrow.width + 20), 2400, accelerateInterpolator); |
| 231 | widthAnim.addKeyframe((spriteArrow.width - 110), 2700, decelerateInterpolator); |
| 232 | widthAnim.addKeyframe((spriteArrow.width - 100), 2800, accelerateInterpolator); |
| 233 | |
| 234 | animationGroupArrowPoint.addAnimation(arrowSwipeAnimation); |
| 235 | |
| 236 | animationGroupArrowPoint.setAnimationGroupListener(new KWAnimationGroupListener() { |
| 237 | @Override |
| 238 | public void onAnimationGroupStarted(KWAnimationGroup group) { |
| 239 | |
| 240 | spriteArrow.y = (SCREEN_HEIGHT - 650); |
Filipe | 50b0171 | 2015-12-01 16:31:59 +0000 | [diff] [blame] | 241 | spriteArrow.x = (SCREEN_WIDTH / 2) + (spriteCircle.drawable.getIntrinsicWidth()/2);// - DynamicGrid.pxFromDp(110, mDisplayMetrics); |
Filipe | 80a5f4c | 2015-08-31 21:28:37 +0000 | [diff] [blame] | 242 | spriteArrow.pivotY = 0.5f; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void onAnimationGroupFinished(KWAnimationGroup group) { |
| 247 | if (listener != null) { |
| 248 | listener.OnAnimationFinished(CameraTutorialAnimationView.this); |
| 249 | } |
| 250 | } |
| 251 | }); |
| 252 | } |
| 253 | |
| 254 | } |