android翻页动画

来源:互联网 发布:淘宝价格变化 编辑:程序博客网 时间:2024/05/21 08:37

先上代码:

public class MainActivity extends Activity implements View.OnClickListener {    private static final int DURATION = 400;    View mFrontView;    View mBackView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mFrontView = findViewById(R.id.front);        mFrontView.setOnClickListener(this);        mBackView = findViewById(R.id.back);        mBackView.setOnClickListener(this);    }    @Override    public void onClick(View view) {        final int id = view.getId();        if (id == R.id.front) flip(mFrontView, mBackView, DURATION);        else if (id == R.id.back) flip(mBackView, mFrontView, DURATION);    }    public void flip(final View front, final View back, final int duration) {        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {            AnimatorSet set = new AnimatorSet();            set.playSequentially(                ObjectAnimator.ofFloat(front, "rotationY", 90).setDuration(duration / 2),                ObjectAnimator.ofInt(front, "visibility", View.GONE).setDuration(0),                ObjectAnimator.ofFloat(back, "rotationY", -90).setDuration(0),                ObjectAnimator.ofInt(back, "visibility", View.VISIBLE).setDuration(0),                ObjectAnimator.ofFloat(back, "rotationY", 0).setDuration(duration / 2)            );            set.start();        }        else {            front.animate().rotationY(90).setDuration(duration / 2).setListener(new AnimatorListenerAdapter() {                @Override                public void onAnimationEnd(Animator animation) {                    front.setVisibility(View.GONE);                    back.setRotationY(-90);                    back.setVisibility(View.VISIBLE);                    back.animate().rotationY(0).setDuration(duration / 2).setListener(null);                }            });        }    }}
代码解析:

ObjectAnimator.ofFloat(front, "rotationY", 90).setDuration(duration / 2)
就是front对象执行沿着Y轴方向,旋转90度,耗时duration/2
ObjectAnimator.ofInt(front, "visibility", View.GONE).setDuration(0)
就是front对象执行可见动画,变为不可见,耗时0秒

front.animate().rotationY(90).setDuration(duration / 2).setListener(new AnimatorListenerAdapter() {                @Override                public void onAnimationEnd(Animator animation) {                    front.setVisibility(View.GONE);                    back.setRotationY(-90);                    back.setVisibility(View.VISIBLE);                    back.animate().rotationY(0).setDuration(duration / 2).setListener(null);                }            });
这个的意思是:

front在旋转90度之后,将front变为不可见,然后back的角度调整(右边向外转90度),然后变为可见,最后旋转到0度

0 0
原创粉丝点击