引导页SpalshActivity实现

2016-09-22  本文已影响59人  一页书啊
2016-09-22_103542.png

1、
创建SpalshActivity,在清单列表中加入以下代码,这样可以实现应用启动时最先加载SpalshActivity页面

   <activity android:name=".activity.SpalshActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

2、清单列表中引用了一个主题,是为了实现全屏,隐藏ActionBar,在style.xml文件中添加以下代码即可

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

3.SpalshActivty代码如下:

public class SpalshActivity extends Activity {
private boolean isStart=false;
private ImageView icon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_luanch);
    initView();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startMainActivity();
        }
    },2000);
}

private void initView() {
    icon = (ImageView)findViewById(R.id.icon);
}

private void startMainActivity() {
    if(!isStart){
        isStart=true;
        Intent intent=new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

/**
 * 点击直接进入,不要等待2秒
 * @param event
 * @return
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    if(!isStart){
        isStart=true;           //
        Intent intent=new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    return super.onTouchEvent(event);
}  }

4.布局文件如下:
其中icon是一个圆形图片,居中显示
bg_logo背景图片

<?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/bg_logo"
  >
  <ImageView
      android:id="@+id/icon"
      android:src="@drawable/icon"
      android:layout_width="250dp"
      android:layout_height="250dp"
      android:layout_centerInParent="true"
      android:scaleType="centerCrop"/>
  <LinearLayout
      android:gravity="center"
      android:id="@+id/ll"
      android:layout_below="@id/icon"
      android:layout_marginTop="5dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <ProgressBar
          android:layout_width="40dp"
          android:layout_height="40dp" />
      <TextView
          android:text="正在启动中"
          android:textColor="#ff0000"
          android:textSize="25sp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

  </LinearLayout>

</RelativeLayout>
上一篇 下一篇

猜你喜欢

热点阅读