Android - getAlpha() from Animated View

不想你离开。 提交于 2019-12-10 21:18:22

问题


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:

  1. 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.

  2. Initialize a Transformation object:

    private Transformation myTransformation;
    mTransformation = new Transformation();
    
  3. Using your code:

    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new DecelerateInterpolator());
    fadeOut.setDuration(350);
    
  4. Get the time at which the drawing of the view hierarchy started (in milliseconds):

    final long time = myView.getDrawingTime();
    
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!