android 图片自动滚动

偏执的太偏执、 2022-05-27 05:38 226阅读 0赞

要实现图片自定轮显,首先要会使用ViewPager控件,还不会使用ViewPager的同学可以https://blog.csdn.net/zenmela2011/article/details/79908728学习。

图片自动轮显就是在viewpager的基础上让它能自动的进行切换,步骤如下:

1.扩展viewpager自定义一个MyScrollImageView类;

2.为MyScrollImageView定义适配器,装载图片;

3.定义图片滑动动画时间控制类;

一、为 自定义ViewPager控件编写适配器

  1. // 适配器
  2. public class MyPagerAdapter extends PagerAdapter {
  3. private Activity mActivity; // 上下文
  4. private List<View> mListViews; // 图片组
  5. public MyPagerAdapter(){
  6. }
  7. public MyPagerAdapter(Activity mActivity,List<View> mListViews){
  8. this.mActivity=mActivity;
  9. this.mListViews=mListViews;
  10. }
  11. public int getCount() {
  12. if (mListViews.size() == 1) {// 一张图片时不用流动
  13. return mListViews.size();
  14. }
  15. return Integer.MAX_VALUE;
  16. }
  17. /**
  18. 返回List中的图片元素装载到控件中
  19. */
  20. public Object instantiateItem(View v, int i) {
  21. if (((ViewPager) v).getChildCount() == mListViews.size()) {
  22. ((ViewPager) v)
  23. .removeView(mListViews.get(i % mListViews.size()));
  24. }
  25. ((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);
  26. return mListViews.get(i % mListViews.size());
  27. }
  28. public boolean isViewFromObject(View arg0, Object arg1) {
  29. return arg0 == (arg1);
  30. }
  31. public void destroyItem(ViewGroup view, int i, Object object) {
  32. view.removeView(mListViews.get(i%mListViews.size()));
  33. }
  34. }

二、自定义一个MyScrollImageView类

自定义一个MyScrollImageView类,主要扩展一个start(…)方法,该方法实现按时间间隔不断切换图片

  1. public class MyImgScroll extends ViewPager {
  2. Activity mActivity; // 上下文
  3. List<View> mListViews; // 图片组
  4. int mScrollTime = 0;
  5. Timer timer;
  6. int oldIndex = 0;
  7. int curIndex = 0;
  8. public MyImgScroll(Context context, AttributeSet attrs) {
  9. super(context, attrs);
  10. }
  11. /**
  12. * 开始广告滚动
  13. *
  14. * @param mainActivity
  15. * 显示广告的主界面
  16. * @param imgList
  17. * 图片列表, 不能为null ,最少一张
  18. * @param scrollTime
  19. * 滚动间隔 ,0为不滚动
  20. * @param ovalLayout
  21. * 圆点容器,可为空,LinearLayout类型
  22. * @param ovalLayoutId
  23. * ovalLayout为空时 写0, 圆点layout XMl
  24. * @param ovalLayoutItemId
  25. * ovalLayout为空时 写0,圆点layout XMl 圆点XMl下View ID
  26. * @param focusedId
  27. * ovalLayout为空时 写0, 圆点layout XMl 选中时的动画
  28. * @param normalId
  29. * ovalLayout为空时 写0, 圆点layout XMl 正常时背景
  30. */
  31. public void start(Activity mainActivity, List<View> imgList,
  32. int scrollTime, LinearLayout ovalLayout, int ovalLayoutId,
  33. int ovalLayoutItemId, int focusedId, int normalId) {
  34. mActivity = mainActivity;
  35. mListViews = imgList;
  36. mScrollTime = scrollTime;
  37. // 设置圆点
  38. setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId,
  39. normalId);
  40. this.setAdapter(new MyPagerAdapter(mActivity,mListViews));// 设置适配器
  41. if (scrollTime != 0 && imgList.size() > 1) {
  42. // 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
  43. new FixedSpeedScroller(mActivity).setDuration(this, 700);
  44. startTimer();
  45. // 触摸时停止滚动
  46. this.setOnTouchListener(new OnTouchListener() {
  47. public boolean onTouch(View v, MotionEvent event) {
  48. if (event.getAction() == MotionEvent.ACTION_UP) {
  49. startTimer();
  50. } else {
  51. stopTimer();
  52. }
  53. return false;
  54. }
  55. });
  56. }
  57. if (mListViews.size() > 1) {
  58. this.setCurrentItem((Integer.MAX_VALUE / 2)
  59. - (Integer.MAX_VALUE / 2) % mListViews.size());// 设置选中为中间/图片为和第0张一样
  60. }
  61. }
  62. // 设置圆点
  63. private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,
  64. final int ovalLayoutItemId, final int focusedId, final int normalId) {
  65. if (ovalLayout != null) {
  66. LayoutInflater inflater=LayoutInflater.from(mActivity);
  67. for (int i = 0; i < mListViews.size(); i++) {
  68. ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
  69. }
  70. //选中第一个
  71. ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId)
  72. .setBackgroundResource(focusedId);
  73. this.setOnPageChangeListener(new OnPageChangeListener() {
  74. public void onPageSelected(int i) {
  75. curIndex = i % mListViews.size();
  76. //取消圆点选中
  77. ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId)
  78. .setBackgroundResource(normalId);
  79. //圆点选中
  80. ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId)
  81. .setBackgroundResource(focusedId);
  82. oldIndex = curIndex;
  83. }
  84. public void onPageScrolled(int arg0, float arg1, int arg2) {
  85. }
  86. public void onPageScrollStateChanged(int arg0) {
  87. }
  88. });
  89. }
  90. }
  91. /**
  92. * 取得当明选中下标
  93. * @return
  94. */
  95. public int getCurIndex() {
  96. return curIndex;
  97. }
  98. /**
  99. * 停止滚动
  100. */
  101. public void stopTimer() {
  102. if (timer != null) {
  103. timer.cancel();
  104. timer = null;
  105. }
  106. }
  107. /**
  108. * 开始滚动
  109. */
  110. public void startTimer() {
  111. timer = new Timer();
  112. timer.schedule(new TimerTask() {
  113. public void run() {
  114. mActivity.runOnUiThread(new Runnable() {
  115. public void run() {
  116. MyImgScroll.this.setCurrentItem(MyImgScroll.this
  117. .getCurrentItem() + 1);//设置控件当前项(改变图片)
  118. }
  119. });
  120. }
  121. }, mScrollTime, mScrollTime);
  122. }
  123. }

