Imitation WeChat photo album rotation arrow - does not return to original position - androi view rotation 180 attribute animation

Imitation WeChat album arrow rotation - does not restore to original position - androi view rotates 180

Insert image description here
Insert image description here

Idea: Use attribute animation to dynamically set the rotation angle of the view

1. Create a rotation animation res/anim/rotate_view.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="0"
        android:repeatMode="restart"
        android:toDegrees="180" />
</rotate>

2、

1) Create ValueAnimator attribute animation-starting angle (0-180)

        valueAnimator = ValueAnimator.ofInt(0, 180);
        valueAnimator.setDuration(300);

2) Add a monitoring key--mainly to reset the rotation angle when the onAnimationEnd animation ends

            @Override
            public void onAnimationEnd(Animator animation) {
                //结束标记位
                if (currentMode == 0) {
                    valueAnimator.setIntValues(180, 360);
                    currentMode = 1;
                } else {
                    valueAnimator.setIntValues(0, 180);
                    currentMode = 0;
                }
            }

3) Set up click monitoring

 valueAnimator.start();

Overall code (view is the view that needs to be rotated)

public class PhoneSelectActivity extends AppCompatActivity implements View.OnClickListener {

    View view;

    int currentMode = 0;
    ValueAnimator valueAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_select);
        view = findViewById(R.id.view1);

        valueAnimator = ValueAnimator.ofInt(0, 180);
        valueAnimator.setDuration(300);
        
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = (int) animation.getAnimatedValue();
                view.setRotation(currentValue);

            }
        });
        view.setOnClickListener(this);
        valueAnimator.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //结束标记位
                if (currentMode == 0) {
                    valueAnimator.setIntValues(180, 360);
                    currentMode = 1;
                } else {
                    valueAnimator.setIntValues(0, 180);
                    currentMode = 0;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    /**
     * 返回上一个界面
     *
     * @param view
     */
    public void gotoBack(View view) {
        finish();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.view1) {
            valueAnimator.start();
        }
    }

Guess you like

Origin blog.csdn.net/qq_38355313/article/details/122244159