一.
1.首先看效果:
2.实现起来很简单:
1)布局(splashlayout.xml)
2)代码(SplashActivity.java)
二.分析布局:
代码(splashlayout.xml):
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_spash_backgroud"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#44000000"
android:layout_alignParentBottom="true"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="知乎日报"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="每天三次,每次七分中"
android:textColor="#FFFFF0"
android:textSize="18sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="8dp"/>
</LinearLayout>
</RelativeLayout>
三.新建activity包专门放各种activity,并创建SplashActivity.java
1.SplashActivity中的逻辑
1) requestWindowFeature(Window.FEATURE_NO_TITLE)去除标题栏,这样就可以全屏显示了
2)加载splashlayout布局,拿到ImageView组件
3)在ImageView上启动Alpha动画,实现从无到有动画
4)动画完成,跳转到MainActivity
2.注意:
1)布局,组件的初始化全部放在initView()方法中。
2)事件的初始化全部放在initListener()方法中。
3.具体代码:
public class SplashActivity extends Activity {
private View mIvBackground;
private AlphaAnimation mAlphaAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1)去除标题栏,这样就可以全屏显示了
requestWindowFeature(Window.FEATURE_NO_TITLE);
initView();
// 初始化动画
initAnimation();
initListener();
}
private void initListener() {
mAlphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
/*
* 4)动画完成,跳转到MainActivity
*/
@Override
public void onAnimationEnd(Animation arg0) {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
}
/**
* 3)在ImageView上启动Alpha动画,实现从无到有动画
*/
private void initAnimation() {
/*
* AnimationSetanimationSet = new AnimationSet(true);
*
* ScaleAnimationscaleAnimation = new ScaleAnimation(0, 1, 0, 1,
*Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
*scaleAnimation.setDuration(2000); scaleAnimation.setFillAfter(true);
*animationSet.addAnimation(scaleAnimation);
*/
mAlphaAnimation = new AlphaAnimation(0f, 1f);
mAlphaAnimation.setDuration(1500);
mAlphaAnimation.setFillAfter(true);
//animationSet.addAnimation(alphaAnimation);
mIvBackground.setAnimation(mAlphaAnimation);
}
/**
* 2)加载splashlayout布局,拿到ImageView组件
*/
private void initView() {
setContentView(R.layout.splashlayout);
mIvBackground = findViewById(R.id.iv_spash_backgroud);
}
}