ObjectAnimator with scale property makes bg black?

后端 未结 3 1105
青春惊慌失措
青春惊慌失措 2021-01-04 17:16

I use ObjectAnimator to scale down a RelativeLayout :

            ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, \"scaleX\", 0.5f);
            Obj         


        
相关标签:
3条回答
  • 2021-01-04 18:10

    The reason why the previous answer is needing to invalidate the parent is because your AnimatorSet scaleDown has no Target. You should set the Target of your ObjectAnimator's to null and set the Target on the AnimatorSet. Or a better way would be to use the code below:

    ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(view, 
        PropertyValuesHolder.ofFloat("scaleX", 0.5f),
        PropertyValuesHolder.ofFloat("scaleY", 0.5f));
    scaleDown.setDuration(1000);
    scaleDown.start();
    
    0 讨论(0)
  • 2021-01-04 18:14

    It may not be the cleanest solution, but adding animation update listener and invalidating the parent might do the job.

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
    scaleDownX.setDuration(1000);
    scaleDownY.setDuration(1000);
    
    AnimatorSet scaleDown = new AnimatorSet();
    scaleDown.play(scaleDownX).with(scaleDownY);
    
    scaleDownX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator valueAnimator) {
             View p= (View) v.getParent();
             p.invalidate();
         });
    scaleDown.start();
    
    0 讨论(0)
  • 2021-01-04 18:19

    As stated in the comments, the approach for creating an ObjectAnimator is to use the View's static Property object instead of passing the string since using the string value involves reflection for the setter and optionally for the getter in order to derive the starting value of the attribute. This is available from API 14

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, View.SCALE_X, 0.5f);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0.5f);
    

    A complete explanation can be found in DevBytes: Property Animations video by Google's engineer Chet Haase

    0 讨论(0)
提交回复
热议问题