一个简单启动画面和引导页
2017-08-25 本文已影响0人
一杯水结成冰
常常会有一些APP开发需要用到启动画面或者引导页面,启动页面正常都是一些简单的Logo或者图片之类,主要是能实现一些后台数据的加载。而引导页面可以通过左右滑动方式传达信息(APP介绍,新功能,插入广告等)给用户。
简单实现启动画面
两种方法的实现:
一 建立一个activity,展示启动画面,然后再跳转到主Activity。
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {//一秒后跳转到主页面
public void run() {
// 跳转至 MainActivity
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
WelcomeActivity.this.finish();//如果没有finish掉,按回退键就会回到欢迎界面不合理。
}
}, 1000);
}
}
布局layout.activity_welcome.xml:只添加一张背景图片
<?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"
android:background="@mipmap/timg">
</LinearLayout>
二 在同一个activity展示启动画面,后进行隐藏。
public class Welcome2Activity extends Activity implements Animation.AnimationListener {
LinearLayout welcomelinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);//2秒渐变
animation.setDuration(2000);
welcomelinearLayout = (LinearLayout) findViewById(R.id.welcome);
welcomelinearLayout.setAnimation(animation);
animation.setAnimationListener(this);
}
@Override
public void onAnimationStart(Animation animation) {
welcomelinearLayout.setVisibility(View.VISIBLE);//开始时进行显示
}
@Override
public void onAnimationEnd(Animation animation) {
welcomelinearLayout.setVisibility(View.GONE); //结束时进行隐藏起来
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
布局layout.activity_welcome2.xml
<?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:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/timg"
android:orientation="vertical"></LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Welcome To MainActivity!"
android:textSize="24sp" />
</LinearLayout>
简单实现引导页
这里是用ViewPager实现引导页。
介绍:引导页有四个可以左右滑动的页面,最后一个页面有一个按钮点击可进入主页面。
分别创建4个子布局welcome_item1.xml,welcome_item2.xml,welcome_item3.xml都是一样布局background设置不一样图片而已
<?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:background="@drawable/timg"
android:orientation="vertical">
</LinearLayout>
welcome_item4.xml也就多一个按钮
<?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"
android:background="@drawable/timg4"
android:orientation="vertical">
<Button
android:id="@+id/Bt_start"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:text="进入主页面"
android:textSize="20sp" />
</RelativeLayout>
接下来我们来看看主布局activity_welcome4.xml,主页面分两个模块一个是ViewPager还一个是要实现底部的小圆点功能:
<?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"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/welcome_vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/spot_linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
接下来编写小圆点的selector,分为选中和没选中两种状态。
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/dark_dot" />
<item android:state_enabled="false" android:drawable="@drawable/white_dot" />
</selector>
dark_dot.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#33ccff"/>
<size android:width="20dp"
android:height="20dp"/>
</shape>
white_dot
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#ccffbb"/>
<size android:width="20dp"
android:height="20dp"/>
</shape>
接下来就是activity部分,先上全码
public class Welcome4Activity extends Activity implements ViewPager.OnPageChangeListener {
private ViewPager welcome_vp;
private ArrayList<View> ViewList;
private Button Bt_start;
private View view1, view2, view3, view4;
//底部小点图片
private ImageView[] dots;
//引导页个数
private int Number=4;
//记录当前选中位置
private int currentIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome4);
initData();
//初始化底部小点
initDots();
welcome_vp = (ViewPager) findViewById(R.id.welcome_vp);
WelcomeVPAdapter adapter = new WelcomeVPAdapter();
welcome_vp.setAdapter(adapter);
welcome_vp.addOnPageChangeListener(this);
Bt_start = (Button) view4.findViewById(R.id.Bt_start);
Bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToActivity(MainActivity.class.getName());
finish();
}
});
}
//初始化底部小点
private void initDots() {
LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
//根据个数添加对应小点的ImageView
for (int i = 0; i < Number; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.dot);
iv.setPadding(30, 30, 30, 30);
spot_linearLayout.addView(iv);
}
dots = new ImageView[Number];
//循环取得小点图片
for (int i = 0; i < Number; i++) {
dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
dots[i].setEnabled(true);//全部设置颜色为不选中状态
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);//默认第一个选中状态
}
private void setCurDot(int positon) {
if (positon < 0 || positon > Number - 1 || currentIndex == positon) {
return;
}
dots[positon].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = positon;
}
//初始化数据
private void initData() {
ViewList = new ArrayList<View>();
view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
ViewList.add(view1);
ViewList.add(view2);
ViewList.add(view3);
ViewList.add(view4);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setCurDot(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
class WelcomeVPAdapter extends PagerAdapter {
//返回可以滑动的VIew的个数
@Override
public int getCount() {
return ViewList.size();
}
//滑动切换的时候销毁当前的组件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(ViewList.get(position));
}
//将当前视图添加到container中并返回当前View视图
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(ViewList.get(position));
return ViewList.get(position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
protected void goToActivity(String className) {
Intent intent = new Intent();
intent.setClassName(getBaseContext(), className);
startActivity(intent);
}
}
首先肯定是初始化数据咯
这里有三个个模块的初始化(点击按钮的就不说了)
- view的初始化
//初始化数据
private void initData() {
ViewList = new ArrayList<View>();
view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
ViewList.add(view1);
ViewList.add(view2);
ViewList.add(view3);
ViewList.add(view4);
}
- 小圆点的初始化
//初始化底部小点
private void initDots() {
LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
//动态添加小圆点的ImageView
for (int i = 0; i < Number; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.dot);
iv.setPadding(30, 30, 30, 30);
spot_linearLayout.addView(iv);
}
dots = new ImageView[Number];
//循环取得小点图片
for (int i = 0; i < Number; i++) {
dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
dots[i].setEnabled(true);//全部设置颜色为不选中状态
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);//默认第一个选中状态
}
我们知道ViewPager类需要一个PagerAdapter适配器类给它提供数据
所以
class WelcomeVPAdapter extends PagerAdapter {
//返回可以滑动的VIew的个数
@Override
public int getCount() {
return ViewList.size();
}
//滑动切换的时候销毁当前的组件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(ViewList.get(position));
}
//将当前视图添加到container中并返回当前View视图
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(ViewList.get(position));
return ViewList.get(position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
然后我们还需要实现了OnPageChangeListener接口,对ViewPager滑动事件作出相应的反应。
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setCurDot(position);//在这里我们进行小点状态的修改
}
@Override
public void onPageScrollStateChanged(int state) {
}
ok一个简单的引导页实现了!
1.png