切换图片(底部带有小点效果)

下面我们要实现这样的效果:



我们将采用两种方式实现这种效果:
1.使用ViewPager:
思路:ViewPager提供左右滑动图片操作的支持,下方小点在代码中动态创建,整个布局采用FrameLayout。
先看布局:
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/black"  
  6.     tools:context=".MainActivity" >  
  7.     <android.support.v4.view.ViewPager  
  8.         android:id="@+id/viewPager"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         ></android.support.v4.view.ViewPager>  
  12.     <RelativeLayout   
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         >  
  16.         <LinearLayout   
  17.             android:id="@+id/viewGroup"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_alignParentBottom="true"  
  21.             android:layout_marginBottom="30dp"  
  22.             android:gravity="center_horizontal"  
  23.             android:orientation="horizontal"  
  24.             ></LinearLayout>  
  25.     </RelativeLayout>  
  26. </FrameLayout>  
再看代码:

  1. package com.example.splash_viewpager;  
  2. import android.app.Activity;  
  3. import android.content.res.Resources;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.os.Bundle;  
  7. import android.support.v4.view.PagerAdapter;  
  8. import android.support.v4.view.ViewPager;  
  9. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.widget.ImageView;  
  14. import android.widget.LinearLayout;  
  15. public class MainActivity extends Activity implements OnPageChangeListener  
  16. {  
  17.     private ViewPager mViewPager = null;  
  18.     private LinearLayout mViewGroup = null;  
  19.       
  20.     private int[] mImageIds = {R.drawable.bg1,R.drawable.bg2,  
  21.             R.drawable.bg3,R.drawable.bg4,  
  22.             R.drawable.bg5,R.drawable.bg6,  
  23.             R.drawable.bg7};  
  24.       
  25.     private ImageView[] mImageViews = null;  
  26.       
  27.     private ImageView[] mTips = null;  
  28.       
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState)  
  31.     {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.           
  35.         mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);  
  36.         mViewPager = (ViewPager) findViewById(R.id.viewPager);  
  37.           
  38.         mTips = new ImageView[mImageIds.length];  
  39.         //动态创建小点并加到布局中  
  40.         for(int i = 0; i < mTips.length; i++)  
  41.         {  
  42.             ImageView iv = new ImageView(this);  
  43.             iv.setLayoutParams(new LayoutParams(10,10));  
  44.             mTips[i] = iv;  
  45.               
  46.             if(i == 0)  
  47.             {  
  48.                 iv.setBackgroundResource(R.drawable.page_indicator_focused);  
  49.             }else  
  50.             {  
  51.                 iv.setBackgroundResource(R.drawable.page_indicator_unfocused);  
  52.             }  
  53.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
  54.             lp.leftMargin = 5;  
  55.             lp.rightMargin = 5;  
  56.             mViewGroup.addView(iv,lp);  
  57.         }  
  58.         mImageViews = new ImageView[mImageIds.length];  
  59.         for(int i = 0; i < mImageViews.length; i++)  
  60.         {  
  61.             ImageView iv = new ImageView(this);  
  62.             mImageViews[i] = iv;  
  63.             int reqWidth = getWindowManager().getDefaultDisplay().getWidth();  
  64.             int reqHeight = getWindowManager().getDefaultDisplay().getHeight();  
  65.             iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mImageIds[i], reqWidth, reqHeight));  
  66.         }  
  67.           
  68.         mViewPager.setAdapter(new MyPagerAdapter());  
  69.         mViewPager.setOnPageChangeListener(this);  
  70.           
  71.     }  
  72.       
  73.     class MyPagerAdapter extends PagerAdapter  
  74.     {  
  75.         @Override  
  76.         public int getCount()  
  77.         {  
  78.             return mImageIds.length;  
  79.         }  
  80.         @Override  
  81.         public boolean isViewFromObject(View arg0, Object arg1)  
  82.         {  
  83.             return arg0 == arg1;  
  84.         }  
  85.         @Override  
  86.         public Object instantiateItem(ViewGroup container, int position)  
  87.         {  
  88.             try  
  89.             {  
  90.                 container.addView(mImageViews[position]);  
  91.             } catch (Exception e)  
  92.             {  
  93.             }  
  94.             return mImageViews[position];  
  95.         }  
  96.         @Override  
  97.         public void destroyItem(ViewGroup container, int position, Object object)  
  98.         {  
  99.         }  
  100.     }  
  101.     @Override  
  102.     public void onPageSelected(int arg0)  
  103.     {  
  104.         for(int i = 0; i < mTips.length; i++)  
  105.         {  
  106.             if(arg0 == i)  
  107.             {  
  108.                 mTips[i].setBackgroundResource(R.drawable.page_indicator_focused);  
  109.             }else  
  110.             {  
  111.                 mTips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);  
  112.             }  
  113.         }      
  114.     }  
  115.     @Override  
  116.     public void onPageScrollStateChanged(int arg0)  
  117.     {  
  118.     }  
  119.     @Override  
  120.     public void onPageScrolled(int arg0, float arg1, int arg2)  
  121.     {  
  122.     }  
  123.       
  124.     private static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight)  
  125.     {  
  126.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  127.         opts.inJustDecodeBounds = true;  
  128.         BitmapFactory.decodeResource(res, resId);  
  129.         int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);  
  130.         opts.inSampleSize = inSampleSize;  
  131.         opts.inJustDecodeBounds = false;  
  132.         return BitmapFactory.decodeResource(res,resId,opts);  
  133.     }  
  134.       
  135.     private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)  
  136.     {  
  137.         if(opts == null)  
  138.         {  
  139.             return 1;  
  140.         }  
  141.         int inSampleSize = 1;  
  142.         int realWidth = opts.outWidth;  
  143.         int realHeight = opts.outHeight;  
  144.           
  145.         if(realWidth > width || realHeight > height)  
  146.         {  
  147.             int heightRatio = realHeight/height;  
  148.             int widthRatio = realWidth/width;  
  149.               
  150.             inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;  
  151.         }  
  152.         return inSampleSize;  
  153.     }  
  154. }  

