【发布时间】:2011-06-27 15:28:04
【问题描述】:
我想在应用程序启动和菜单可见之前显示一个徽标几秒钟。我也想在它消失时使用一些。我应该创建一个新活动吗?我可以在布局中设置它吗?
【问题讨论】:
-
喜欢闪屏? Here 就是一个例子。
标签: android
我想在应用程序启动和菜单可见之前显示一个徽标几秒钟。我也想在它消失时使用一些。我应该创建一个新活动吗?我可以在布局中设置它吗?
【问题讨论】:
标签: android
为包含您的徽标的启动屏幕定义一个布局,然后将此代码添加到您的活动中:
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();
}
【讨论】:
您可以使用获取 setVisibility(Visibility.GONE); 的图像视图或某种程度的东西,或者您可以编写一个在一段时间结束后弹出并退出的活动。那是你的个人喜好...
【讨论】:
您可以在启动时使用仅样式方法使用启动徽标。不幸的是,它仅适用于从 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>
【讨论】:
为什么?用户不喜欢等待。但是,如果您因为正在加载一些数据而需要等待,您可以:
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 */
}
【讨论】:
延迟执行可以用更简单的方式实现:
new Handler().postDelayed(new Runnable() {
// ... Hide splash image and show the real UI
}, 3000)
android 标准 android.widget.ViewSwitcher 类也非常适用于这类事情。
【讨论】:
活动文件:
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>
【讨论】: