学了两天的Splash安卓启动页面的程序逻辑结构和练习,先不多说,先把代码贴上再说。
布局文件:activity_main.xml, splash.xml, secondview.xml
1<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal">
<ImageView
android:id="@+id/welcome_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/three"
/>
</LinearLayout>
2<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Button_1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Button_2" />
<TextView
android:id="@+id/textView1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#fff"
android:text="@string/tv_1" />
</LinearLayout>
3<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_2"
android:textSize="18sp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/male_checked" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female_checked" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_3"
android:textSize="18sp" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/yes_checked" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_checked" />
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RadioGroup>
</LinearLayout>
Java程序:MainActivity.java, Splash.java, SecondView.java
1package com.example.demo6;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity implements AnimationListener {
private ImageView imageView = null;
private Animation alphaAnimation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.welcome_image_view);
alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);
alphaAnimation.setFillEnabled(true); //启动Fill保持
alphaAnimation.setFillAfter(true); //设置动画的最后一帧是保持在View上面
imageView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(this); //为动画设置监听
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束时结束欢迎界面并转到软件的主界面
Intent intent = new Intent(this, Splash.class);
startActivity(intent);
this.finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//在欢迎界面屏蔽BACK键
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return false;
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
2package com.example.demo6;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Splash extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Splash.this, SecondView.class);
startActivity(intent);
//设置切换动画,从右边进入,左边退出
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
});
}
}
3package com.example.demo6;
import android.app.Activity;
import android.os.Bundle;
public class SecondView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second_view);
}
}
anim文件(动画效果):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="3000"
android:duration="3000"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="500" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="500" />
</set>