2.使用ViewFlipper
思路:在ViewFlipper中填充多个ImageView,然后使用GestureDetector检测手势,小点仍然是通过动态产生。
布局:

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/black"  
  6.     tools:context=".MainActivity" >  
  7.       
  8.     <ViewFlipper   
  9.         android:id="@+id/vf"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         ></ViewFlipper>  
  13.     <RelativeLayout   
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         >  
  17.         <LinearLayout   
  18.             android:id="@+id/viewGroup"  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:orientation="horizontal"  
  22.             android:layout_alignParentBottom="true"  
  23.             android:layout_marginBottom="30dp"  
  24.             android:gravity="center_horizontal"  
  25.             ></LinearLayout>          
  26.     </RelativeLayout>  
  27. </FrameLayout>  

代码:

  1. package com.example.splash_viewflipper;  
  2. import android.app.Activity;  
  3. import android.content.res.Resources;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.os.Bundle;  
  7. import android.view.GestureDetector;  
  8. import android.view.GestureDetector.OnGestureListener;  
  9. import android.view.MotionEvent;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.AnimationUtils;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.LinearLayout.LayoutParams;  
  15. import android.widget.Toast;  
  16. import android.widget.ViewFlipper;  
  17. public class MainActivity extends Activity implements OnGestureListener  
  18. {  
  19.     private ViewFlipper mViewFlipper = null;  
  20.     private LinearLayout mViewGroup = null;  
  21.       
  22.     private GestureDetector mGestureDetector = null;  
  23.       
  24.     private int[] mImageIds = {  
  25.             R.drawable.bg1,R.drawable.bg2,  
  26.             R.drawable.bg3,R.drawable.bg4,  
  27.             R.drawable.bg5,R.drawable.bg6,  
  28.             R.drawable.bg7  
  29.     };  
  30.       
  31.     private ImageView[] mImageViews = null;  
  32.     private ImageView[] mTips = null;  
  33.       
  34.     private int currentIndex = 0;  
  35.       
  36.     @Override  
  37.     protected void onCreate(Bundle savedInstanceState)  
  38.     {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.           
  42.         mViewFlipper = (ViewFlipper) findViewById(R.id.vf);  
  43.         mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);  
  44.           
  45.         mGestureDetector = new GestureDetector(this,this);  
  46.           
  47.         mImageViews = new ImageView[mImageIds.length];  
  48.         for(int i = 0; i < mImageViews.length; i++)  
  49.         {  
  50.             ImageView iv = new ImageView(this);  
  51.             int reqWidth = getWindowManager().getDefaultDisplay().getWidth();  
  52.             int reqHeight = getWindowManager().getDefaultDisplay().getHeight();  
  53.             iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(),mImageIds[i], reqWidth, reqHeight));  
  54.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
  55.             mViewFlipper.addView(iv,lp);  
  56.         }  
  57.           
  58.         mTips = new ImageView[mImageIds.length];  
  59.         for(int i = 0; i < mTips.length; i++)  
  60.         {  
  61.             ImageView iv = new ImageView(this);  
  62.             iv.setLayoutParams(new LayoutParams(10,10));  
  63.             mTips[i] = iv;  
  64.               
  65.             if(i == 0)  
  66.             {  
  67.                 iv.setBackgroundResource(R.drawable.page_indicator_focused);  
  68.             }else  
  69.             {  
  70.                 iv.setBackgroundResource(R.drawable.page_indicator_unfocused);  
  71.             }  
  72.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
  73.             lp.leftMargin = 5;  
  74.             lp.rightMargin = 5;  
  75.             mViewGroup.addView(iv,lp);  
  76.         }  
  77.     }  
  78.       
  79.     @Override  
  80.     public boolean onTouchEvent(MotionEvent event)  
  81.     {  
  82.         return mGestureDetector.onTouchEvent(event);  
  83.     }  
  84.       
  85.     private static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight)  
  86.     {  
  87.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  88.         opts.inJustDecodeBounds = true;  
  89.         BitmapFactory.decodeResource(res, resId);  
  90.         int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);  
  91.         opts.inSampleSize = inSampleSize;  
  92.         opts.inJustDecodeBounds = false;  
  93.         return BitmapFactory.decodeResource(res,resId,opts);  
  94.     }  
  95.       
  96.     private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)  
  97.     {  
  98.         if(opts == null)  
  99.         {  
  100.             return 1;  
  101.         }  
  102.         int inSampleSize = 1;  
  103.         int realWidth = opts.outWidth;  
  104.         int realHeight = opts.outHeight;  
  105.           
  106.         if(realWidth > width || realHeight > height)  
  107.         {  
  108.             int heightRatio = realHeight/height;  
  109.             int widthRatio = realWidth/width;  
  110.               
  111.             inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;  
  112.         }  
  113.         return inSampleSize;  
  114.     }  
  115.     @Override  
  116.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  117.             float velocityY)  
  118.     {  
  119.         if(e1.getX() - e2.getX() > 120)//显示下一张  
  120.         {  
  121.             if(currentIndex != mTips.length-1)  
  122.             {  
  123.                 currentIndex++;  
  124.                 setImageBackground(currentIndex);  
  125.                 Animation in_right = AnimationUtils.loadAnimation(this,R.anim.in_right_);  
  126.                 Animation out_left = AnimationUtils.loadAnimation(this,R.anim.out_left_);  
  127.                 mViewFlipper.setInAnimation(in_right);  
  128.                 mViewFlipper.setOutAnimation(out_left);  
  129.                 mViewFlipper.showNext();  
  130.             }else  
  131.             {  
  132.                 Toast.makeText(this,"已经是最后一张..",0).show();  
  133.             }  
  134.         }else if(e1.getX() - e2.getX() < -120)//显示上一张  
  135.         {  
  136.             if(currentIndex != 0)  
  137.             {  
  138.                 currentIndex--;  
  139.                 setImageBackground(currentIndex);  
  140.                 Animation in_left = AnimationUtils.loadAnimation(this,R.anim.in_left_);  
  141.                 Animation out_right = AnimationUtils.loadAnimation(this,R.anim.out_right_);  
  142.                 mViewFlipper.setInAnimation(in_left);  
  143.                 mViewFlipper.setOutAnimation(out_right);  
  144.                 mViewFlipper.showPrevious();  
  145.             }else  
  146.             {  
  147.                 Toast.makeText(this,"已经是第一张..",0).show();  
  148.             }  
  149.         }  
  150.         return true;  
  151.     }  
  152.     private void setImageBackground(int selectItems)  
  153.     {      
  154.         for(int i = 0; i < mTips.length; i++)  
  155.         {      
  156.             if(i == selectItems)  
  157.             {      
  158.                 mTips[i].setBackgroundResource(R.drawable.page_indicator_focused);      
  159.             }else  
  160.             {      
  161.                 mTips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);      
  162.             }      
  163.         }      
  164.     }  
  165.     @Override  
  166.     public boolean onDown(MotionEvent e)  
  167.     {  
  168.         return false;  
  169.     }  
  170.     @Override  
  171.     public void onShowPress(MotionEvent e)  
  172.     {  
  173.     }  
  174.     @Override  
  175.     public boolean onSingleTapUp(MotionEvent e)  
  176.     {  
  177.         return false;  
  178.     }  
  179.     @Override  
  180.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  181.             float distanceY)  
  182.     {  
  183.         return false;  
  184.     }  
  185.     @Override  
  186.     public void onLongPress(MotionEvent e)  
  187.     {  
  188.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值