启动界面

2016-09-18  本文已影响29人  TTTqiu

1. 方法一:SplashActivity 作为主 Activity,定时跳转到 MainActivity:

package com.ttt.zhihudaily.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ttt.zhihudaily.R;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        // 动画
        ImageView imageView=(ImageView)findViewById(R.id.splash_image);
        TextView textView1=(TextView)findViewById(R.id.splash_text_1);
        TextView textView2=(TextView)findViewById(R.id.splash_text_2);
        AlphaAnimation imageAnimation=new AlphaAnimation(0.0f,1.0f);
        imageAnimation.setDuration(1000);
        imageView.setAnimation(imageAnimation);
        AlphaAnimation textAnimation=new AlphaAnimation(0.0f,1.0f);
        textAnimation.setDuration(300);
        textView1.setAnimation(textAnimation);
        textView2.setAnimation(textAnimation);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(SplashActivity.this,MainActivity.class);
                SplashActivity.this.startActivity(intent);
                SplashActivity.this.finish();
            }
        },2000);
    }
}

2. 方法二:直接在 MainActivity 中,通过设置 Visibility 实现。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <android.support.v4.widget.DrawerLayout...>

    <LinearLayout
        android:id="@+id/splash_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/white">

        <ImageView
            android:id="@+id/splash_image"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:scaleType="centerCrop"
            android:src="@drawable/splash_image"/>

        <TextView
            android:id="@+id/splash_text_1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="40sp"
            android:textColor="@color/colorPrimary"
            android:text="知乎日报"/>

        <TextView
            android:id="@+id/splash_text_2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_horizontal"
            android:textSize="15sp"
            android:textColor="@color/colorPrimary"
            android:text="By   TTTqiu"/>

    </LinearLayout>

</RelativeLayout>
    ......
    private void initSplash(){
        // 防止启动界面时 RelativeLayout 下的主界面会响应事件
        drawerLayout.setVisibility(View.INVISIBLE);

        ImageView splashImage=(ImageView)findViewById(R.id.splash_image);
        TextView splashText1=(TextView)findViewById(R.id.splash_text_1);
        TextView splashText2=(TextView)findViewById(R.id.splash_text_2);

        AlphaAnimation imageAnimation=new AlphaAnimation(0.0f,1.0f);
        imageAnimation.setDuration(1000);
        splashImage.setAnimation(imageAnimation);

        AlphaAnimation textAnimation=new AlphaAnimation(0.0f,1.0f);
        textAnimation.setDuration(1000);
        textAnimation.setStartOffset(800);
        splashText1.setAnimation(textAnimation);
        splashText2.setAnimation(textAnimation);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                LinearLayout splashLayout=(LinearLayout)findViewById(R.id.splash_layout);
                AlphaAnimation layoutAnimation=new AlphaAnimation(1.0f,0.0f);
                layoutAnimation.setDuration(500);
                splashLayout.setAnimation(layoutAnimation);
                splashLayout.setVisibility(View.GONE);
                drawerLayout.setVisibility(View.VISIBLE);
            }
        },2500);
    }
    ......
上一篇 下一篇

猜你喜欢

热点阅读