我的一个非授权项目需要一个启动画面,即使它不是必需的。也许它对您的项目有用,因为它基于对话而不是活动。如果启动屏幕被轻按或手势,它将被关闭,以及在动画完成后(定时淡出)。可以对类进行修改,以便在允许点击或手势关闭图像之前检查某种“就绪状态布尔值”。
类文件:AppIntro.java
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
public class AppIntro extends Dialog {
protected int mLayoutRes = 0;
protected int mAnimRes = 0;
protected Animation mIntroAnim = null;
protected View mLayout = null;
public AppIntro(Context aContext, int aLayoutRes, int aAnimRes) {
super(aContext);
mLayoutRes = aLayoutRes;
mAnimRes = aAnimRes;
}
@Override
protected void onCreate(Bundle aSavedState) {
super.onCreate(aSavedState);
mLayout = LayoutInflater.from(getContext()).inflate(mLayoutRes,null);
mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppIntro.this.dismiss();
}
});
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(mLayout);
mIntroAnim = AnimationUtils.loadAnimation(getContext(),mAnimRes);
mIntroAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//nothing to do
}
@Override
public void onAnimationRepeat(Animation animation) {
//nothing to do
}
@Override
public void onAnimationEnd(Animation animation) {
AppIntro.this.dismiss();
}
});
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
dismiss();
return true;
}
@Override
public void show() {
super.show();
mLayout.startAnimation(mIntroAnim);
}
}
接下来,我们在文件“res/anim/intro_anim.xml”中定义动画淡出(将持续时间更改为加载应用程序所需的时间)。4200 = 4.2 秒。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<alpha
android:fromAlpha="1.0" android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="4200"
android:repeatCount="0" >
</alpha>
</set>
最后,我们在“layout/intro.xml”中定义我们的初始屏幕布局(使用您想要的任何图像)。我的特殊启动屏幕显示了一个带有图像的应用程序标题以及来自各种资金来源的 3 个徽标。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_intro"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/intro_Text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/title_intro"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/intro_Image_myproject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/intro_Text_title"
android:layout_centerHorizontal="true"
android:src="@drawable/intro_image" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/intro_Image_myproject"
android:layout_alignRight="@id/intro_Image_myproject"
android:layout_alignLeft="@id/intro_Image_myproject">
<ImageView
android:id="@+id/intro_Image_logo1"
android:layout_width="80dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/logo1"
android:layout_gravity="left|center_vertical"/>
<ImageView
android:id="@+id/intro_Image_logo2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:src="@drawable/logo2"
android:layout_gravity="center"
android:scaleType="centerInside"/>
<ImageView
android:id="@+id/intro_Image_logo3"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="fitXY"
android:src="@drawable/logo3"
android:layout_gravity="right|center_vertical"/>
</FrameLayout>
</RelativeLayout>
用于弹出对话框的代码:
@Override
protected void onCreate(Bundle aSavedState) {
super.onCreate(aSavedState);
if (aSavedState==null) {
//only show splash screen at app start, not on rotate screen
new AppIntro(this,R.layout.intro,R.anim.intro_anim).show();
}
setContentView(R.layout.main);
//...rest of onCreate()
}
我的应用程序在启动画面的同时显示了我的主视图,所以不能保证这个对话框会在你调用 .show() 时立即显示。