Android基础知识

android之界面切换动画使用

2020-12-02  本文已影响0人  Lee_5566
image.png

overridePendingTransition

一般情况下Activity的切换是就是从中间弹出来,然后遮盖住之前的Activity。

要显示的Acitvity从左边往右的平移进入,被遮盖的Activity从左往右的平移划出。就需要用到overridePendingTransition函数。

public void overridePendingTransition (int enterAnim, int exitAnim)

其中:

enterAnim 定义Activity进入屏幕时的动画(要显示的Activity进入时的动画)
exitAnim 定义Activity退出屏幕时的动画(被遮盖住的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.

另外:overridePendingTransition 方法必须在startActivity()或者 finish()方法的后面。

实例代码:

startActivity(new Intent(MainActivity.this,SecondActivity.class));
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
image.png

实战

main_acrivity.java:

package com.exmple.transition;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.switchButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  new Intent();
                intent.setClass(MainActivity.this, test2.class);

                startActivity(intent);

                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                overridePendingTransition(0, 0);
            }
        });
    }
}

test2.java:

package com.exmple.transition;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class test2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/switchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_test2.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".test2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello,world"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
上一篇下一篇

猜你喜欢

热点阅读