This article shares the specific code of Android custom carousel diagram for your reference. The specific content is as follows
Definition Banner
Mainly use ViewPager to achieve sliding
public class Banner extends FrameLayout { public Context context; private @LayoutRes int layoutId = .banner_view; private View inflate; private ViewPager pager; private AutoHandler mHandler; private PagerAdapter adapter; public IndicatorView indicatorView; public Banner(@NonNull Context context) { this(context, null); } public Banner(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, -1); } public Banner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } public void setAdapter(PagerAdapter adapter) { = adapter; (adapter); (pager); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { = context; inflate = (context).inflate(layoutId, this, true); pager = (.banner_pager); indicatorView = (); mHandler = new AutoHandler(pager); (); } public abstract static class BannerAdapter extends PagerAdapter { private List list; private Context context; public BannerAdapter(List list, Context context) { = list; = context; } @Override public int getCount() { if (null == list || () == 0) { return 1; } else { return (); } } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { if (getLayout() > 0) { View inflate = (context).inflate(getLayout(), container, false); (inflate); setView(inflate, position); return inflate; } else { ImageView imageView = new ImageView(context); (imageView); if (() > 0) { (context).load((position)) .apply(new RequestOptions().centerCrop()) .into(imageView); } else { // (context) // .load(.ic_launcher) // .apply(new RequestOptions().centerCrop()) // .into(imageView); (.ic_launcher); } return imageView; } } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { ((View) object); } protected abstract void setView(View inflate, int position); protected abstract int getLayout(); } }
Define the timer Handler
Mainly handles the scrolling of ViewPager. Turn on the timing task. ViewPager automatically scrolls
public class AutoHandler extends Handler { private ViewPager pager; public static int TIME = 1000 * 2; public boolean stopHandler; public AutoHandler(ViewPager pager) { = pager; } @Override public void handleMessage(@NonNull Message msg) { (msg); switch () { case 100: if (!stopHandler) { sendEmptyMessageDelayed(100, TIME); int position = () + 1; if (position >= ().getCount()) { position = 0; } (position); } else { removeMessages(100); } break; default: removeMessages(100); break; } } public void start() { stopHandler = false; if (!hasMessages(100)) { sendEmptyMessageDelayed(100, TIME); } } public void stop() { stopHandler = true; if (hasMessages(100)) { removeMessages(100); } } }
Draw a subscript indicator
It is mainly drawn according to the needs and is optional. It is associated with ViewPager to achieve linkage
public class IndicatorView extends View { private Context context; private ValueAnimator valueAnimator; private float value; public int indiWidth; public int indiHeight; public int indiDivide; //0 rounded corner 1 right angle public int mode; private int normalColor; private int selectColor; private int curPosition; private int count; private Paint paint; private int width; private int height; private double lastPosition; private ViewPager pager; private PagerAdapter adapter; private DataSetObserver dataSetObserver; public void setPager(final ViewPager pager) { = pager; = (); dataSetObserver = new DataSetObserver() { @Override public void onChanged() { (); setCount(()); setCurPosition(()); } }; if (null != adapter) { setCount(()); setCurPosition(()); (dataSetObserver); } (new () { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { lastPosition = position; } @Override public void onPageSelected(int position) { curPosition = position; setCurPosition(position); } @Override public void onPageScrollStateChanged(int state) { } }); (new () { @Override public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) { if (oldAdapter != newAdapter) { adapter = newAdapter; setCount(()); setCurPosition(()); } } }); } public void setCount(int count) { = count; if (count <= 1) { setVisibility(INVISIBLE); } requestLayout(); postInvalidate(); } public void setCurPosition(int curPos) { /*Record the last record*/ //lastPos = ; = curPos; //postInvalidate(); (); } public IndicatorView(Context context) { this(context, null); } public IndicatorView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, -1); } public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { = context; valueAnimator = (0, 1f); (200); (new () { @Override public void onAnimationUpdate(ValueAnimator animation) { value = (float) (); postInvalidate(); } }); TypedArray typedArray = (attrs, ); indiHeight = (int) (.IndicatorView_indi_height, getResources().getDimension(.dp4)); indiWidth = (int) (.IndicatorView_indi_width, getResources().getDimension(.dp4)); indiDivide = (int) (.IndicatorView_indi_divier, getResources().getDimension(.dp4)); normalColor = (.IndicatorView_indi_color, ("#66dddddd")); selectColor = (.IndicatorView_indi_color_select, ("#eedddddd")); mode = (.IndicatorView_indi_mode, 0); curPosition = 0; count = 0; paint = new Paint(); (true); (normalColor); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { (widthMeasureSpec, heightMeasureSpec); width = indiWidth * count + (count - 1) * indiDivide;//The width of each plus the width of the middle spacing height = indiHeight; setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { (canvas); for (int i = 0; i < count; i++) { int x = i * (indiDivide + indiWidth); if (mode == 0) { (normalColor); if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { (x, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); } if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (curPosition == i) { (selectColor); if (curPosition > lastPosition) { (x, 0, x + value * indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); } else { (x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); } } } if (mode == 1) { (normalColor); (x, 0, x + indiWidth, indiHeight, paint); if (curPosition == i) { (selectColor); if (curPosition > lastPosition) { (x, 0, x + value * indiWidth, indiHeight, paint); } else { (x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, paint); } } } } } } }
Use in Activity
(new (strings, this) { @Override protected void setView(View inflate, int position) { ImageView img = (); ().load((position)) .apply(new RequestOptions().centerCrop()) .into(img); } @Override protected int getLayout() { return ; // return 0; } });
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.