This article in my personal website, my Jane book, my CSDN blog published at the same time, please pay more attention to!

preface

I didn’t have time to blog this week because of work issues. I didn’t have a general impression of the work experience required by many companies, but now I do. A question that has troubled you for a few days, asked a person with many years of experience, can give you a breakthrough point of advice. It is also clear that I need to improve a lot of areas. Well, it’s time to improve yourself step by step!

This week’s article is the last in the ViewPager series – Image rotation effects




Automatic picture rotation

If you’ve missed the last two, you can poke the portal below: D

An example of how to simply play with the guide page of ViewPager (1) with Pointers

ViewPager (2) : ViewPaper+TabLayout+Fragment top TAB interface slide

Talk is cheap,show u the code

  • Custom ViewPager – CarouselViewPager
private int displayTime = 3000; Private CarouselDirection direction = carouseldirection. LEFT; Public CarouselViewPager(Context Context) {super(Context); } public CarouselViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @param time public void setDisplayTime(int time){displayTime = time; } @param direction */ public void setDirection(CarouselDirection) {this.direction = direction; } /** * public void start(){stop(); postDelayed(automaticDisplay,displayTime); } /** * public void stop(){removeCallbacks(automaticDisplay); } /** * public enum CarouselDirection {LEFT,RIGHT} private Runnable automaticDisplay = new Runnable() { @Override public void run() { display(direction); }}; @param direction */ private synchronized void display(CarouselDirection direction) {PagerAdapter pagerAdapter = getAdapter(); if (pagerAdapter ! = null ) { int count = pagerAdapter.getCount(); Int currentItem = getCurrentItem(); Switch (direction) {case LEFT: currentItem++; CurrentItem (currentItem >= count){currentItem = 0; currentItem (currentItem >= count){currentItem = 0; } break; case RIGHT: currentItem--; CurrentItem (currentItem < 0){currentItem = count-1; currentItem (currentItem < 0){currentItem = count-1; } break; } setCurrentItem(currentItem); } start(); } @Override protected void onFinishInflate() { addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { if (state == SCROLL_STATE_IDLE){ start(); } else if (state == SCROLL_STATE_DRAGGING) { stop(); }}}); }Copy the code







    
    

    

Copy the code
  • Customize the CarouselViewPager control in the Fragment
private CarouselViewPager mCarouselView; private List ivList = new ArrayList(); private int[] ivIds = {R.mipmap.pic1, R.mipmap.pic2, R.mipmap.pic3, R.mipmap.pic4}; private ImageView[] indicationPoint; // Private LinearLayout pointLayout; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment2_layout, container, false); initViews(rootView); initData(); return rootView; } private void initViews(View rootView) { mCarouselView = (CarouselViewPager) rootView.findViewById(R.id.mCarouselView);  pointLayout = (LinearLayout) rootView.findViewById(R.id.pointLayout); } private void initData() { for (int i = 0; i < ivIds.length; i++) { ImageView iv = new ImageView(getActivity()); iv.setImageResource(ivIds[i]); ivList.add(iv); } // New ImageView[ivList.size()]; for (int i = 0; i < indicationPoint.length; i++) { ImageView point = new ImageView(getActivity()); LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(10, 10); layout.setMargins(10, 0, 10, 0); point.setLayoutParams(layout); indicationPoint[i] = point; if (i == 0) { indicationPoint[i].setBackgroundResource(R.mipmap.page_indicator_focused); } else { indicationPoint[i].setBackgroundResource(R.mipmap.page_indicator_unfocused); } pointLayout.addView(point); } mCarouselView.setAdapter(new CarouselPagerAdapter(ivList)); mCarouselView.addOnPageChangeListener(this); / / set the pictures show the time mCarouselView. SetDisplayTime (2000); McArouselview.start (); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { setPointColor(position % ivList.size()); } @Override public void onPageScrollStateChanged(int state) { } private void setPointColor(int selectItem) { for (int i = 0; i < indicationPoint.length; i++) { if (i == selectItem) { indicationPoint[i].setBackgroundResource(R.mipmap.page_indicator_focused); } else { indicationPoint[i].setBackgroundResource(R.mipmap.page_indicator_unfocused); }}}Copy the code

Download the source code

ViewPagerDemo

Like can little Star oh!

conclusion

In the code comments are also written more clearly, but in this article you should see that unlike the first article, this time the indicator is created dynamically based on the number of images, which also eliminates the code redundancy caused by the direct use of controls on the layout file. With this three-part series on ViewPager, you can easily learn how to use it.

Use ViewPager to customize Banner rotation

Little brother is not talented, but also hope more advice!