经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android自定义轮播图效果
来源:jb51  时间:2022/8/15 18:59:23  对本文有异议

本文实例为大家分享了Android自定义轮播图的具体代码,供大家参考,具体内容如下

定义Banner

主要使用ViewPager实现滑动

  1. public class Banner extends FrameLayout {
  2. ? ? public Context context;
  3. ? ? private @LayoutRes
  4. ? ? int layoutId = R.layout.banner_view;
  5. ? ? private View inflate;
  6. ? ? private ViewPager pager;
  7. ? ? private AutoHandler mHandler;
  8. ? ? private PagerAdapter adapter;
  9. ? ? public IndicatorView indicatorView;
  10.  
  11. ? ? public Banner(@NonNull Context context) {
  12. ? ? ? ? this(context, null);
  13. ? ? }
  14.  
  15. ? ? public Banner(@NonNull Context context, @Nullable AttributeSet attrs) {
  16. ? ? ? ? this(context, attrs, -1);
  17. ? ? }
  18.  
  19. ? ? public Banner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  20. ? ? ? ? super(context, attrs, defStyleAttr);
  21. ? ? ? ? init(context, attrs, defStyleAttr);
  22. ? ? }
  23.  
  24. ? ? public void setAdapter(PagerAdapter adapter) {
  25. ? ? ? ? this.adapter = adapter;
  26. ? ? ? ? pager.setAdapter(adapter);
  27. ? ? ? ? indicatorView.setPager(pager);
  28. ? ? }
  29.  
  30. ? ? private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  31. ? ? ? ? this.context = context;
  32. ? ? ? ? inflate = LayoutInflater.from(context).inflate(layoutId, this, true);
  33. ? ? ? ? pager = inflate.findViewById(R.id.banner_pager);
  34. ? ? ? ? indicatorView = inflate.findViewById(R.id.indicatorView);
  35.  
  36. ? ? ? ? mHandler = new AutoHandler(pager);
  37.  
  38. ? ? ? ? mHandler.start();
  39.  
  40. ? ? }
  41.  
  42. ? ? public abstract static class BannerAdapter extends PagerAdapter {
  43. ? ? ? ? private List list;
  44. ? ? ? ? private Context context;
  45.  
  46. ? ? ? ? public BannerAdapter(List list, Context context) {
  47. ? ? ? ? ? ? this.list = list;
  48. ? ? ? ? ? ? this.context = context;
  49. ? ? ? ? }
  50.  
  51. ? ? ? ? @Override
  52. ? ? ? ? public int getCount() {
  53. ? ? ? ? ? ? if (null == list || list.size() == 0) {
  54. ? ? ? ? ? ? ? ? return 1;
  55. ? ? ? ? ? ? } else {
  56. ? ? ? ? ? ? ? ? return list.size();
  57. ? ? ? ? ? ? }
  58. ? ? ? ? }
  59.  
  60. ? ? ? ? @Override
  61. ? ? ? ? public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
  62. ? ? ? ? ? ? return view == object;
  63. ? ? ? ? }
  64.  
  65. ? ? ? ? @NonNull
  66. ? ? ? ? @Override
  67. ? ? ? ? public Object instantiateItem(@NonNull ViewGroup container, int position) {
  68. ? ? ? ? ? ? if (getLayout() > 0) {
  69. ? ? ? ? ? ? ? ? View inflate = LayoutInflater.from(context).inflate(getLayout(), container, false);
  70. ? ? ? ? ? ? ? ? container.addView(inflate);
  71. ? ? ? ? ? ? ? ? setView(inflate, position);
  72. ? ? ? ? ? ? ? ? return inflate;
  73. ? ? ? ? ? ? } else {
  74. ? ? ? ? ? ? ? ? ImageView imageView = new ImageView(context);
  75. ? ? ? ? ? ? ? ? container.addView(imageView);
  76. ? ? ? ? ? ? ? ? if (list.size() > 0) {
  77. ? ? ? ? ? ? ? ? ? ? Glide.with(context).load(list.get(position))
  78. ? ? ? ? ? ? ? ? ? ? ? ? ? ? .apply(new RequestOptions().centerCrop())
  79. ? ? ? ? ? ? ? ? ? ? ? ? ? ? .into(imageView);
  80. ? ? ? ? ? ? ? ? } else {
  81. // ? ? ? ? ? ? ? ? ? ?Glide.with(context)
  82. // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.load(R.mipmap.ic_launcher)
  83. // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.apply(new RequestOptions().centerCrop())
  84. // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.into(imageView);
  85. ? ? ? ? ? ? ? ? ? ? imageView.setBackgroundResource(R.mipmap.ic_launcher);
  86. ? ? ? ? ? ? ? ? }
  87. ? ? ? ? ? ? ? ? return imageView;
  88. ? ? ? ? ? ? }
  89.  
  90. ? ? ? ? }
  91.  
  92. ? ? ? ? @Override
  93. ? ? ? ? public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
  94. ? ? ? ? ? ? container.removeView((View) object);
  95. ? ? ? ? }
  96.  
  97. ? ? ? ? protected abstract void setView(View inflate, int position);
  98.  
  99. ? ? ? ? protected abstract int getLayout();
  100.  
  101. ? ? }
  102.  
  103.  
  104. }

