Precise control over Androids VectorDrawable animations

前端 未结 3 643
Happy的楠姐
Happy的楠姐 2020-12-13 05:23

UPDATE: Solution found! Scroll down for my accepted answer!

I want to animate multiple elements of one image and link the animation to ViewPagers po

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 06:00

    I believe you can't do this with the AnimatedVectorDrawable/AnimatedVectorDrawableCompat programmatically.

    A workaround is to use a Level List drawable and animate its levels: but you'll need lots of images for the levels to simulate the smooth transition, I'm using just three levels here for demonstration.

    First you'll define the drawable level list like this:res/drawable/my_level_list.xml

    
    
    
        
    
        
    
    

    Then in the layout file:

    
    

    Then in your activity:

    int MIN_LEVEL=0;
    int MAX_LEVEL=2;
    
    ImageView img=(ImageView)findViewById(R.id.img);
    
    Drawable myLevelSetDrawable= img.getDrawable();
    ObjectAnimator anim=ObjectAnimator.ofInt(myLevelSetDrawable,"level",MIN_LEVEL,MAX_LEVEL);
    anim.setInterpolator(new BounceInterpolator());
    AnimatorSet set=new AnimatorSet();
    set.play(anim);
    set.setDuration(1000);
    set.start();
    

    Hope that helps.

提交回复
热议问题