在Activity之间切换,默认切换的效果是弹出的,切换起来比较突兀,看了一些常用的Android应用,滑动切换Activity还是比较舒服的一种方式。
此效果主要使用的技能是:anim动画。
类:LeftRight,RightLeft,anim内四个xml文件。(代码来自网络)
anim中的文件如下所示:
in_from_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="500"
android:fromXDelta="-100%p"
android:toXDelta="0%p" />
</set>
in_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
</set>
out_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="500"
android:fromXDelta="0%p"
android:toXDelta="-100%p" />
</set>
LeftRightSlideActivity
public class LeftRightSlideActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(LeftRightSlideActivity.this,
SlideSecondActivity.class);
startActivity(intent);
// 设置切换动画,从右边进入,左边退出
overridePendingTransition(R.anim.in_from_left,
R.anim.out_to_right);
}
});
}
}
SlideSecondActivity
public class SlideSecondActivity extends LauncherActivity {
TextView back;
String[] names = {"个数","号","常数","板"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 , names);
// 设置该窗口显示的列表所需的Adapter
setListAdapter(adapter);
back = (TextView)findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(SlideSecondActivity.this, LeftRightSlideActivity.class);
startActivity(intent);
// 设置切换动画,从右边进入,左边退出
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
}
);
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activityexchange.LeftRightSlideActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exchange"
android:layout_below="@id/text1"
/>
</RelativeLayout>
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#900"
android:orientation="vertical" >
<TextView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#090"
android:text="返回"
android:textSize="50sp" />
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/back"
android:layout_weight="1"
android:background="#0000ff"
android:drawSelectorOnTop="false" />
</RelativeLayout>
点击按钮,实现从一个activity拉出另一个activity的效果,如果想在两个activity之前传参可使用上一节的Application方式。