定义定时器Handler

主要处理ViewPager的滚动 开启定时任务 ViewPager自动滚动

  1. public class AutoHandler extends Handler {
  2. ? ? private ViewPager pager;
  3. ? ? public static int TIME = 1000 * 2;
  4. ? ? public boolean stopHandler;
  5.  
  6. ? ? public AutoHandler(ViewPager pager) {
  7. ? ? ? ? this.pager = pager;
  8. ? ? }
  9.  
  10. ? ? @Override
  11. ? ? public void handleMessage(@NonNull Message msg) {
  12. ? ? ? ? super.handleMessage(msg);
  13. ? ? ? ? switch (msg.what) {
  14. ? ? ? ? ? ? case 100:
  15. ? ? ? ? ? ? ? ? if (!stopHandler) {
  16.  
  17. ? ? ? ? ? ? ? ? ? ? sendEmptyMessageDelayed(100, TIME);
  18.  
  19. ? ? ? ? ? ? ? ? ? ? int position = pager.getCurrentItem() + 1;
  20.  
  21. ? ? ? ? ? ? ? ? ? ? if (position >= pager.getAdapter().getCount()) {
  22. ? ? ? ? ? ? ? ? ? ? ? ? position = 0;
  23. ? ? ? ? ? ? ? ? ? ? }
  24. ? ? ? ? ? ? ? ? ? ? pager.setCurrentItem(position);
  25.  
  26. ? ? ? ? ? ? ? ? } else {
  27. ? ? ? ? ? ? ? ? ? ? removeMessages(100);
  28. ? ? ? ? ? ? ? ? }
  29. ? ? ? ? ? ? ? ? break;
  30. ? ? ? ? ? ? default:
  31. ? ? ? ? ? ? ? ? removeMessages(100);
  32. ? ? ? ? ? ? ? ? break;
  33. ? ? ? ? }
  34. ? ? }
  35.  
  36. ? ? public void start() {
  37. ? ? ? ? stopHandler = false;
  38. ? ? ? ? if (!hasMessages(100)) {
  39. ? ? ? ? ? ? sendEmptyMessageDelayed(100, TIME);
  40. ? ? ? ? }
  41. ? ? }
  42.  
  43. ? ? public void stop() {
  44. ? ? ? ? stopHandler = true;
  45. ? ? ? ? if (hasMessages(100)) {
  46. ? ? ? ? ? ? removeMessages(100);
  47. ? ? ? ? }
  48. ? ? }
  49.  
  50. }

绘制一个下标指示器

