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();
}
}