平时我做的界面跳转都是默认的跳转,但是发现有很多app界面跳转很炫酷,我研究了一下,做个笔记,看了下,Android中有几种设置好的跳转方式,我demo中Android自带的跳转方式主要有5种:
默认效果
android.R.anim.fade_in 淡入
android.R.anim.fade_out 淡出
android.R.anim.slide_in_left 左滑
android.R.anim.slide_out_right 右滑
下面直接上代码展示:
这里我是从 MainActivity通过 Intent 跳转到 AfterJumpActivity,在 AfterJumpActivity 点击返回时是直接 finish()
MainActivity
-
import android.content.Intent;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
-
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
Button btn_default = findViewById(R.id.btn_default);
-
Button btn_default1 = findViewById(R.id.btn_default1);
-
Button btn_default2 = findViewById(R.id.btn_default2);
-
Button btn_default3 = findViewById(R.id.btn_default3);
-
Button btn_default4 = findViewById(R.id.btn_default4);
-
-
btn_default.setOnClickListener(this);
-
btn_default1.setOnClickListener(this);
-
btn_default2.setOnClickListener(this);
-
btn_default3.setOnClickListener(this);
-
btn_default4.setOnClickListener(this);
-
-
}
-
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.btn_default:
-
startActivity(new Intent(MainActivity.this, AfterJumpActivity.class));
-
break;
-
case R.id.btn_default1:
-
startActivity(new Intent(MainActivity.this, AfterJumpActivity.class));
-
overridePendingTransition(android.R.anim.fade_in,0);
-
break;
-
case R.id.btn_default2:
-
startActivity(new Intent(MainActivity.this, AfterJumpActivity.class));
-
overridePendingTransition(android.R.anim.fade_out,0);
-
break;
-
case R.id.btn_default3:
-
startActivity(new Intent(MainActivity.this, AfterJumpActivity.class));
-
overridePendingTransition(android.R.anim.slide_in_left,0);
-
break;
-
case R.id.btn_default4:
-
startActivity(new Intent(MainActivity.this, AfterJumpActivity.class));
-
overridePendingTransition(android.R.anim.slide_out_right,0);
-
break;
-
}
-
}
-
}
activity_main.xml
-
-
<LinearLayout 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:orientation= "vertical"
-
tools:context= ".MainActivity">
-
-
<Button
-
android:id= "@+id/btn_default"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "默认效果"/>
-
-
<Button
-
android:id= "@+id/btn_default1"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "淡入"/>
-
-
<Button
-
android:id= "@+id/btn_default2"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "淡出"/>
-
-
<Button
-
android:id= "@+id/btn_default3"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "左滑3"/>
-
-
<Button
-
android:id= "@+id/btn_default4"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "右滑4"/>
-
-
</LinearLayout>
AfterJumpActivity
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
-
public class AfterJumpActivity extends AppCompatActivity implements View.OnClickListener{
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_after_jump);
-
-
Button btn_default = findViewById(R.id.btn_default);
-
Button btn_default1 = findViewById(R.id.btn_default1);
-
Button btn_default2 = findViewById(R.id.btn_default2);
-
Button btn_default3 = findViewById(R.id.btn_default3);
-
Button btn_default4 = findViewById(R.id.btn_default4);
-
-
btn_default.setOnClickListener(this);
-
btn_default1.setOnClickListener(this);
-
btn_default2.setOnClickListener(this);
-
btn_default3.setOnClickListener(this);
-
btn_default4.setOnClickListener(this);
-
}
-
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.btn_default:
-
finish();
-
break;
-
case R.id.btn_default1:
-
finish();
-
overridePendingTransition(android.R.anim.fade_in,0);
-
break;
-
case R.id.btn_default2:
-
finish();
-
overridePendingTransition(android.R.anim.fade_out,0);
-
break;
-
case R.id.btn_default3:
-
finish();
-
overridePendingTransition(android.R.anim.slide_in_left,0);
-
break;
-
case R.id.btn_default4:
-
finish();
-
overridePendingTransition(android.R.anim.slide_out_right,0);
-
break;
-
}
-
}
-
}
activity_after_jump.xml
-
-
<LinearLayout 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:orientation= "vertical"
-
tools:context= ".AfterJumpActivity">
-
-
<Button
-
android:id= "@+id/btn_default"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "默认效果"/>
-
-
<Button
-
android:id= "@+id/btn_default1"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "淡入返回"/>
-
-
<Button
-
android:id= "@+id/btn_default2"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "淡出返回"/>
-
-
<Button
-
android:id= "@+id/btn_default3"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "左滑返回3"/>
-
-
<Button
-
android:id= "@+id/btn_default4"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "右滑返回4"/>
-
-
</LinearLayout>
看代码可以看出,主要是用到了 overridePendingTransition(android.R.anim.slide_out_right,0); 方法
-
/**
-
* Call immediately after one of the flavors of {@link #startActivity(Intent)}
-
* or {@link #finish} to specify an explicit transition animation to
-
* perform next.
-
*
-
* <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
-
* to using this with starting activities is to supply the desired animation
-
* information through a {@link ActivityOptions} bundle to
-
* {@link #startActivity(Intent, Bundle)} or a related function. This allows
-
* you to specify a custom animation even when starting an activity from
-
* outside the context of the current top activity.
-
*
-
* @param enterAnim A resource ID of the animation resource to use for
-
* the incoming activity. Use 0 for no animation.
-
* @param exitAnim A resource ID of the animation resource to use for
-
* the outgoing activity. Use 0 for no animation.
-
*/
-
public void overridePendingTransition(int enterAnim, int exitAnim) {
-
try {
-
ActivityManager.getService().overridePendingTransition(
-
mToken, getPackageName(), enterAnim, exitAnim);
-
} catch (RemoteException e) {
-
}
-
}
这个方法的两个参数是就是设置跳转的动画效果
第一个参数是指跳转过去时的动画效果参数,第二个参数是指点击系统返回按钮时的动画效果参数
而这个demo中返回时点击的是自定义的按钮,所以也需要调用 overridePendingTransition 方法设置返回时的动画效果
demo演示的都是系统自带的动画效果,如果想要更炫酷的效果,就需要自定义跳转动画
在res下面建一个anim文件夹,在这里进行自定义跳转效果,如下
anim_in.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="2.0" android:toXScale="1.0" android:fromYScale="2.0" android:toYScale="1.0" android:pivotX="50%p" android:pivotY="50%p" android:duration="@android:integer/config_mediumAnimTime" /> </set>
anim_out.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="@android:integer/config_mediumAnimTime" /> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="@android:integer/config_mediumAnimTime"/> </set>
如果要其他效果可以根据当前的更改参数设置,使用方法也是类似上面的,在demo中也有
case R.id.btn_default5: startActivity(new Intent(MainActivity.this, AfterJumpActivity.class)); overridePendingTransition(R.anim.anim_in,0); break; case R.id.btn_default6: startActivity(new Intent(MainActivity.this, AfterJumpActivity.class)); overridePendingTransition(R.anim.anim_out,0); break;
下载demo后自己建个项目将代码复制进去就可以看到效果,很简单的,主要是下图划线部分,这样能完美解决android studio版本不匹配问题