标签:
github 地址: https://github.com/Trinea/android-auto-scroll-view-pager
Gradle:
compile (‘cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2‘) { exclude module: ‘support-v4‘ }
-keep class cn.trinea.android.** { *; } -keepclassmembers class cn.trinea.android.** { *; } -dontwarn cn.trinea.android.**
布局文件
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager android:id="@+id/auto_view_pager" android:layout_width="match_parent" android:layout_height="320dp" /> <LinearLayout android:id="@+id/point_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:layout_alignParentBottom="true" android:orientation="horizontal" android:paddingBottom="5dp" >
代码设置:
if(imageList.size()==1){ auto_view_pager.stopAutoScroll(); } auto_view_pager.setAdapter(new MyAdapter(imageList)); // //设置延时时间 auto_view_pager.setInterval(5000); //设置轮播的方向 AutoScrollViewPager.RIGHT/AutoScrollViewPager.LEFT auto_view_pager.setDirection(AutoScrollViewPager.RIGHT); //设置是否自动循环轮播,默认为true //注意:一旦设为true,则不能和ViewPagerIndicator一起使用 auto_view_pager.setCycle(true); //设置切换动画的时长 auto_view_pager.setScrollDurationFactor(5); //设置当滑动到最后一个或者第一个时,如何切换下一张 //当滑动到最后一张或第一张时是否开启动画,默认为true auto_view_pager.setBorderAnimation(true); //当触摸的时候,停止轮播 auto_view_pager.setStopScrollWhenTouch(true); // // //解决最后一个跳转到第一个闪动问题 // auto_view_pager.setCurrentItem((Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % imageList.size())); auto_view_pager.setCurrentItem(imageList.size()*10); final LinearLayout point_group = (LinearLayout) findViewById(R.id.point_group); point_group.removeAllViews(); for (int i = 0; i < imageList.size(); i++) { // 添加指示点 ImageView point = new ImageView(DetailActivity.this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.rightMargin = 10; point.setLayoutParams(params); point.setBackgroundResource(R.drawable.point_bg); if (i == 0) { point.setEnabled(true); } else { point.setEnabled(false); } point_group.addView(point); } // auto_view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { Log.e("TAG", "position" + position); // 改变指示点的状态 // 把当前点enbale 为true try { point_group.getChildAt(position % imageList.size()).setEnabled(true); // 把上一个点设为false point_group.getChildAt(lastPosition).setEnabled(false); lastPosition = position % imageList.size(); } catch (Exception e) { } } @Override public void onPageScrollStateChanged(int state) { } });
adapter
class MyAdapter extends PagerAdapter { List<String> mSimpleDraweeViewList; public MyAdapter(List<String> mSimpleDraweeViewList) { this.mSimpleDraweeViewList=mSimpleDraweeViewList; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { Uri uri = Uri.parse(HttpBase.HOST_STRING +"/"+ mSimpleDraweeViewList.get(position % mSimpleDraweeViewList.size())); // SimpleDraweeView draweeView = new SimpleDraweeView(DetailActivity.this); SimpleDraweeView draweeView = (SimpleDraweeView)View.inflate(DetailActivity.this,R.layout.layout_image,null).findViewById(R.id.image1); draweeView.setScaleType( ImageView.ScaleType.FIT_XY); ViewGroup.LayoutParams imagebtn_params = new ViewGroup.LayoutParams( AutoUtils.getDisplayWidthValue(720), AutoUtils.getDisplayHeightValue(640)); draweeView.setLayoutParams(imagebtn_params); draweeView.setImageURI(uri); // ImageView im= new ImageView(DetailActivity.this); // Picasso.with(DetailActivity.this).load(HttpBase.HOST_STRING +"/"+ mSimpleDraweeViewList.get(position % mSimpleDraweeViewList.size())).into(im); ViewParent parent = draweeView.getParent(); // remove掉View之前已经加到一个父控件中,否则报异常 if (parent != null) { ViewGroup group = (ViewGroup) parent; group.removeView(draweeView); } container.addView(draweeView); return draweeView; // container.addView(images.get(position % images.size())); // return images.get(position % images.size()); // container.addView(draweeView); // return draweeView; } }
@Override protected void onResume() { super.onResume(); //开启自动轮播,延时时间为getInterval() auto_view_pager.startAutoScroll(); //开启自动轮播,并设置延时 // auto_view_pager.startAutoScroll(delayTime); } @Override protected void onPause() { super.onPause(); //停止轮播 auto_view_pager.stopAutoScroll(); }
point_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/point_nomal" android:state_enabled="true"/> <item android:drawable="@drawable/point_focured" android:state_enabled="false"/> </selector>
point_focured.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:width="5dp" android:height="5dp"/> <solid android:color="#33000000"/> </shape>
point_nomal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:width="5dp" android:height="5dp"/> <solid android:color="#aaFFFFFF"/> </shape>
android-auto-scroll-view-pager (无限广告轮播图)
标签:
原文地址:http://www.cnblogs.com/java-g/p/5691541.html