所谓转场动画,通俗的讲就是一个Activity跳转到另一个Activity是的动画。
Activity的转场动画很早就有了,5.0之前用的是overridePendingTransition()这个方法。在5.0之后,Google使用Material Design设计风格,进而有了的新的转场转场动画的诞生,效果还是挺炫酷的,下面我们先看下效果。
startActivity(Intent(this@BeforeActivity,BeforeTwoActivity::class.java)
//Fade(淡入淡出)
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
//Slide(左右交错)
//overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
finish()
//Fade(淡入淡出)
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
//Slide(左右交错)
//overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
其实这里主要是靠overridePendingTransition(int enterAnim, int exitAnim)来加载动画,顾名思义第一个参数是进场动画,第二个是出场动画。 这几个效果是可以交互使用,比如进场用Fade效果,出场用SLide效果。也可以使用自定义的效果,这里不细说。当然5.0后了类似共享元素这类的效果那就另当别论了
说到这里不得不说Google在5.0MD设计中给我提供全新的过度动画ActivityOptions,以及兼容包ActivityOptionsCompat.下面来说一说它提供几种过度效果的方法
这个方法其实和上面的overridePendingTransition()方法使用其实差不多,第二个参数是进场动画,第三个是出场动画。(效果参考overridePendingTransition()) 使用如下:
ActivityCompat.startActivity(Intent(this@BeforeActivity,BeforeTwoActivity::class.java),
ActivityOptionsCompat.makeCustomAnimation(this@BeforeActivity,
android.R.anim.fade_in, android.R.anim.fade_out).toBundle())
####finish的时候:
ActivityCompat.finishAfterTransition(this)
public boolean startExitBackTransition(final Activity activity) {
if (mEnteringNames == null || mCalledExitCoordinator != null) {
return false;
} else {
...
}
}
/**
* The shared elements that the calling Activity has said that they transferred to this
* Activity.
* 调用Activity的共享元素表示已转移到此Activity(请不要介意这个机翻,凑和看)
*/
private ArrayList<String> mEnteringNames;
ActivityOptionsCompat makeScaleUpAnimation(View source,int startX, int startY, int startWidth, int startHeight)
val options = ActivityOptionsCompat.makeScaleUpAnimation(view,view.width/2,view.height/2,
0, 0)
ActivityCompat.startActivity(this@AfterActivity,Intent(this@AfterActivity,AfterTwoActivity::class.java),options.toBundle())
ActivityCompat.finishAfterTransition(this)
ActivityOptionsCompat makeScaleUpAnimation(View source,int startX, int startY, int startWidth, int startHeight)
val options = ActivityOptionsCompat.makeScaleUpAnimation(view,view.width/2,view.height/2,
0, 0)
ActivityCompat.startActivity(this@AfterActivity,Intent(this@AfterActivity,AfterTwoActivity::class.java),options.toBundle())
ActivityCompat.finishAfterTransition(this)
ActivityOptionsCompat makeThumbnailScaleUpAnimation(View source,Bitmap thumbnail, int startX, int startY)
var bitmap = BitmapFactory.decodeResource(resources,effect.uri)
val options = ActivityOptionsCompat.makeThumbnailScaleUpAnimation(view, bitmap,
view.width/2, view.height/2)
ActivityCompat.startActivity(this@AfterActivity,Intent(this@AfterActivity,AfterTwoActivity::class.java),options.toBundle())
ActivityCompat.finishAfterTransition(this)
ActivityOptionsCompat makeClipRevealAnimation(View source,int startX, int startY, int width, int height)
val options = ActivityOptionsCompat.makeClipRevealAnimation(view,view.width/2,
view.height/2,0, 0)
ActivityCompat.startActivity(this@AfterActivity,Intent(this@AfterActivity,AfterTwoActivity::class.java),options.toBundle())
ActivityCompat.finishAfterTransition(this)
####ActivityOptions CompatmakeSceneTransitionAnimation(Activity activity,Pair<View, String>... sharedElements)
startActivity(Intent(this@AfterActivity, AfterTwoActivity::class.java),
ActivityOptionsCompat.makeSceneTransitionAnimation(this@AfterActivity).toBundle())
//Explode
window.enterTransition = Explode()
window.exitTransition = Explode()
//Slide
window.enterTransition = Slide()
window.exitTransition = Slide()
//Fade
window.enterTransition = Fade()
window.exitTransition = Fade()
-
1.在A和B的布局中为需要进行过渡效果的View设置两个相同的 android:transitionName = “标识名称”
-
2.1 在startActivity的时候(适用单个view过渡):
//第二参数传入过渡的view,第三个参数传入 android:transitionName 标识名称 startActivity(Intent(this@ShareElementActivity, ShareElementTwoActivity::class.java), ActivityOptions.makeSceneTransitionAnimation(this@ShareElementActivity,view,"shareImg").toBundle())
-
2.2 在startActivity的时候(适用多个个view过渡):
//其实就是把过个需要过渡的View集合起来 var one = android.support.v4.util.Pair<View, String>(img5, "shareImg5") var two = android.support.v4.util.Pair<View, String>(img6, "shareImg6") var pairs = arrayOf(one,two) val transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *pairs) startActivity(Intent(this@ShareElementActivity, ShareElementThreeActivity::class.java), transitionActivityOptions.toBundle())