Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chromoting; | |
| 6 | |
| 7 import android.graphics.Color; | |
| 8 import android.graphics.Paint; | |
| 9 import android.graphics.Point; | |
| 10 import android.graphics.RadialGradient; | |
| 11 import android.graphics.Shader; | |
| 12 import android.os.SystemClock; | |
| 13 | |
| 14 /** Helper class for displaying the press feedback animations. */ | |
|
Lambros
2016/06/01 23:51:36
Is this class now single-threaded?
Hzj_jie
2016/06/02 02:21:37
Yes, this class is not necessarily thread-safe. Si
| |
| 15 public final class FeedbackAnimator | |
| 16 implements Event.ParameterCallback<Boolean, EventParameters.Paint> { | |
| 17 /** Total duration of the animation, in milliseconds. */ | |
| 18 private static final float TOTAL_DURATION_MS = 220; | |
| 19 | |
| 20 /** Start time of the animation, from {@link SystemClock#uptimeMillis()}. */ | |
| 21 private final long mStartTimeInMs; | |
| 22 | |
| 23 /** Contains the size of the feedback animation for the most recent request. */ | |
| 24 private final float mFeedbackSizeInPixels; | |
| 25 | |
| 26 private final Paint mPaint = new Paint(); | |
| 27 | |
| 28 private final Point mPos; | |
| 29 | |
| 30 private FeedbackAnimator(float feedbackSizeInPixels, Point pos) { | |
| 31 mStartTimeInMs = SystemClock.uptimeMillis(); | |
| 32 mFeedbackSizeInPixels = feedbackSizeInPixels; | |
| 33 mPos = pos; | |
| 34 } | |
| 35 | |
| 36 /** Begins a new animation sequence at position (|x|, |y|). */ | |
|
Lambros
2016/06/01 23:51:36
position |pos|.
Hzj_jie
2016/06/02 02:21:37
Done.
| |
| 37 public static void startAnimation(DesktopView view, | |
| 38 Point pos, | |
| 39 DesktopView.InputFeedbackType feedbackType ) { | |
| 40 Preconditions.notNull(view); | |
|
Lambros
2016/06/01 23:51:36
Remove this?
If view == null, then view.onPaint()
Hzj_jie
2016/06/02 02:21:37
My fault, I recently wrote more C++ code than Java
| |
| 41 if (feedbackType == DesktopView.InputFeedbackType.NONE) { | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 view.onPaint().addSelfRemovable(new FeedbackAnimator( | |
| 46 getInputFeedbackSizeInPixels(feedbackType), pos)); | |
| 47 } | |
| 48 | |
| 49 private static float getInputFeedbackSizeInPixels(DesktopView.InputFeedbackT ype feedbackType) { | |
| 50 switch (feedbackType) { | |
| 51 case SMALL_ANIMATION: | |
| 52 return 40.0f; | |
| 53 | |
| 54 case LARGE_ANIMATION: | |
| 55 return 160.0f; | |
| 56 | |
| 57 default: | |
| 58 // Unreachable, but required by Google Java style and findbugs. | |
| 59 assert false : "Unreached"; | |
| 60 return 0.0f; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 @Override | |
| 65 public Boolean run(EventParameters.Paint parameter) { | |
| 66 float elapsedTimeInMs = SystemClock.uptimeMillis() - mStartTimeInMs; | |
| 67 if (elapsedTimeInMs < 1) { | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 // |progress| is 0 at the beginning, 1 at the end. | |
| 72 float progress = elapsedTimeInMs / TOTAL_DURATION_MS; | |
| 73 if (progress >= 1) { | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 float size = mFeedbackSizeInPixels / parameter.scale; | |
| 78 // Animation grows from 0 to |size|, and goes from fully opaque to trans parent for a | |
|
Lambros
2016/06/01 23:51:36
Blank line above comment.
Hzj_jie
2016/06/02 02:21:37
Done.
| |
| 79 // seamless fading-out effect. The animation needs to have more than one color so it's | |
| 80 // visible over any background color. | |
| 81 float radius = size * progress; | |
| 82 int alpha = (int) ((1 - progress) * 0xff); | |
| 83 | |
| 84 int transparentBlack = Color.argb(0, 0, 0, 0); | |
| 85 int white = Color.argb(alpha, 0xff, 0xff, 0xff); | |
| 86 int black = Color.argb(alpha, 0, 0, 0); | |
| 87 mPaint.setShader(new RadialGradient(mPos.x, mPos.y, radius, | |
| 88 new int[] {transparentBlack, white, black, transparentBlack}, | |
| 89 new float[] {0.0f, 0.8f, 0.9f, 1.0f}, Shader.TileMode.CLAMP)); | |
| 90 parameter.canvas.drawCircle(mPos.x, mPos.y, radius, mPaint); | |
| 91 return true; | |
| 92 } | |
| 93 } | |
| OLD | NEW |