diff --git a/README.md b/README.md index 1297cea..6afe0c3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Android图片轮播控件 *如果对你有帮助请star哦!* [![Apache 2.0 License](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0.html) +
现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能。因为ViewPager并不支持循环翻页, 所以要实现循环还得需要自己去动手,我就把项目中的控件剔了出来,希望大家觉得有用。目前框架可以进行不同样式、不同动画设置, @@ -78,7 +79,6 @@ compile project(':banner') |setOnBannerClickListener(this)|设置点击事件,下标是从1开始 |setOnBannerImageListener(this)|设置图片加载事件,可以自定义图片加载方式 |setOnPageChangeListener(this)|设置viewpager的滑动监听 -|setScrollerTime(int duration)|设置viewpager的切换速度,(单位毫秒,默认800) |setBannerAnimation(Class transformer)|设置viewpager的默认动画,传值见动画表 |setPageTransformer(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer)|设置viewpager的自定义动画 diff --git a/app/app.iml b/app/app.iml index 33b782c..94924e3 100644 --- a/app/app.iml +++ b/app/app.iml @@ -103,8 +103,8 @@ - + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 00b54bb..107431b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,8 +19,8 @@ - - + + \ No newline at end of file diff --git a/app/src/main/java/com/test/banner/BannerActivity.java b/app/src/main/java/com/test/banner/BannerActivity.java index a5c6c52..32fb29a 100644 --- a/app/src/main/java/com/test/banner/BannerActivity.java +++ b/app/src/main/java/com/test/banner/BannerActivity.java @@ -7,6 +7,7 @@ import com.youth.banner.Banner; import com.youth.banner.BannerConfig; +import com.youth.banner.listener.OnBannerClickListener; public class BannerActivity extends AppCompatActivity { @Override @@ -14,7 +15,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner); getSupportActionBar().setTitle(getIntent().getStringExtra("des")); - String[] images= getResources().getStringArray(R.array.url); + String[] images= getResources().getStringArray(R.array.url2); String[] titles= getResources().getStringArray(R.array.title); int position=getIntent().getIntExtra("position",0); Banner banner = (Banner) findViewById(R.id.banner1); @@ -56,9 +57,9 @@ protected void onCreate(Bundle savedInstanceState) { break; } banner.setImages(images);//可以选择设置图片网址,或者资源文件,默认用Glide加载 - banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {//设置点击事件 + banner.setOnBannerClickListener(new OnBannerClickListener() {//设置点击事件 @Override - public void OnBannerClick(View view, int position) { + public void OnBannerClick(int position) { Toast.makeText(getApplicationContext(),"你点击了:"+position,Toast.LENGTH_LONG).show(); } }); diff --git a/app/src/main/java/com/test/banner/Banner3Activity.java b/app/src/main/java/com/test/banner/BannerCustomActivity.java similarity index 68% rename from app/src/main/java/com/test/banner/Banner3Activity.java rename to app/src/main/java/com/test/banner/BannerCustomActivity.java index c63ab82..57adc3b 100644 --- a/app/src/main/java/com/test/banner/Banner3Activity.java +++ b/app/src/main/java/com/test/banner/BannerCustomActivity.java @@ -2,22 +2,16 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; -import android.view.View; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.Toast; -import com.bumptech.glide.Glide; import com.youth.banner.Banner; import com.youth.banner.BannerConfig; -public class Banner3Activity extends AppCompatActivity { +public class BannerCustomActivity extends AppCompatActivity { private Banner banner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_banner3); + setContentView(R.layout.activity_banner_custom); getSupportActionBar().setTitle(getIntent().getStringExtra("des")); String[] images2= getResources().getStringArray(R.array.url2); banner = (Banner) findViewById(R.id.banner3); diff --git a/app/src/main/java/com/test/banner/Banner2Activity.java b/app/src/main/java/com/test/banner/BannerSeniorActivity.java similarity index 79% rename from app/src/main/java/com/test/banner/Banner2Activity.java rename to app/src/main/java/com/test/banner/BannerSeniorActivity.java index cf1a7da..8145cea 100644 --- a/app/src/main/java/com/test/banner/Banner2Activity.java +++ b/app/src/main/java/com/test/banner/BannerSeniorActivity.java @@ -1,6 +1,5 @@ package com.test.banner; -import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; @@ -13,11 +12,10 @@ import com.youth.banner.Banner; import com.youth.banner.BannerConfig; import com.youth.banner.Transformer; +import com.youth.banner.listener.OnBannerClickListener; +import com.youth.banner.listener.OnLoadImageListener; -import java.util.Arrays; -import java.util.List; - -public class Banner2Activity extends AppCompatActivity implements View.OnClickListener { +public class BannerSeniorActivity extends AppCompatActivity implements View.OnClickListener { private Banner banner; private Button refresh,stop,start; boolean flag=true; @@ -25,7 +23,7 @@ public class Banner2Activity extends AppCompatActivity implements View.OnClickLi @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_banner2); + setContentView(R.layout.activity_banner_senior); getSupportActionBar().setTitle(getIntent().getStringExtra("des")); images= getResources().getStringArray(R.array.url); images2= getResources().getStringArray(R.array.url2); @@ -46,16 +44,14 @@ protected void onCreate(Bundle savedInstanceState) { //设置轮播间隔时间 banner.setDelayTime(3000); //设置动画 - banner.setBannerAnimation(Transformer.ZoomIn); - //设置ViewPager的切换速度 - banner.setScrollerTime(2000); + banner.setBannerAnimation(Transformer.CubeOut); /** - * 可以选择设置图片网址,或者资源文件,默认用Glide加载 + * 可以选择设置图片网址或者资源文件,默认用Glide加载 * 如果你想设置默认图片就在xml里设置default_image * banner.setImages(images); */ //如果你想用自己项目的图片加载,那么----->自定义图片加载框架 - banner.setImages(images, new Banner.OnLoadImageListener() { + banner.setImages(images, new OnLoadImageListener() { @Override public void OnLoadImage(ImageView view, Object url) { /** @@ -63,15 +59,13 @@ public void OnLoadImage(ImageView view, Object url) { */ Glide.with(getApplicationContext()) .load(url) - .centerCrop() - .placeholder(R.mipmap.loading2) - .crossFade() .into(view); } }); - banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {//设置点击事件 + //设置点击事件 + banner.setOnBannerClickListener(new OnBannerClickListener() { @Override - public void OnBannerClick(View view, int position) { + public void OnBannerClick(int position) { Toast.makeText(getApplicationContext(),"你点击了:"+position,Toast.LENGTH_LONG).show(); } }); diff --git a/app/src/main/java/com/test/banner/MainActivity.java b/app/src/main/java/com/test/banner/MainActivity.java index a983019..1fb3ce2 100644 --- a/app/src/main/java/com/test/banner/MainActivity.java +++ b/app/src/main/java/com/test/banner/MainActivity.java @@ -1,18 +1,12 @@ package com.test.banner; -import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; -import android.view.Gravity; import android.view.View; import android.widget.AdapterView; -import android.widget.ImageView; import android.widget.ListView; -import android.widget.Toast; -import com.bumptech.glide.Glide; import com.youth.banner.Banner; @@ -37,10 +31,10 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (position == des.length - 1) { - startActivity(new Intent(this, Banner2Activity.class) + startActivity(new Intent(this, BannerSeniorActivity.class) .putExtra("des", des[position])); } else if (position == des.length - 2) { - startActivity(new Intent(this, Banner3Activity.class) + startActivity(new Intent(this, BannerCustomActivity.class) .putExtra("des", des[position])); } else { startActivity(new Intent(this, BannerActivity.class) diff --git a/app/src/main/res/layout/activity_banner3.xml b/app/src/main/res/layout/activity_banner_custom.xml similarity index 100% rename from app/src/main/res/layout/activity_banner3.xml rename to app/src/main/res/layout/activity_banner_custom.xml diff --git a/app/src/main/res/layout/activity_banner2.xml b/app/src/main/res/layout/activity_banner_senior.xml similarity index 100% rename from app/src/main/res/layout/activity_banner2.xml rename to app/src/main/res/layout/activity_banner_senior.xml diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 4648471..ee8f775 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -12,8 +12,8 @@ http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png http://img.zcool.cn/community/01fca557a7f5f90000012e7e9feea8.jpg - http://img.zcool.cn/community/01996b57a7f6020000018c1bedef97.jpg - http://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg + + 51巅峰钜惠 diff --git a/banner/banner-banner.iml b/banner/banner-banner.iml index 35bea69..6cce639 100644 --- a/banner/banner-banner.iml +++ b/banner/banner-banner.iml @@ -64,14 +64,6 @@ - - - - - - - - @@ -80,6 +72,14 @@ + + + + + + + + @@ -107,8 +107,8 @@ - + \ No newline at end of file diff --git a/banner/src/main/java/com/youth/banner/Banner.java b/banner/src/main/java/com/youth/banner/Banner.java index 73736a9..955db15 100644 --- a/banner/src/main/java/com/youth/banner/Banner.java +++ b/banner/src/main/java/com/youth/banner/Banner.java @@ -12,7 +12,6 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; -import android.view.animation.AccelerateInterpolator; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ImageView.ScaleType; @@ -21,41 +20,24 @@ import android.widget.TextView; import com.bumptech.glide.Glide; +import com.youth.banner.listener.OnBannerClickListener; +import com.youth.banner.listener.OnLoadImageListener; +import com.youth.banner.view.BannerViewPager; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -import static android.support.v4.view.ViewPager.*; +import static android.support.v4.view.ViewPager.OnPageChangeListener; +import static android.support.v4.view.ViewPager.PageTransformer; public class Banner extends FrameLayout implements OnPageChangeListener { - /** - * 常量已经移入BannerConfig类里 - */ - @Deprecated - public static final int NOT_INDICATOR = 0; - @Deprecated - public static final int CIRCLE_INDICATOR = 1; - @Deprecated - public static final int NUM_INDICATOR = 2; - @Deprecated - public static final int NUM_INDICATOR_TITLE = 3; - @Deprecated - public static final int CIRCLE_INDICATOR_TITLE = 4; - @Deprecated - public static final int LEFT = 5; - @Deprecated - public static final int CENTER = 6; - @Deprecated - public static final int RIGHT = 7; - public String tag = "banner"; private int mIndicatorMargin = BannerConfig.PADDING_SIZE; private int mIndicatorWidth = BannerConfig.INDICATOR_SIZE; private int mIndicatorHeight = BannerConfig.INDICATOR_SIZE; private int bannerStyle = BannerConfig.NOT_INDICATOR; private int delayTime = BannerConfig.TIME; - private int duration = BannerConfig.DURATION; private boolean isAutoPlay = BannerConfig.IS_AUTO_PLAY; private int mIndicatorSelectedResId = R.drawable.gray_radius; private int mIndicatorUnselectedResId = R.drawable.white_radius; @@ -68,20 +50,19 @@ public class Banner extends FrameLayout implements OnPageChangeListener { private int currentItem; private int gravity = -1; private int lastPosition = 1; + private String[] titles; private List imageViews; private List indicatorImages; private Context context; - private ViewPager viewPager; + private BannerViewPager viewPager; + private TextView bannerTitle, numIndicatorInside, numIndicator; private LinearLayout indicator, indicatorInside, titleView; private Handler handler = new Handler(); - private OnBannerClickListener listener; private OnLoadImageListener imageListener; - private String[] titles; - private TextView bannerTitle, numIndicatorInside, numIndicator; private BannerPagerAdapter adapter; private OnPageChangeListener mOnPageChangeListener; private ViewPagerScroller mScroller; - + private OnBannerClickListener listener; public Banner(Context context) { this(context, null); @@ -122,7 +103,7 @@ private void handleTypedArray(Context context, AttributeSet attrs) { private void initView(Context context, AttributeSet attrs) { imageViews.clear(); View view = LayoutInflater.from(context).inflate(R.layout.banner, this, true); - viewPager = (ViewPager) view.findViewById(R.id.viewpager); + viewPager = (BannerViewPager) view.findViewById(R.id.viewpager); titleView = (LinearLayout) view.findViewById(R.id.titleView); indicator = (LinearLayout) view.findViewById(R.id.indicator); indicatorInside = (LinearLayout) view.findViewById(R.id.indicatorInside); @@ -130,21 +111,21 @@ private void initView(Context context, AttributeSet attrs) { numIndicator = (TextView) view.findViewById(R.id.numIndicator); numIndicatorInside = (TextView) view.findViewById(R.id.numIndicatorInside); handleTypedArray(context, attrs); + initViewPagerScroll(); } public void isAutoPlay(boolean isAutoPlay) { this.isAutoPlay = isAutoPlay; - startAutoPlay(); + if (isAutoPlay) + startAutoPlay(); + else + stopAutoPlay(); } public void setDelayTime(int delayTime) { this.delayTime = delayTime; } - public void setScrollerTime(int duration) { - this.duration = duration; - } - public void setIndicatorGravity(int type) { switch (type) { case BannerConfig.LEFT: @@ -342,14 +323,6 @@ private void setData() { if (adapter == null) { adapter = new BannerPagerAdapter(); viewPager.setAdapter(adapter); - try { - Field mField = ViewPager.class.getDeclaredField("mScroller"); - mField.setAccessible(true); - mScroller = new ViewPagerScroller(viewPager.getContext(), new AccelerateInterpolator()); - mField.set(viewPager, mScroller); - } catch (Exception e) { - e.printStackTrace(); - } } else { adapter.notifyDataSetChanged(); } @@ -361,28 +334,43 @@ private void setData() { startAutoPlay(); } + private void initViewPagerScroll() { + try { + Field mField = null; + mField = ViewPager.class.getDeclaredField("mScroller"); + mField.setAccessible(true); + mScroller = new ViewPagerScroller(viewPager.getContext()); + mField.set(viewPager, mScroller); + + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } private void startAutoPlay() { if (isAutoPlay) { - handler.removeCallbacks(task); - handler.postDelayed(task, delayTime); + stopAutoPlay(); } + handler.postDelayed(task, delayTime); + } + public void stopAutoPlay() { + handler.removeCallbacks(task); } - private final Runnable task = new Runnable() { @Override public void run() { - if (isAutoPlay) { - if (count > 1) { - currentItem = currentItem % (count + 1) + 1; - if (currentItem == 1) { - viewPager.setCurrentItem(currentItem, false); - } else { - viewPager.setCurrentItem(currentItem); - } - mScroller.setmDuration(duration); - handler.postDelayed(task, delayTime); + if (count > 1) { + currentItem = currentItem % (count + 1) + 1; + if (currentItem == 1) { + viewPager.setCurrentItem(currentItem, false); + } else { + viewPager.setCurrentItem(currentItem); } + handler.postDelayed(task, delayTime); } } }; @@ -390,18 +378,11 @@ public void run() { @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.i(tag, ev.getAction() + "--" + isAutoPlay); - if (count > 1) { - switch (ev.getAction()) { - case MotionEvent.ACTION_DOWN: - isAutoPlay(false); - Log.i(tag, "--" + isAutoPlay); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - isAutoPlay(true); - Log.i(tag, "--" + isAutoPlay); - break; - } + int action = ev.getAction(); + if (action == MotionEvent.ACTION_UP||action == MotionEvent.ACTION_CANCEL||action == MotionEvent.ACTION_OUTSIDE) { + isAutoPlay(true); + } else if (action == MotionEvent.ACTION_DOWN) { + isAutoPlay(false); } return super.dispatchTouchEvent(ev); } @@ -421,15 +402,15 @@ public boolean isViewFromObject(View arg0, Object arg1) { @Override public Object instantiateItem(ViewGroup container, final int position) { container.addView(imageViews.get(position)); - final ImageView view = imageViews.get(position); - view.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (listener != null) { - listener.OnBannerClick(v, position); + ImageView view = imageViews.get(position); + if (listener!=null) { + view.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + listener.OnBannerClick(position); } - } - }); + }); + } return view; } @@ -442,21 +423,21 @@ public void destroyItem(ViewGroup container, int position, Object object) { @Override public void onPageScrollStateChanged(int state) { + currentItem = viewPager.getCurrentItem(); switch (state) { case 1: - isAutoPlay = false; + isAutoPlay=false; break; case 2: - isAutoPlay = true; + isAutoPlay=true; break; case 0: - if (viewPager.getCurrentItem() == 0) { + if (currentItem == 0) { viewPager.setCurrentItem(count, false); - } else if (viewPager.getCurrentItem() == count + 1) { + } else if (currentItem == count + 1) { viewPager.setCurrentItem(1, false); } - currentItem = viewPager.getCurrentItem(); - isAutoPlay = true; + isAutoPlay=true; break; } if (mOnPageChangeListener != null) { @@ -517,7 +498,7 @@ public void onPageSelected(int position) { public void setOnBannerClickListener(OnBannerClickListener listener) { - this.listener = listener; + this.listener=listener; } public void setOnBannerImageListener(OnLoadImageListener imageListener) { @@ -528,11 +509,4 @@ public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) { mOnPageChangeListener = onPageChangeListener; } - public interface OnBannerClickListener { - void OnBannerClick(View view, int position); - } - - public interface OnLoadImageListener { - void OnLoadImage(ImageView view, Object url); - } } diff --git a/banner/src/main/java/com/youth/banner/ViewPagerScroller.java b/banner/src/main/java/com/youth/banner/ViewPagerScroller.java index cc8ba90..94a11ad 100644 --- a/banner/src/main/java/com/youth/banner/ViewPagerScroller.java +++ b/banner/src/main/java/com/youth/banner/ViewPagerScroller.java @@ -15,6 +15,10 @@ public ViewPagerScroller(Context context, Interpolator interpolator) { super(context, interpolator); } + public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) { + super(context, interpolator, flywheel); + } + @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, mDuration); @@ -25,11 +29,8 @@ public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, mDuration); } - public void setmDuration(int time) { + public void setDuration(int time) { mDuration = time; } - public int getmDuration() { - return mDuration; - } } diff --git a/banner/src/main/java/com/youth/banner/listener/OnBannerClickListener.java b/banner/src/main/java/com/youth/banner/listener/OnBannerClickListener.java new file mode 100644 index 0000000..be73307 --- /dev/null +++ b/banner/src/main/java/com/youth/banner/listener/OnBannerClickListener.java @@ -0,0 +1,7 @@ +package com.youth.banner.listener; + +import android.view.View; + +public interface OnBannerClickListener { + public void OnBannerClick(int position); +} diff --git a/banner/src/main/java/com/youth/banner/listener/OnLoadImageListener.java b/banner/src/main/java/com/youth/banner/listener/OnLoadImageListener.java new file mode 100644 index 0000000..4f5a9af --- /dev/null +++ b/banner/src/main/java/com/youth/banner/listener/OnLoadImageListener.java @@ -0,0 +1,7 @@ +package com.youth.banner.listener; + +import android.widget.ImageView; + +public interface OnLoadImageListener { + public void OnLoadImage(ImageView view, Object url); +} \ No newline at end of file diff --git a/banner/src/main/java/com/youth/banner/view/BannerViewPager.java b/banner/src/main/java/com/youth/banner/view/BannerViewPager.java new file mode 100644 index 0000000..fca605d --- /dev/null +++ b/banner/src/main/java/com/youth/banner/view/BannerViewPager.java @@ -0,0 +1,68 @@ +package com.youth.banner.view; + + +import android.content.Context; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.View; + +public class BannerViewPager extends ViewPager { + + private ViewPager.PageTransformer mPageTransformer; + + public BannerViewPager(Context context) { + this(context, null); + } + + public BannerViewPager(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /** + * android4.1+设置PageTransformer会使ViewPager的子页里面的触摸事件异常 + * (当前看到的子页并非在最上面,所以触摸事件被隐藏在其上面的View给消费了) + * 所以结合setPageTransformer(),在onPageScrolled()里“手动”调用切换页面的动画 + * + * @param position + * @param offset + * @param offsetPixels + */ + @Override + protected void onPageScrolled(int position, float offset, int offsetPixels) { + super.onPageScrolled(position, offset, offsetPixels); + // 下面的源码来自super.onPageScrolled() + int scrollX; + int childCount; + int i; + if (this.mPageTransformer != null) { + scrollX = this.getScrollX(); + childCount = this.getChildCount(); + + for (i = 0; i < childCount; ++i) { + View var15 = this.getChildAt(i); + ViewPager.LayoutParams var16 = (ViewPager.LayoutParams) var15.getLayoutParams(); + if (!var16.isDecor) { + float var17 = (float) (var15.getLeft() - scrollX) / (float) this.getClientWidth(); + this.mPageTransformer.transformPage(var15, var17); + } + } + } + } + + private int getClientWidth() { + return this.getMeasuredWidth() - this.getPaddingLeft() - this.getPaddingRight(); + } + + /** + * 覆盖该方法,不设置PageTransformer,以成员变量的形式保存PageTransformer + * + * @param reverseDrawingOrder + * @param transformer + */ + @Override + public void setPageTransformer(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer) { + super.setPageTransformer(reverseDrawingOrder, null); + mPageTransformer = transformer; + } + +} \ No newline at end of file diff --git a/banner/src/main/res/layout/banner.xml b/banner/src/main/res/layout/banner.xml index a74b185..292f2fd 100644 --- a/banner/src/main/res/layout/banner.xml +++ b/banner/src/main/res/layout/banner.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent"> -