android中一种欢迎界面模式

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在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>


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值