三、定义图片滑动动画时间控制类

  1. package com.tianshicoffeeom.app.imgscroll;
  2. import java.lang.reflect.Field;
  3. import android.content.Context;
  4. import android.support.v4.view.ViewPager;
  5. import android.view.animation.Interpolator;
  6. import android.widget.Scroller;
  7. /**
  8. * 图片滑动动画时间控制类 , 如果采用默认时间可不用这个类 ,通过反射技术改变ViewPager的滑动时间
  9. *
  10. */
  11. public class FixedSpeedScroller extends Scroller {
  12. private Context context;
  13. private int mDuration = 500;
  14. public FixedSpeedScroller(Context context) {
  15. super(context);
  16. this.context=context;
  17. }
  18. public FixedSpeedScroller(Context context, Interpolator interpolator) {
  19. super(context, interpolator);
  20. this.context=context;
  21. }
  22. /**
  23. * 设置改变ViewPager的滑动时间
  24. * @param vp ViewPager 对象
  25. * @param time 时间
  26. */
  27. public void setDuration(ViewPager vp,int time) {
  28. try {
  29. Field field = ViewPager.class.getDeclaredField("mScroller");
  30. field.setAccessible(true);
  31. this.setmDuration(time);//设置翻动时间
  32. field.set(vp, this);
  33. } catch (Exception e) {
  34. }
  35. }
  36. @Override
  37. public void startScroll(int startX, int startY, int dx, int dy, int duration) {
  38. //System.out.println("startScroll1");
  39. super.startScroll(startX, startY, dx, dy, mDuration);
  40. }
  41. @Override
  42. public void startScroll(int startX, int startY, int dx, int dy) {
  43. //System.out.println("startScroll2");
  44. super.startScroll(startX, startY, dx, dy, mDuration);
  45. }
  46. public void setmDuration(int time) {
  47. mDuration = time;
  48. }
  49. public int getmDuration() {
  50. return mDuration;
  51. }
  52. }

四、在MainActivity中配置

  1. public class MainActivity extends Activity {
  2. private MyImgScroll myPager; // 图片容器
  3. private LinearLayout ovalLayout; // 圆点容器
  4. private List<View> listViews; // 图片组
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. myPager = (MyImgScroll) findViewById(R.id.myvp);
  10. ovalLayout = (LinearLayout) findViewById(R.id.vb);
  11. InitViewPager();//初始化图片
  12. //开始滚动
  13. myPager.start(this, listViews, 4000, ovalLayout,
  14. R.layout.ad_bottom_item, R.id.ad_item_v,
  15. R.drawable.dot_focused, R.drawable.dot_normal);
  16. }
  17. @Override
  18. protected void onRestart() {
  19. myPager.startTimer();
  20. super.onRestart();
  21. }
  22. @Override
  23. protected void onStop() {
  24. myPager.stopTimer();
  25. super.onStop();
  26. }
  27. /**
  28. * 初始化图片
  29. */
  30. private void InitViewPager() {
  31. listViews = new ArrayList<View>();
  32. int[] imageResId = new int[] { R.drawable.banner1, R.drawable. banner2,
  33. R.drawable. banner3, R.drawable.d, R.drawable. banner4 };
  34. for (int i = 0; i < imageResId.length; i++) {
  35. ImageView imageView = new ImageView(this);
  36. imageView.setOnClickListener(new OnClickListener() {
  37. public void onClick(View v) {// 设置图片点击事件
  38. Toast.makeText(MainActivity.this,
  39. "点击了:" + myPager.getCurIndex(), Toast.LENGTH_SHORT)
  40. .show();
  41. }
  42. });
  43. imageView.setImageResource(imageResId[i]);
  44. imageView.setScaleType(ScaleType.CENTER_CROP);
  45. listViews.add(imageView);
  46. }
  47. }
  48. }

代码参考 点击打开链接

发表评论

表情:
评论列表 (有 0 条评论,226人围观)

还没有评论,来说两句吧...

相关阅读

    相关 垂直滚动图片

    这个效果不美观,只要了解的是函数如何进行调用的。函数循环还可以调用animate,控制动画的时间就可以实现隔一段时间调用一次函数,而不一定一直使用setTimeout()函数。