Android ViewPager实现图片轮翻效果
很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的。今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的状态也在变化),用定时器来实现不难。
importjava.util.ArrayList; importjava.util.List; importandroid.app.Activity; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.os.SystemClock; importandroid.support.v4.view.PagerAdapter; importandroid.support.v4.view.ViewPager; importandroid.support.v4.view.ViewPager.OnPageChangeListener; importandroid.util.Log; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.ImageView; importandroid.widget.LinearLayout; importandroid.widget.LinearLayout.LayoutParams; importandroid.widget.TextView; publicclassMainActivityextendsActivityimplementsOnPageChangeListener{ protectedstaticfinalStringTAG="MainActivity"; privateList<ImageView>imageViewList; privateViewPagermViewPager; privatefinalint[]imageResIDs={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; privatefinalString[]imageDescriptions={ "巩俐不低俗,我就不能低俗", "扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" }; privateTextViewtvImageDescription;//图片描述 privateLinearLayoutllPointGroup;//点控件的组 privateintpreviousPosition=0;//viewpager选中的前一个position privatebooleanisStop=false;//控制循环的子线程是否停止 privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1); } }; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); newThread(newRunnable(){ @Override publicvoidrun(){ //每两秒钟向主线程发送一条消息,切换viewpager的界面 while(!isStop){ SystemClock.sleep(2000); handler.sendEmptyMessage(0); } Log.i(TAG,"循环线程停止了"); }}).start(); } @Override protectedvoidonDestroy(){ isStop=true; super.onDestroy(); } privatevoidinitView(){ mViewPager=(ViewPager)findViewById(R.id.viewpager); llPointGroup=(LinearLayout)findViewById(R.id.ll_point_group); tvImageDescription=(TextView)findViewById(R.id.tv_image_description); imageViewList=newArrayList<ImageView>(); ImageViewiv; ViewpointView; LayoutParamsparams; for(inti=0;i<imageResIDs.length;i++){ iv=newImageView(this); iv.setBackgroundResource(imageResIDs[i]); imageViewList.add(iv); //根据图片添加点 pointView=newView(this); params=newLayoutParams(5,5); params.leftMargin=5; pointView.setLayoutParams(params); pointView.setEnabled(false); pointView.setBackgroundResource(R.drawable.point_bg); llPointGroup.addView(pointView); } MyPagerAdaptermAdapter=newMyPagerAdapter(); mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(this); intitem=(Integer.MAX_VALUE/2)-((Integer.MAX_VALUE/2)%imageViewList.size()); mViewPager.setCurrentItem(item);//设置当前选中的item的position tvImageDescription.setText(imageDescriptions[previousPosition]); llPointGroup.getChildAt(previousPosition).setEnabled(true);//第一点被选中 } classMyPagerAdapterextendsPagerAdapter{ @Override publicintgetCount(){ returnInteger.MAX_VALUE; } /** *移动的对象和进来的对象如果是同一个就返回true,代表复用view对象 *false使用object对象 */ @Override publicbooleanisViewFromObject(Viewarg0,Objectarg1){ returnarg0==arg1; } /** *需要销毁的对象的position传进来 */ @Override publicvoiddestroyItem(ViewGroupcontainer,intposition,Objectobject){ //移除掉指定position的对象 mViewPager.removeView(imageViewList.get(position%imageViewList.size())); } /** *加载position位置的view对象 */ @Override publicObjectinstantiateItem(ViewGroupcontainer,intposition){ //添加指定position的对象 mViewPager.addView(imageViewList.get(position%imageViewList.size())); returnimageViewList.get(position%imageViewList.size()); } } @Override publicvoidonPageScrollStateChanged(intarg0){ //TODOAuto-generatedmethodstub } @Override publicvoidonPageScrolled(intarg0,floatarg1,intarg2){ //TODOAuto-generatedmethodstub } /** *当viewpager页面切换时回调 *@paramposition就是当前显示的page页 */ @Override publicvoidonPageSelected(intposition){ tvImageDescription.setText(imageDescriptions[position%imageViewList.size()]); llPointGroup.getChildAt(position%imageViewList.size()).setEnabled(true); llPointGroup.getChildAt(previousPosition).setEnabled(false); previousPosition=position%imageViewList.size(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。