Rotate an Imagewith Animation

前端 未结 6 1638
死守一世寂寞
死守一世寂寞 2021-02-01 04:23

\"The

What I Have

I have an arrow image (like the left one). Whe

6条回答
  •  生来不讨喜
    2021-02-01 04:38

    Verified Code: (You can follow my solution)

    imageView.setImageResource(R.drawable.ic_arrow_up);
    
    boolean up = true;
    
    if (!up) { 
        up = true; 
        imageView.startAnimation(animate(up)); 
    } else { 
        up = false; 
        imageView.startAnimation(animate(up)); 
    }
    
    private Animation animate(boolean up) {
        Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
        anim.setInterpolator(new LinearInterpolator()); // for smooth animation
        return anim;
    }
    

    drawable/ic_arrow_up.xml

    
        
    
    

    anim/rotate_up.xml

    
    
        
    
    

    anim/rotate_down.xml

    
    
        
    
    

    I used that code. Because this will save the animation state:

    android:fillAfter="true"
    android:fillEnabled="true"
    

提交回复
热议问题