主要根据需求自行绘制 可有可无 和ViewPager关联在一起 实现联动

  1. public class IndicatorView extends View {
  2. ? ? private Context context;
  3. ? ? private ValueAnimator valueAnimator;
  4. ? ? private float value;
  5. ? ? public int indiWidth;
  6. ? ? public int indiHeight;
  7. ? ? public int indiDivide;
  8. ? ? //0圆角 ? 1直角
  9. ? ? public int mode;
  10. ? ? private int normalColor;
  11. ? ? private int selectColor;
  12. ? ? private int curPosition;
  13. ? ? private int count;
  14. ? ? private Paint paint;
  15. ? ? private int width;
  16. ? ? private int height;
  17. ? ? private double lastPosition;
  18. ? ? private ViewPager pager;
  19. ? ? private PagerAdapter adapter;
  20. ? ? private DataSetObserver dataSetObserver;
  21.  
  22. ? ? public void setPager(final ViewPager pager) {
  23. ? ? ? ? this.pager = pager;
  24. ? ? ? ? this.adapter = pager.getAdapter();
  25. ? ? ? ? dataSetObserver = new DataSetObserver() {
  26. ? ? ? ? ? ? @Override
  27. ? ? ? ? ? ? public void onChanged() {
  28. ? ? ? ? ? ? ? ? super.onChanged();
  29. ? ? ? ? ? ? ? ? setCount(adapter.getCount());
  30. ? ? ? ? ? ? ? ? setCurPosition(pager.getCurrentItem());
  31. ? ? ? ? ? ? }
  32. ? ? ? ? };
  33. ? ? ? ? if (null != adapter) {
  34. ? ? ? ? ? ? setCount(adapter.getCount());
  35. ? ? ? ? ? ? setCurPosition(pager.getCurrentItem());
  36. ? ? ? ? ? ? adapter.registerDataSetObserver(dataSetObserver);
  37. ? ? ? ? }
  38.  
  39. ? ? ? ? pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  40. ? ? ? ? ? ? @Override
  41. ? ? ? ? ? ? public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  42. ? ? ? ? ? ? ? ? lastPosition = position;
  43. ? ? ? ? ? ? }
  44.  
  45. ? ? ? ? ? ? @Override
  46. ? ? ? ? ? ? public void onPageSelected(int position) {
  47. ? ? ? ? ? ? ? ? curPosition = position;
  48. ? ? ? ? ? ? ? ? setCurPosition(position);
  49. ? ? ? ? ? ? }
  50.  
  51. ? ? ? ? ? ? @Override
  52. ? ? ? ? ? ? public void onPageScrollStateChanged(int state) {
  53.  
  54. ? ? ? ? ? ? }
  55. ? ? ? ? });
  56.  
  57. ? ? ? ? pager.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() {
  58. ? ? ? ? ? ? @Override
  59. ? ? ? ? ? ? public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
  60. ? ? ? ? ? ? ? ? if (oldAdapter != newAdapter) {
  61. ? ? ? ? ? ? ? ? ? ? adapter = newAdapter;
  62. ? ? ? ? ? ? ? ? ? ? setCount(adapter.getCount());
  63. ? ? ? ? ? ? ? ? ? ? setCurPosition(viewPager.getCurrentItem());
  64. ? ? ? ? ? ? ? ? }
  65. ? ? ? ? ? ? }
  66. ? ? ? ? });
  67. ? ? }
  68.  
  69. ? ? public void setCount(int count) {
  70. ? ? ? ? this.count = count;
  71. ? ? ? ? if (count <= 1) {
  72. ? ? ? ? ? ? setVisibility(INVISIBLE);
  73. ? ? ? ? }
  74. ? ? ? ? requestLayout();
  75. ? ? ? ? postInvalidate();
  76. ? ? }
  77.  
  78. ? ? public void setCurPosition(int curPos) {
  79. ? ? ? ? /*记录上次记录*/
  80. ? ? ? ? //lastPos = this.curPos;
  81. ? ? ? ? this.curPosition = curPos;
  82. ? ? ? ? //postInvalidate();
  83. ? ? ? ? valueAnimator.start();
  84. ? ? }
  85.  
  86.  
  87. ? ? public IndicatorView(Context context) {
  88. ? ? ? ? this(context, null);
  89. ? ? }
  90.  
  91. ? ? public IndicatorView(Context context, @Nullable AttributeSet attrs) {
  92. ? ? ? ? this(context, attrs, -1);
  93. ? ? }
  94.  
  95. ? ? public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  96. ? ? ? ? super(context, attrs, defStyleAttr);
  97. ? ? ? ? init(context, attrs, defStyleAttr);
  98. ? ? }
  99.  
  100. ? ? private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  101. ? ? ? ? this.context = context;
  102. ? ? ? ? valueAnimator = ValueAnimator.ofFloat(0, 1f);
  103. ? ? ? ? valueAnimator.setDuration(200);
  104. ? ? ? ? valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  105. ? ? ? ? ? ? @Override
  106. ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator animation) {
  107. ? ? ? ? ? ? ? ? value = (float) animation.getAnimatedValue();
  108. ? ? ? ? ? ? ? ? postInvalidate();
  109. ? ? ? ? ? ? }
  110. ? ? ? ? });
  111.  
  112. ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndicatorView);
  113.  
  114. ? ? ? ? indiHeight = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_height, getResources().getDimension(R.dimen.dp4));
  115. ? ? ? ? indiWidth = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_width, getResources().getDimension(R.dimen.dp4));
  116.  
  117. ? ? ? ? indiDivide = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_divier, getResources().getDimension(R.dimen.dp4));
  118.  
  119. ? ? ? ? normalColor = typedArray.getColor(R.styleable.IndicatorView_indi_color, Color.parseColor("#66dddddd"));
  120. ? ? ? ? selectColor = typedArray.getColor(R.styleable.IndicatorView_indi_color_select, Color.parseColor("#eedddddd"));
  121.  
  122. ? ? ? ? mode = typedArray.getInteger(R.styleable.IndicatorView_indi_mode, 0);
  123.  
  124. ? ? ? ? curPosition = 0;
  125. ? ? ? ? count = 0;
  126.  
  127. ? ? ? ? paint = new Paint();
  128. ? ? ? ? paint.setAntiAlias(true);
  129.  
  130. ? ? ? ? paint.setColor(normalColor);
  131.  
  132. ? ? }
  133.  
  134. ? ? @Override
  135. ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  136. ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  137.  
  138. ? ? ? ? width = indiWidth * count + (count - 1) * indiDivide;//每个的宽度加上中间间距的宽度
  139. ? ? ? ? height = indiHeight;
  140.  
  141. ? ? ? ? setMeasuredDimension(width, height);
  142. ? ? }
  143.  
  144. ? ? @Override
  145. ? ? protected void onDraw(Canvas canvas) {
  146. ? ? ? ? super.onDraw(canvas);
  147.  
  148. ? ? ? ? for (int i = 0; i < count; i++) {
  149. ? ? ? ? ? ? int x = i * (indiDivide + indiWidth);
  150.  
  151. ? ? ? ? ? ? if (mode == 0) {
  152. ? ? ? ? ? ? ? ? paint.setColor(normalColor);
  153. ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  154. ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint);
  155. ? ? ? ? ? ? ? ? }
  156.  
  157. ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  158. ? ? ? ? ? ? ? ? ? ? if (curPosition == i) {
  159. ? ? ? ? ? ? ? ? ? ? ? ? paint.setColor(selectColor);
  160. ? ? ? ? ? ? ? ? ? ? ? ? if (curPosition > lastPosition) {
  161. ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x, 0, x + value * indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint);
  162. ? ? ? ? ? ? ? ? ? ? ? ? } else {
  163. ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint);
  164. ? ? ? ? ? ? ? ? ? ? ? ? }
  165. ? ? ? ? ? ? ? ? ? ? }
  166.  
  167. ? ? ? ? ? ? ? ? }
  168.  
  169. ? ? ? ? ? ? ? ? if (mode == 1) {
  170. ? ? ? ? ? ? ? ? ? ? paint.setColor(normalColor);
  171. ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x, 0, x + indiWidth, indiHeight, paint);
  172.  
  173.  
  174. ? ? ? ? ? ? ? ? ? ? if (curPosition == i) {
  175. ? ? ? ? ? ? ? ? ? ? ? ? paint.setColor(selectColor);
  176.  
  177. ? ? ? ? ? ? ? ? ? ? ? ? if (curPosition > lastPosition) {
  178. ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x, 0, x + value * indiWidth, indiHeight, paint);
  179. ? ? ? ? ? ? ? ? ? ? ? ? } else {
  180. ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, paint);
  181. ? ? ? ? ? ? ? ? ? ? ? ? }
  182. ? ? ? ? ? ? ? ? ? ? }
  183.  
  184. ? ? ? ? ? ? ? ? }
  185. ? ? ? ? ? ? }
  186. ? ? ? ? }
  187. ? ? }
  188. }

在Activity中使用

  1. banner.setAdapter(new Banner.BannerAdapter(strings, this) {
  2. ? ? ? ? ? ? @Override
  3. ? ? ? ? ? ? protected void setView(View inflate, int position) {
  4. ? ? ? ? ? ? ? ? ImageView img = inflate.findViewById(R.id.img);
  5. ? ? ? ? ? ? ? ? Glide.with(MainActivity.this).load(strings.get(position))
  6. ? ? ? ? ? ? ? ? ? ? ? ? .apply(new RequestOptions().centerCrop())
  7. ? ? ? ? ? ? ? ? ? ? ? ? .into(img);
  8. ? ? ? ? ? ? }
  9.  
  10. ? ? ? ? ? ? @Override
  11. ? ? ? ? ? ? protected int getLayout() {
  12. ? ? ? ? ? ? ? ? return R.layout.img;
  13. // ? ? ? ? ? ? ? ?return 0;
  14. ? ? ? ? ? ? }
  15. });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号