之前用为知笔记,今年开始收费了,其它几家笔记在WIN10上联网有点问题。以后就在这里记点笔记吧,
首先sdk最小版本
minSdkVersion 21
简单的转场过渡动画分为三种
Slide,Fade,Explode,滑动,渐变,爆炸三种
1.先初始化动画
private void setupWindowAnimations() { // Re-enter transition is executed when returning to this activity Slide slideTransition = new Slide(); slideTransition.setSlideEdge(Gravity.LEFT); slideTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long)); getWindow().setEnterTransition(slideTransition);//设置进入动画 getWindow().setExitTransition(slideTransition);//设置退出动画 getWindow().setReturnTransition(slideTransition);//设置返回的动画(这个没具体试过) getWindow().setReenterTransition(slideTransition);//设置重新进入的动画 }
无论是设置单纯的Activity转场,还是设置某个控件转场,都需要一个新类
Pair, String>[]
类。
这是一个容器类,用来标记被共享和共享页面之间的关系。
几种用法:
1.简单activity转场(sample参数为 intent跳转携带的参数)
private void transitionToActivity(Class target, Sample sample) { final Pair, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, true); startActivity(target, pairs, sample); }
2.指定某个控件的共享元素(createSafeTransitionParticipants第三个参数里面指定两个参数(本页面共享的控件,共享标识))
private void transitionToActivity(Class target, SamplesViewHolder viewHolder, Sample sample) { final Pair, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, false, new Pair<>(viewHolder.binding.sampleIcon, activity.getString(R.string.square_blue_name)), new Pair<>(viewHolder.binding.sampleName, activity.getString(R.string.sample_blue_title))); startActivity(target, pairs, sample); }
startActivity跳转中
使用
ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pairs);
创建opationscompat对象放进startActivity中
private void startActivity(Class target, Pair, String>[] pairs, Sample sample) { Intent i = new Intent(activity, target); ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pairs); i.putExtra("sample", sample); activity.startActivity(i, transitionActivityOptions.toBundle()); }
跳转完成查看效果。