Android ViewPager引导页功能(动态加载指示器) Android开发实现的ViewPager引导页功能(动态加载指示器)详解
飘走的我 人气:0本文实例讲述了Android开发实现的ViewPager引导页功能(动态加载指示器)。分享给大家供大家参考,具体如下:
先看效果图咯~
现在几乎每个App都会有引导页,是不是感觉很炫很厉害,所以就想做出来一个学习一下~让自己的App看起来更加的美观~
现在来分析一下:
这个引导页可以分为俩部分~
1.小红点--来提醒这是第几页了~
2.“开始体验”这个Button--可以进入主界面,但是要控制这个Button只能在最后一页出现
布局的话使用相对布局~
那现在来看看布局吧:
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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" /> <RelativeLayout android:id="@+id/rl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"> <LinearLayout android:id="@+id/linear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </RelativeLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp" android:visibility="invisible" android:text="开始体验"/> </RelativeLayout>
开始体验这个Button设置了不可见来控制在最后一页出现~
小圆点还没开始写出来,只是写了个RelativeLayout嵌套了一个LinearLayout
然后在分析这个页面的滑动~
先把要滑动的图片放进来:
private ViewPager viewPager; private int[]images={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3}; private List<ImageView>imageViews;//用来存放几个imageview的实例
实例化并且加载适配器:
viewPager.setAdapter(new MyAdapter()); imageViews=new ArrayList<ImageView>(); for(int i=0;i<images.length;i++){ ImageView imageView=new ImageView(this); imageView.setImageResource(images[i]); imageViews.add(imageView);
class MyAdapter extends PagerAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return images.length; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(imageViews.get(position)); return imageViews.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView((View)object); } }
其实写到这里就可以完成图片的滑动,但是还没有小红点的出现~这时候就要写小红点了~小红点默认是在第一页就出现了的~
在drawable文件下写了俩个xml文件,red_circle--代表红点,gray_circle--代表灰点~
red_circle:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <!-- 填充颜色 --> <solid android:color="#ff0000"/> </shape>
gray_circle:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <!-- 填充颜色 --> <solid android:color="#ECECEC"/> </shape>
现在代表颜色的文件写好了,那怎么把它们加进去呢~~
先默认都是灰点,然后在把红点覆盖上去,看代码怎么写:
ImageView gray_Iv=new ImageView(this); gray_Iv.setImageResource(R.drawable.gray_circle); //使用LayoutParams改变控件的位置 LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if(i>0){ layoutParams.leftMargin=20; } gray_Iv.setLayoutParams(layoutParams); li.addView(gray_Iv);
把灰点都加到LinearLayout里面
而红点:
red_Iv=new ImageView(this); red_Iv.setImageResource(R.drawable.red_circle); rl.addView(red_Iv);
把红点加到RelativeLayout里面
还要写个示图树~让红点来滑动:
//示图树 red_Iv.getViewTreeObserver(). addOnGlobalLayoutListener(new OnGlobalLayoutListener() { //该方法就是在界面全面绘制结束之后回调 @Override public void onGlobalLayout() { // TODO Auto-generated method stub //求距离 left=li.getChildAt(1).getLeft()-li.getChildAt(0).getLeft(); System.out.println("left为"+left); red_Iv.getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); viewPager的滑动监听器还要监听图片什么时候滑动,以至于让红点滑动的跟好看(就比如第一个点到第二个点的途中也会有红点)
//滑动的时候 @Override public void onPageScrolled(int position, float posionOffset, int arg2) { // TODO Auto-generated method stub System.out.println(posionOffset);//滑动的百分比 RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams)red_Iv.getLayoutParams(); layoutParams.leftMargin=(int)(left*posionOffset+position*left); red_Iv.setLayoutParams(layoutParams); }
还要分析下让“开始体验”这个Button出现在最后一页,那就只能在viewPager的滑动监听方法里面写可见与不可见了~
@Override public void onPageSelected(int position) { // TODO Auto-generated method stub if(position==images.length-1){ btn.setVisibility(View.VISIBLE); }else{ btn.setVisibility(View.GONE); } }
完整MainActicity代码:
public class MainActivity extends Activity { private ViewPager viewPager; private int[]images={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3}; private List<ImageView>imageViews;//用来存放几个imageview的实例 private LinearLayout li; private RelativeLayout rl; private ImageView red_Iv; private int left; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); viewPager=(ViewPager) findViewById(R.id.viewPager); li=(LinearLayout) findViewById(R.id.linear); rl=(RelativeLayout) findViewById(R.id.rl); btn=(Button) findViewById(R.id.btn); viewPager.setAdapter(new MyAdapter()); imageViews=new ArrayList<ImageView>(); for(int i=0;i<images.length;i++){ ImageView imageView=new ImageView(this); imageView.setImageResource(images[i]); imageViews.add(imageView); ImageView gray_Iv=new ImageView(this); gray_Iv.setImageResource(R.drawable.gray_circle); //使用LayoutParams改变控件的位置 LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if(i>0){ layoutParams.leftMargin=20; } gray_Iv.setLayoutParams(layoutParams); li.addView(gray_Iv); } red_Iv=new ImageView(this); red_Iv.setImageResource(R.drawable.red_circle); rl.addView(red_Iv); //示图树 red_Iv.getViewTreeObserver(). addOnGlobalLayoutListener(new OnGlobalLayoutListener() { //该方法就是在界面全面绘制结束之后回调 @Override public void onGlobalLayout() { // TODO Auto-generated method stub //求距离 left=li.getChildAt(1).getLeft()-li.getChildAt(0).getLeft(); System.out.println("left为"+left); red_Iv.getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { // TODO Auto-generated method stub if(position==images.length-1){ btn.setVisibility(View.VISIBLE); }else{ btn.setVisibility(View.GONE); } } //滑动的时候 @Override public void onPageScrolled(int position, float posionOffset, int arg2) { // TODO Auto-generated method stub System.out.println(posionOffset);//滑动的百分比 RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams)red_Iv.getLayoutParams(); layoutParams.leftMargin=(int)(left*posionOffset+position*left); red_Iv.setLayoutParams(layoutParams); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } class MyAdapter extends PagerAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return images.length; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(imageViews.get(position)); return imageViews.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView((View)object); } } }
PS:这里使用到了android-support-v4.jar文件,需要在项目中导入jar包,可参考本站相关教程https://m.qb5200.com/www.qb5200.com/softjc/552873.html
附:完整实例代码点击此处本站下载。
希望本文所述对大家Android程序设计有所帮助。
加载全部内容