Android ViewPager 打造炫酷欢迎页
Android ViewPager 打造炫酷欢迎页
ViewPager是Android扩展v4包中的类,这个类可以让用户切换当前的View。对于这个类的应用场景,稍加修改就可以应用到多个环境下。比如:App的欢迎页,App导航页设计,app的侧滑退出和app的侧边栏应用界面设计等都可以用ViewPager实现。

1. 关于欢迎页的导航设计
设计思想:欢迎页的导航包括几个app说明(欢迎页),页面的下方有导航点,显示当前所在的页面数,自动跳转下一页,最后一页有进入的 登陆\注册 的按钮。或者有提示进入的按钮。或者有直接的登陆或者注册的输入框。输入完成后进入主界面。
代码设计:创建包容欢迎页面的视图集合,包含指示当前第几页的“点”的集合,包含最后一页的跳转设计,包含用户当前不操作的自动跳转,包含用户销毁当前应用第二次进入的跳过欢迎页等设计。
2. 欢迎页的引导页设计
- 1.引导类:
//建立Guide类继承自 AppCompatActivity,实现OnPageChangeListener 并实现OnPageChangeListener的onPageScrolled,onPageSelected,onPageScrollStateChanged类。
public class Guide extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter; //创建ViewPager适配器
private List<View> views; //创建视图集合
private ImageView[] dots; //创建指示点集合
private int[] ids = {R.id.iv_bit1, R.id.iv_bit2, R.id.iv_bit3, R.id.iv_bit4}; //初始化指示点集合
private Button btn_start; //初始末页跳转按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //欢迎页无标题设置
setContentView(R.layout.guide_layout);
initViews();
initDots();
}
private void initViews() {
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
views.add(inflater.inflate(R.layout.four, null));
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewPager);
vp.setAdapter(vpAdapter);
//适配器适配页面
btn_start = (Button) views.get(3).findViewById(R.id.btn_start);
//到第四页点击按钮跳转
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Guide.this,MainActivity.class);
//跳转到主Activity
startActivity(intent);
finish(); //销毁没有用的内存占用
}
});
vp.setOnPageChangeListener(this);
}
private void initDots() {
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageView) findViewById(ids[i]);
//根据当前页面的views.size()来变化指示图标
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//指示点的变化函数
@Override
public void onPageSelected(int position) {
for (int i = 0; i < ids.length; i++) {
if (position == i) {
dots[i].setImageResource(R.drawable.login_point_selected);
} else {
dots[i].setImageResource(R.drawable.login_point);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
- 2.引导视图:
位于页面最下方的四个点:第一个是默认被选中的点。
<?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.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00333333"
>
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<ImageView
android:id="@+id/iv_bit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point_selected"
/>
<ImageView
android:id="@+id/iv_bit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point"
/>
<ImageView
android:id="@+id/iv_bit3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point"
/>
<ImageView
android:id="@+id/iv_bit4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point"
/>
</LinearLayout>
</RelativeLayout>
- 3.打造视图适配器:
//建立ViewPagerAdapter继承自 PagerAdapter,并实现其destroyItem,instantiateItem,getCount,isViewFromObject方法。构造ViewPagerAdapter的内部类,创建View的数组类和联系上下文的Context。
public class ViewPagerAdapter extends PagerAdapter {
private List<View> views;
private Context context;
public ViewPagerAdapter(List<View> views,Context context){
this.views = views;
this.context = context;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager)container).removeView(views.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager)container).addView(views.get(position));
return views.get(position);
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
}
- 4.分别实现四个欢迎视图:
第一个欢迎界面:
<?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">
<ImageView
android:id="@+id/iv_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guide_one"
/>
</LinearLayout>
第四(末)个欢迎界面:
<?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:id="@+id/iv_four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guide_four"
/>
<LinearLayout
android:id="@+id/btn_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome"
/>
</LinearLayout>
</RelativeLayout>
- 5.建立控制进入与跳转变化的类WelCome:
//用异步的方法来实现跳转的判断
public class Welcome extends AppCompatActivity {
private boolean isFirstIn = false; //设置第一次进入为false
private static final int TIME = 2300; //从logo界面到欢迎页的时间为2.3秒
private static final int GO_HOME = 1000; //到主页的时间
private static final int GO_GUIDE = 1001; //到欢迎页的时间多1来判断
private Handler mHandler = new Handler(){ //异步信息
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case GO_HOME:
goHome();
break;
case GO_GUIDE:
goGuide();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
final Intent intent = new Intent(Welcome.this,MainActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_layout);
init(); //自定义方法判断是否第一次开启App
//设置自动跳转时间Timer 无响应后10秒自动跳转
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
startActivity(intent);
}
};
timer.schedule(task,1000*10);
finish();
}
private void init(){
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
isFirstIn = preferences.getBoolean("isFirstIn",true);
if (!isFirstIn){
mHandler.sendEmptyMessageDelayed(GO_HOME,TIME);
}else {
mHandler.sendEmptyMessageDelayed(GO_GUIDE,TIME);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("isFirstIn",false);
editor.commit();
}
}
private void goHome() {
Intent intent = new Intent(Welcome.this,MainActivity.class);
startActivity(intent);
finish();
}
private void goGuide() {
Intent intent = new Intent(Welcome.this,Guide.class);
startActivity(intent);
finish();
}
}
- 6.WelCome视图:
加入logo的ImageView视图:
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcome_android"
/>
</LinearLayout>
- 7.修改AndroidManifest.xml 注册信息以及首选开启项:
修改主题为NoTitleBar,修改引导页为WelCome类:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asdemot">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity android:name="com.example.asdemot.Guide">
</activity>
<activity android:name="com.example.asdemot.Welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- 8.总结:
需要导入V4的jar包。至此,一个热气腾腾的欢迎页就完成了。
Android ViewPager 打造炫酷欢迎页的更多相关文章
- Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI
1.概述 哈,今天给大家带来一个ViewPagerIndicator的制作,相信大家在做tabIndicator的时候,大多数人都用过 TabPageIndicator,并且很多知名APP都使用过这个 ...
- JParticles 2.0 发布,打造炫酷的粒子特效
JParticles 2.0 发布,打造炫酷的粒子特效.不好意思哈,在这么繁花似锦的世界里,标题不得不取得吸引眼球一点哈,不然...还是不啰嗦了,我们进入正题吧 简单介绍一下 JParticles 2 ...
- Android开发之炫酷MD风格
文章转自:一点点征服的 http://www.cnblogs.com/ldq2016/p/5217590.html 安卓开发中非常炫的效果集合 这几天开发的时候,想做一些好看而且酷炫的特效,于是又开始 ...
- Photoshop和WPF双剑配合,打造炫酷个性的进度条控件
现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或 ...
- hexo的next主题个性化教程:打造炫酷网站
看到有些next主题的网站很炫酷,那么是怎么配置的呢?接下来我会讲一讲如何实现一些炫酷的效果 主要有以下32种: 在右上角或者左上角实现fork me on github 添加RSS 添加动态背景 实 ...
- Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器
1.概述 本篇博客的产生呢,是因为,群里的哥们暖暖给我发了个效果图,然后问我该如何实现顶部ViewPager指示器的字体变色,该效果图是这样的: 大概是今天头条的app,神奇的地方就在于,切换View ...
- Android ViewPager打造3D画廊
本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 网上有很多关于使用Gallery来打造3D画廊的博客,但是在关于Gallery的官方说法中表明: This cl ...
- Android Viewpager+Fragment实现滑动标签页
ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应. 主页布局 <?xml version="1.0" ...
- Linux进阶之使用Oh-My-Zsh打造炫酷终端
Oh My Zsh是基于zsh命令行的一个扩展工具集,提供了丰富的扩展功能.除了功能增强之外,还提供非常丰富的主题.使用Oh-My-Zsh打造酷炫Shell终端的步骤(Deepin系统): 原始终端: ...
随机推荐
- CodeForces 554B(扫房间)
CodeForces 554B Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- C# WinForm的SplitContainer控件固定Panel大小[转]
原文地址:http://zhidao.baidu.com/link?url=mhkUszZ8am_vqNX3KAOff-psd3af7Xl3DL77KxJ-rWIAqIArQHzQIEBoX49mQA ...
- Atlantis
poj1151:http://poj.org/problem?id=1151 题意:求矩形面积的并题解:扫描线加线段树 #include<iostream> #include<cst ...
- linux eclipse c++配置
安装cdt: https://www.eclipse.org/cdt/downloads.php 新建一个c++工程,运行发生错误: Eclipse CDT launch failed.Binary ...
- Annikken Andee–Arduino与Android间的简易连接
一个Arduino的兼容板,允许你显示并控制来自Android设备的Arduino应用.无需Anroid APP开发. 点击:观看视频 什么是Annikken Andee? Annikken Ande ...
- DB_WRITER_PROCESSES与LOG_ARCHIVE_MAX_PROCESSES
DB_WRITER_PROCESSES Property Description Parameter type Integer Default value 1 or CPU_COUNT / 8, ...
- 【HDOJ】5179 beautiful number
DFS. /* 5179 */ #include <iostream> #include <algorithm> #include <map> #include & ...
- bootchart--检测linux启动性能的软件
bootchart--检测linux启动性能的软件 摘自http://www-128.ibm.com/developerworks/library/l-boot-faster/index.html?c ...
- MPI Maelstrom(Dijkstra)
http://poj.org/problem?id=1502 刷一道模板题稳定一下心情... Dijkstra求单源最短路,就是输入的时候注意下,是按下三角输入的(无向图),输入字符x表示i与j不通. ...
- bzoj2427
一开始读错题导致各种不会做,无奈其实是一道水题,缩点反向建图树形dp即可 type link=^point; point=record po:longint; next:link; end; ..] ...