问题
I have an animation like this:
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new DecelerateInterpolator());
fadeOut.setDuration(350);
myView.startAnimation(fadeOut);
I am trying is to get its Alpha durring the Animation as such:
System.out.println(myView.getAlpha());
However, it always returns "1.0" throughout the animation. How can I obtain the actual alpha value of myView
during the animation process?
回答1:
By using Transformation
with your AlphaAnimation
, you may be able to retrieve alpha value directly from your AlphaAnimation
object instead of your View
.
Purpose of using Transformation:
Defines the transformation to be applied at one point in time of an Animation.
Steps:
Create a new
Transformation.java
class as shown in https://sourcegraph.com/android.googlesource.com/platform/frameworks/base/-/blob/core/java/android/view/animation/Transformation.java.Initialize a
Transformation
object:private Transformation myTransformation; mTransformation = new Transformation();
Using your code:
Animation fadeOut = new AlphaAnimation(1, 0); fadeOut.setInterpolator(new DecelerateInterpolator()); fadeOut.setDuration(350);
Get the time at which the drawing of the view hierarchy started (in milliseconds):
final long time = myView.getDrawingTime();
Get the transformation to apply at a specified point in time. Finally, obtain the actual alpha value during the animation:
fadeOut.getTransformation(time, myTransformation); final float myAlphaValue = myTransformation.getAlpha();
Resources: https://sourcegraph.com/android.googlesource.com/platform/frameworks/base/-/blob/core/java/android/widget/ProgressBar.java#L1699 https://developer.android.com/reference/android/view/animation/Animation.html https://developer.android.com/reference/android/view/animation/Transformation.html
来源:https://stackoverflow.com/questions/38435423/android-getalpha-from-animated-view