java - 如何制作像 youtube、reddit 和 twitter 等那样的起始 View /Activity ?

标签 java android android-activity

在 Android 中,当您启动 YouTube 时,您会看到几秒钟的 View ,然后开始后面的内容,如所附的图片

Example of Youtube app

another example of reddit app

我的方法是创建一个 Activity ,并在 onCreate 中声明一个倒计时器,就像下面的代码一样:

new CountDownTimer(2000, 1000) {
        public void onTick(long millisUntilFinished) {
        }
        public void onFinish() {
            startActivity(new Intent(WelcomingActivity.this, LoginActivity.class));
            finish();
        }
    }.start();

这是一个好方法吗? 提前致谢!

最佳答案

  1. 创建SplashScreenActivity.java:

    public class SplashScreenActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash_screen);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }, 2000); // your screen will disappear after 2 seconds
        }
    }
    
  2. AndroidManifest.xml 中注册 Activity :

    <activity android:name="SplashScreenActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
    </activity>
    
  3. 并为启动屏幕创建布局:activity_splash_screen.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="@drawable/gradient_background" >
    
        <ImageView
            android:id="@+id/imgLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="your_image" />
    
    </RelativeLayout>
    

关于java - 如何制作像 youtube、reddit 和 twitter 等那样的起始 View /Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43939869/

相关文章:

java - 如果如何避免

java - 预期位置参数计数 : 1, 实际参数:[]

android - 将广播 Intent 从服务发送到应用程序类

android - 如何以类似于相机为 SurfaceTexture 生成帧的方式生成 YUVI420 帧到 SurfaceTexture?

java - 具有不同布局的多个按钮

Android-Activity-我应该在 OnDestroy 方法上将 View 设置为 null 吗?

java - 在 Android 中使用锅炉管

java - 使用java在play框架模板中发送Session

android - 为什么在 Compose 中使用 jetpack 导航时 View 一直闪烁?

即使我指定了 api 级别,Android 也会显示弃用警告