Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android中一種歡迎界面模式

android中一種歡迎界面模式

編輯:關於Android編程

在android應用中,在進入主界面之前經常會有一段用於過渡的歡迎界面,下面介紹一種,代碼如下:   WelcomeActivity:  
package com.home.welcompage;  
  
import java.util.Random;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Message;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.animation.Animation;  
import android.view.animation.AnimationUtils;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
  
public class WelcomeActivity extends Activity {  
    private ImageView welcomeImage;  
    private Thread t1, t2;  
    private Handler handler;  
    private int rand = new Random().nextInt(4);;  
    private Animation animation;  
    private int pageNumber = 1;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.welcome);  
        welcomeImage = (ImageView) findViewById(R.id.welcome_iv);  
        animation = AnimationUtils.loadAnimation(this, R.anim.scal_anim);  
        LinearLayout layout = (LinearLayout) findViewById(R.id.welcome_layout);  
        layout.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                if (pageNumber == 2) {  
                    t1.interrupt();  
                    t2.interrupt();  
                    turnPage();// 跳轉界面  
                }  
            }  
        });  
        handler = new Handler() {  
            @Override  
            public void handleMessage(Message msg) {  
                super.handleMessage(msg);  
                if (msg.what == 0) {  
                    switch (rand) {  
                    case 0:  
                        // 設置動畫背景  
                        startAnimation(R.drawable.back1);  
                        break;  
                    case 1:  
                        startAnimation(R.drawable.back2);  
                        break;  
                    case 2:  
                        startAnimation(R.drawable.back3);  
                        break;  
                    case 3:  
                        startAnimation(R.drawable.back4);  
                        break;  
                    default:  
                        break;  
                    }  
                }  
            }  
        };  
        t1 = new Thread(new Runnable() {  
  
            @Override  
            public void run() {  
                try {  
                    Thread.sleep(3000);  
                    handler.sendEmptyMessage(0);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
        t1.start();  
        t2 = new Thread(new Runnable() {  
  
            @Override  
            public void run() {  
                try {  
                    Thread.sleep(4000);  
                    turnPage();// 跳轉界面  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
    }  
  
    /** 
     * 跳轉到主界面 
     */  
    private void turnPage() {  
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);  
        WelcomeActivity.this.startActivity(intent);  
        WelcomeActivity.this.finish();  
    }  
  
    /** 
     * 設置動畫背景 
     *  
     * @param imageSource 
     */  
    private void startAnimation(int imageSource) {  
        welcomeImage.setBackgroundDrawable(getResources().getDrawable(  
                imageSource));  
        welcomeImage.setAnimation(animation);  
        welcomeImage.startAnimation(animation);  
        t2.start();  
        pageNumber = 2;  
    }  
}  

 

  welcome.xml:  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:id="@+id/welcome_layout">  
  
    <ImageView  
        android:id="@+id/welcome_iv"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="@drawable/logo"/>  
  
</LinearLayout>  

 

    主界面省略。 動畫:scal_anim.xml:    
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
  
    <scale  
        android:duration="6000"  
        android:fromXScale="1.0"  
        android:fromYScale="1.0"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:toXScale="1.2"  
        android:toYScale="1.2" />  
  
</set>  

 

   
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved