Android的Activity跳转动画各种效果整理
程序员文章站
2023-11-25 10:57:40
大家使用android的原生ui都知道,android的activity跳转就是很生硬的切换界面。其实android的activity跳转可以设置各种动画。下面给大家看看效...
大家使用android的原生ui都知道,android的activity跳转就是很生硬的切换界面。其实android的activity跳转可以设置各种动画。下面给大家看看效果:
实现非常简单,用overridependingtransition(int inid, int outid)即可实现。inid是下一界面进入效果的xml文件的id,outid是当前界面退出效果的xml文件id。
效果是用xml文件写的,首先要在res文件夹下建立anim文件夹,然后把动画效果xml文件放到里面去。
下面是放大进入,缩小退出的xml文件:
zoomin.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromxscale="0.1" android:toxscale="1.0"
android:fromyscale="0.1" android:toyscale="1.0"
android:pivotx="50%p" android:pivoty="50%p"
android:duration="300" />
<!-- 这里为了看到动画演示效果,把动画持续时间设为3秒 -->
<alpha
android:fromalpha="0.1"
android:toalpha="1.0"
android:duration="300" />
</set>
zoomout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zadjustment="top">
<scale android:fromxscale="1.0" android:toxscale=".5"
android:fromyscale="1.0" android:toyscale=".5"
android:pivotx="50%p" android:pivoty="50%p"
android:duration="300" />
<!-- 系统内置的动画持续时间
android:duration="@android:integer/config_mediumanimtime"
-->
<alpha android:fromalpha="1.0" android:toalpha="0"
android:duration="300"/>
</set>
mainactivity.java:
public class mainactivity extends activity implements onclicklistener {
private button btn1;
private button btn2;
private button btn3;
private button btn4;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_1);
btn1 = (button) findviewbyid(r.id.btn1);
btn2 = (button) findviewbyid(r.id.btn2);
btn3 = (button) findviewbyid(r.id.btn3);
btn4 = (button) findviewbyid(r.id.btn4);
btn1.setonclicklistener(this);
btn2.setonclicklistener(this);
btn3.setonclicklistener(this);
btn4.setonclicklistener(this);
}
@override
public void onclick(view v) {
// todo auto-generated method stub
int version = integer.valueof(android.os.build.version.sdk);
switch(v.getid()) {
case r.id.btn1:
//放大缩小跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.zoomin, r.anim.zoomout);
}
break;
case r.id.btn2:
//淡入淡出跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.alphain, r.anim.alphaout);
}
break;
case r.id.btn3:
//左向右跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.move_in_right, r.anim.move_out_right);
}
break;
case r.id.btn4:
//右向左跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.move_in_left, r.anim.move_out_left);
}
break;
}
finish();
}
}
还有很多动画效果,就要靠我们发挥自己的想象力,自己去设计,呵呵~
实现非常简单,用overridependingtransition(int inid, int outid)即可实现。inid是下一界面进入效果的xml文件的id,outid是当前界面退出效果的xml文件id。
效果是用xml文件写的,首先要在res文件夹下建立anim文件夹,然后把动画效果xml文件放到里面去。
下面是放大进入,缩小退出的xml文件:
zoomin.xml:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromxscale="0.1" android:toxscale="1.0"
android:fromyscale="0.1" android:toyscale="1.0"
android:pivotx="50%p" android:pivoty="50%p"
android:duration="300" />
<!-- 这里为了看到动画演示效果,把动画持续时间设为3秒 -->
<alpha
android:fromalpha="0.1"
android:toalpha="1.0"
android:duration="300" />
</set>
zoomout.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zadjustment="top">
<scale android:fromxscale="1.0" android:toxscale=".5"
android:fromyscale="1.0" android:toyscale=".5"
android:pivotx="50%p" android:pivoty="50%p"
android:duration="300" />
<!-- 系统内置的动画持续时间
android:duration="@android:integer/config_mediumanimtime"
-->
<alpha android:fromalpha="1.0" android:toalpha="0"
android:duration="300"/>
</set>
mainactivity.java:
复制代码 代码如下:
public class mainactivity extends activity implements onclicklistener {
private button btn1;
private button btn2;
private button btn3;
private button btn4;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_1);
btn1 = (button) findviewbyid(r.id.btn1);
btn2 = (button) findviewbyid(r.id.btn2);
btn3 = (button) findviewbyid(r.id.btn3);
btn4 = (button) findviewbyid(r.id.btn4);
btn1.setonclicklistener(this);
btn2.setonclicklistener(this);
btn3.setonclicklistener(this);
btn4.setonclicklistener(this);
}
@override
public void onclick(view v) {
// todo auto-generated method stub
int version = integer.valueof(android.os.build.version.sdk);
switch(v.getid()) {
case r.id.btn1:
//放大缩小跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.zoomin, r.anim.zoomout);
}
break;
case r.id.btn2:
//淡入淡出跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.alphain, r.anim.alphaout);
}
break;
case r.id.btn3:
//左向右跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.move_in_right, r.anim.move_out_right);
}
break;
case r.id.btn4:
//右向左跳转
startactivity(new intent(this, otheractivity.class));
if(version > 5 ){
overridependingtransition(r.anim.move_in_left, r.anim.move_out_left);
}
break;
}
finish();
}
}
还有很多动画效果,就要靠我们发挥自己的想象力,自己去设计,呵呵~