仿微信相册箭头旋转 - 不恢复原位 - androi view旋转180
思路:利用属性动画,动态设置view的旋转角度
1 、创建旋转动画 res/anim/rotate_view.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"><rotateandroid:duration="1000"android:fromDegrees="0"android:pivotX="50%"android:pivotY="50%"android:repeatCount="0"android:repeatMode="restart"android:toDegrees="180" />
</rotate>
2、
1)创建 ValueAnimator 属性动画 -起始角度(0-180)
valueAnimator = ValueAnimator.ofInt(0, 180);valueAnimator.setDuration(300);
2)添加监听 关键-- 主要是在onAnimationEnd 动画结束的时候重新设置旋转的角度
@Overridepublic void onAnimationEnd(Animator animation) {//结束标记位if (currentMode == 0) {valueAnimator.setIntValues(180, 360);currentMode = 1;} else {valueAnimator.setIntValues(0, 180);currentMode = 0;}}
3) 设置点击监听即可
valueAnimator.start();
整体代码 (view 即需要旋转的view)
public class PhoneSelectActivity extends AppCompatActivity implements View.OnClickListener {View view;int currentMode = 0;ValueAnimator valueAnimator;@Overrideprotected 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() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {int currentValue = (int) animation.getAnimatedValue();view.setRotation(currentValue);}});view.setOnClickListener(this);valueAnimator.addListener(new Animator.AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {//结束标记位if (currentMode == 0) {valueAnimator.setIntValues(180, 360);currentMode = 1;} else {valueAnimator.setIntValues(0, 180);currentMode = 0;}}@Overridepublic void onAnimationCancel(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}});}/*** 返回上一个界面** @param view*/public void gotoBack(View view) {finish();}@Overridepublic void onClick(View v) {if (v.getId() == R.id.view1) {valueAnimator.start();}}