当前位置:操作系统 > 安卓/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>  

 

 
 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,