android实现图片左右滑动效果:viewpager

看着大神在做项目,很多都不会的自己闲着多尴尬,看着博客上图片左右滑动效果,别人弄的好漂亮,自己不会,着急。于是自己也练练

在网上搜了下思路,好多都是使用viewpager+fragment。不过我这里仅仅使用了viewpager来实现。

注:本demo参考http://my.oschina.net/u/1409622/blog/178881?fromerr=HggRBpI8

                           http://www.it165.net/pro/html/201502/33844.html

                           http://blog.csdn.net/singwhatiwanna/article/details/8875241

都值得学习的大神!呵呵!码自其处!

不啰嗦了,贴码吧!

xml布局:

 
    
    

      
        
   	
    	 

 


实现方法:


public class HomePageActivity extends Activity implements OnPageChangeListener {
	
	String TAG = "HomePageActivity";

	private LinearLayout mLinearLayout;
	private ViewPager viewPager;
	private ImageView imageView;//显示数组图片的View
	private ImageView[] imgsArray;//图片数组
	private ImageView[] diandianArray;//点数组
	private int[] imgs = new int[]{R.drawable.baba,
								   R.drawable.ningm, 
								   R.drawable.feather, 
								   R.drawable.flowers,
								   R.drawable.lotus};//图片resource
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.home_page_activity);
         
		//xml中的LinearLayout布局
        mLinearLayout = (LinearLayout)findViewById(R.id.layout);
        viewPager = (ViewPager)findViewById(R.id.viewPager); 
        viewPager.setOnPageChangeListener(this);
        
        //设置图片resource的view
        imgsArray = new ImageView[imgs.length];
        for(int i = 0; i < imgsArray.length; i++){ 
        	imageView = new ImageView(this);
        	imgsArray[i] = imageView;
        	imageView.setBackgroundResource(imgs[i]);
        } 
        
        //设置点点的view
        diandianArray = new ImageView[imgs.length];
        for(int i = 0 ; i < diandianArray.length; i++){
        	imageView = new ImageView(this);  
        	diandianArray[i] = imageView;
        	if(i == 0){
        		diandianArray[i].setBackgroundResource(R.drawable.pagedot);
	    	}else{ 
	    		diandianArray[i].setBackgroundResource(R.drawable.pagedotfocus);
	    	}  

    		Log.e(TAG, "diandianArray:" + diandianArray);
			 //此处通过方法来设置点点的布局属性,并将点点的布局属性放入到xml中的mLinearLayout,显示出来
        	LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
        	linearLayout.width = 15;
        	linearLayout.height = 15;
        	linearLayout.leftMargin = 5;
        	linearLayout.rightMargin = 5; 
        	linearLayout.bottomMargin = 3;
        	mLinearLayout.addView(imageView,linearLayout); 
        } 
        viewPager.setAdapter(new ContentAdapter());
    } 
	 
	@Override
	public void onPageScrollStateChanged(int arg0) {
		Log.e(TAG, "onPageScrollStateChanged()方法有没有响应啊,吱一声啊!1");
	} 

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		Log.e(TAG, "onPageScrolled()方法有没有响应啊,吱一声啊!2");
		
	}

	@Override
	public void onPageSelected(int position) {
		Log.e(TAG, "onPageSelected()方法有没有响应啊,吱一声啊!3");
		for(int i = 0; i < diandianArray.length; i++){
			diandianArray[position].setBackgroundResource(R.drawable.pagedot);
	    	if(i != position){
	    		diandianArray[i].setBackgroundResource(R.drawable.pagedotfocus);
	    	} 
		}
	}

	public class ContentAdapter extends PagerAdapter{ 
		
		@Override
		public int getCount() {
			return imgs.length  ;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(imgsArray[position]);
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) { 
			container.addView(imgsArray[position],0);
			return imgsArray[position];  
		} 
	}
	
	public void  OnPageChangeListener(){
		 Intent mIntent = new Intent(this,CalendarActivity.class);
		 startActivity(mIntent);
	}
	
}
 


好了,给大家看一下效果图吧!


android实现图片左右滑动效果:viewpager_第1张图片


android实现图片左右滑动效果:viewpager_第2张图片


没事,多练练,反正闲着也是闲着!整天嘻嘻哈哈见到风儿就气浪。。。。。。。。(*^__^*) 嘻嘻……




你可能感兴趣的:(android实现图片左右滑动效果:viewpager)