您的位置:首页 > 移动开发

APP启动界面的实现,第一种方式

2016-06-06 14:41 603 查看
最近想把自己在项目中用到的一些功能和看到相关demo整理下,方便以后自己使用和学习,也希望能帮到刚开始开发的朋友,如果在这里有写的不好的地方希望大家帮忙提出和修改。

下面就开始一个简单的APP启动界面的功能实现.:

这是启动的xml文件.

<span style="font-size:18px;"><?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:scaleType="centerCrop"
android:src="@mipmap/launch_lina" />

</RelativeLayout></span>
预览图如下所示.
<img src="http://img.blog.csdn.net/20160530165404641?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
在这里我们如果要设置整个图片全屏的话有2种方式。

1:在Java文件中在onCreate()方法中使用如下代码可以实现全屏:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide the status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//hide the title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
2:或者在AndroidManifest.xml 中进行配置如下

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
如图所示:



下面就是java中的代码了,我在这里使用了一个很简单的方法来实现

使用了android中 AlphaAnimation对象

AlphaAnimation animation = new AlphaAnimation(0.7f ,1.0f);//设置透明度0.7 到1.0;


整个Activty的代码:

package com.chf.myexerciseapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.chf.myexerciseapp.MainActivity;
import com.chf.myexerciseapp.R;

/**
* Created by chenhf on 2016/5/30.
*/
public class LaunchActivity extends Activity{
private static final  String TAG = "LaunchActivity";
private ImageView mImgView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
mImgView  = (ImageView) findViewById(R.id.img_launch_lina);
AlphaAnimation animation = new AlphaAnimation(0.7f ,1.0f);//设置透明度0.7 到1.0;
animation.setDuration(3000);//设置动画持续时间。这里设置的是3秒。
mImgView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
Intent intent  = new Intent();
intent.setClass(LaunchActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.ECLAIR) {
return true;
} else {
onBackPressed();
}
return true;
}else {
return super.onKeyDown(keyCode, event);
}
}

@Override
public void onBackPressed() {
Log.d(TAG,"在onBackPressed中拦截返回键");
return;
}

}
在上面代码中,我们使用了setAnimationListener这个方法。该方法所需要传入AnimationListener这个listener,在改listener的onAnimationEnd中增加了,跳转到主界面的代码。

一个很简单的启动界面就完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android