先说一下在XML中定义动画的类型,分别有:
alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动画效果
在XML中定义动画上面的动画类型可以随意组合,达到想要的效果,下面是代码片段:
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<!-- 渐变透明度动画效果 -->
<alpha
android:duration=
"1000"
android:fromAlpha=
"0.0"
android:toAlpha=
"1.0" />
<!--
fromAlpha:开始时透明度
toAlpha: 结束时透明度
duration:动画持续时间 -->
</set>
<!-- 画面转移旋转动画效果 -->
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees=
"0"
android:pivotX=
"100%"
android:pivotY=
"0"
android:toDegrees=
"360"
android:duration =
"1000"/>
<!--
fromDegrees 动画开始时的角度
toDegrees 动画结束时物件的旋转角度,正代表顺时针
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置 -->
</set>
<!-- 渐变尺寸伸缩动画效果 -->
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta=
"0"
android:fromYDelta=
"0"
android:toXDelta=
"-100%"
android:toYDelta=
"0"
android:duration =
"500"/>
<!--fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是-->
<!--toXDelta, toYDelta 动画结束时X,Y的座标 –>-->
</set>
<!-- 画面转换位置移动动画效果 -->
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<scale
android:interpolator=
"@android:anim/decelerate_interpolator"
android:fromXScale=
"0.0"
android:toXScale=
"1.5"
android:fromYScale=
"0.0"
android:toYScale=
"1.5"
android:pivotX=
"50%"
android:pivotY=
"50%"
android:startOffset=
"0"
android:duration=
"10000"
android:repeatCount=
"1"
android:repeatMode=
"reverse"
/>
</set>
<!--
interpolator指定动画插入器,常见的有加速减速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,减速插入器decelerate_interpolator。
fromXScale,fromYScale,动画开始前X,Y的缩放,
0.0为不显示,
1.0为正常大小
toXScale,toYScale,动画最终缩放的倍数,
1.0为正常大小,大于
1.0放大
pivotX,pivotY动画起始位置,相对于屏幕的百分比,两个都为
50%表示动画从屏幕中间开始
startOffset,动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
repeatCount,动画重复的计数,动画将会执行该值+
1次
repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
-->
第一种跳转页面的方法,在XML中定义两个动画,一个是页面进入的动画,一个是页面退出动画,实现方式如下:
写一个进入动画 alpha_in
<alpha
android:duration=
"1000"
android:fromAlpha=
"0.0"
android:toAlpha=
"1.0" />
alpha_out
<alpha
<alpha
android:duration=
"1000"
android:fromAlpha=
"1.0"
android:toAlpha=
"0.0" />
在java代码中实现方式:
startActivity(
new Intent(A.
this, B.class)); overridePendingTransition(R.anim.alpha_into,R.anim.alpha_out);
这种实现能在跳转的时候实现动画,当返回页面时候就没有动画了。
第二种跳转页面的方法,在XML中定义四个,
第一个是打开页面的动画 translate_into 相当于A跳转B,B的动画效果
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta=
"100%"
android:fromYDelta=
"0"
android:toXDelta=
"0"
android:toYDelta=
"0"
android:duration =
"500"
/>
</set>
第二个XML translate_out 相当于A跳转B,A的动画效果
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta=
"0"
android:fromYDelta=
"0"
android:toXDelta=
"-100%"
android:toYDelta=
"0"
android:duration =
"500"/>
</set>
第三个XML translate_close_into 相当于A跳转B后,从B返回到A时候A的动画效果
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta=
"-100%"
android:fromYDelta=
"0"
android:toXDelta=
"0"
android:toYDelta=
"0"
android:duration =
"500"/>
</set>
第四个XML translate_close_out相当于A跳转B后,从B返回到A时候B的动画效果
<?xml version=
"1.0" encoding=
"utf-8"?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta=
"0"
android:fromYDelta=
"0"
android:toXDelta=
"100%"
android:toYDelta=
"0"
android:duration =
"500"/>
</set>
在styles中定义:
<style name=
"Anim_fade" parent=
"android:Theme">
<item name=
"android:windowAnimationStyle">
@style/PageAnimation</item>
</style>
<style name=
"PageAnimation" parent=
"@android:style/Animation.Activity">
<item name=
"android:activityOpenEnterAnimation" >
@anim/translate_into</item>
<item name=
"android:activityOpenExitAnimation">
@anim/translate_out</item>
<item name=
"android:activityCloseEnterAnimation">
@anim/translate_close_into</item>
<item name=
"android:activityCloseExitAnimation">
@anim/translate_close_out</item>
</style>
最后异步在AndroidManifest中在需要跳转的连个Activity设置 android:theme=”@style/Anim_fade”
<activity
android:name=
".A"
android:theme=
"@style/Anim_fade"/>
<activity
android:name=
".B"
android:theme=
"@style/Anim_fade"/>
在java代码中正常调用跳转也买年就行了,设置Theme在页面返回也有退出的动画效果
转载请注明原文地址: https://ju.6miu.com/read-679831.html