在应用程序开始和菜单显示之前,我想显示标志几秒钟。 我还想在它消失时使用一些动画效果。 我应该创建一个新的 Activity 吗? 还是可以在布局中设置?
我想在应用程序启动和菜单可见之前显示标识几秒钟,并在其消失时使用一些动画效果。 我应该创建一个新的 Activity 还是可以在布局中设置?请保留 HTML 标记。在应用程序开始和菜单显示之前,我想显示标志几秒钟。 我还想在它消失时使用一些动画效果。 我应该创建一个新的 Activity 吗? 还是可以在布局中设置?
我想在应用程序启动和菜单可见之前显示标识几秒钟,并在其消失时使用一些动画效果。 我应该创建一个新的 Activity 还是可以在布局中设置?请保留 HTML 标记。定义一个包含您的标识的启动屏幕布局,然后将此代码添加到您的活动中:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//display the logo during 5 seconds,
new CountDownTimer(5000,1000){
@Override
public void onTick(long millisUntilFinished){}
@Override
public void onFinish(){
//set the new Content of your activity
YourActivity.this.setContentView(R.layout.main);
}
}.start();
}
您可以使用仅样式的方法在启动时使用闪屏标志。不幸的是,它只能在API 23及以上版本中使用。但是您无需管理闪屏Activity。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:windowBackground">@drawable/logo_startup</item>
<item name="android:windowNoTitle">true</item>
</style>
res/drawable-v23/logo_startup.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/PageBackground"/>
<item android:width="@dimen/logo_startup" android:height="@dimen/logo_startup" android:gravity="center">
<bitmap android:src="@drawable/logo"/> //use PNG file here, not vector
</item>
</layer-list>
res/drawable/logo_startup.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/PageBackground"/>
</layer-list>
您可以使用设置setVisibility(Visibility.GONE)的图像视图,或者编写一个在时间结束后弹出并退出的活动。这取决于您个人的喜好...
为什么?用户不喜欢等待。但是,如果你需要等待因为你正在加载一些数据,你可以:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/* Do some work in a new thread, calling setContentView at the end with your view */
}
活动文件:
package com.karan.android.video;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splash extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashThread = new Thread()
{
@Override
public void run()
{
try {
int waited = 0;
while (waited < 3000)
{
sleep(100);
waited += 100;
}
} catch (InterruptedException e)
{
// do nothing
} finally
{
finish();
Intent i = new Intent(splash.this,video.class);
startActivity(i);
}
}
};
splashThread.start();
}
}
Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/buff"
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:textSize="40dp"
android:textColor="#CCFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Buffering..."
/>
</FrameLayout>
延迟执行可以用更简单的方式实现:
new Handler().postDelayed(new Runnable() {
// ... Hide splash image and show the real UI
}, 3000)
同时,Android标准的android.widget.ViewSwitcher
类非常适用于这种情况。