Android最完善的自定义Banner轮播图之一,带给你最全面的体验(二...

Android最完善的自定义Banner轮播图之一,带给你最全面的体验(二...

2023年7月7日发(作者:)

Android最完善的⾃定义Banner轮播图之⼀,带给你最全⾯的体验(⼆)

⼀.⾃定义Banner⽆限轮播图式样本篇博客的源码:1.效果图:(⼀屏三页⾃定义,⽤RecyclerView实现⽆限轮播图,有普通版和3d版,包括上下滑动效果)图⼀:图⼆: 图三:

⽀持⾃定义圆形、圆⾓矩形、椭圆形等指⽰器(⽀持所有shape标签的形状显⽰)⽀持⽆限轮播⽀持轮播图点击事件⽀持快速活动⽀持上下滑动⽀持当前图⽚缩放⽐列等等属性属性名称

app:showIndicatorapp:intervalapp:isAutoPlayingapp:orientationapp:itemSpaceapp:centerScaleapp:moveSpeed⽅法setShowIndicator(boolean showIndicator)setAutoPlayDuration(int autoPlayDuration)setAutoPlaying(boolean isAutoPlaying)setOrientation(int orientation)setItemSpace(int itemSpace)setCenterScale(float centerScale)setMoveSpeed(float moveSpeed)意义是否显⽰指⽰器轮播时间间隔是否开启⾃动轮播轮播图⽅向图⽚间距当前图⽚缩放⽐列滚动速度,越⼤越快类型booleanintbooleanenumintfloatfloat默认值true4000truehorizontal201.21.02.主要函数代码:(JAVA)import ;import ;import patActivity;import ;import ;import Layout;import nerAdapter;import ist;import ;public class MainActivity extends AppCompatActivity implements erItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); BannerLayout recyclerBanner = findViewById(er); BannerLayout bannerVertical = findViewById(er_ver); List list = new ArrayList<>(); ("/it/u=1352823040,1166166164&fm=27&gp="); ("/it/u=2293177440,3125900197&fm=27&gp="); ("/it/u=3967183915,4078698000&fm=27&gp="); ("/it/u=3184221534,2238244948&fm=27&gp="); ("/it/u=1794621527,1964098559&fm=27&gp="); ("/it/u=1243617734,335916716&fm=27&gp="); WebBannerAdapter webBannerAdapter=new WebBannerAdapter(this,list); annerItemClickListener(new erItemClickListener() { @Override public void onItemClick(int position) { xt(, "点击了第 " + position+" 项", _SHORT).show(); } }); WebBannerAdapter WebBannerAdapter2 =new WebBannerAdapter(this,list); annerItemClickListener(new erItemClickListener() { @Override public void onItemClick(int position) { xt(, "点击了第 " + position+" 项", _SHORT).show(); } }); pter(webBannerAdapter); pter(WebBannerAdapter2); } public void jump(View view) { startActivity(new Intent(, )); } public void jumpOverFlying(View view) { startActivity(new Intent(, )); } @Override public void onItemClick(int position) { }}主要函数代码:(Kotlin) val webBannerAdapter = WebBannerAdapter(mActivity,it); annerItemClickListener(object : erItemClickListener { override fun onItemClick(position: Int) { xt(mActivity ,"点击了第 " + position+" 项", _SHORT).show(); } }); binding!!.pter(webBannerAdapter)3.⾃定义Banner(图⼀的两个效果)import t;import ces;import rray;import ;import le;import ntDrawable;import rawable;import r;import e;import yCompat;import LayoutManager;import ationHelper;import erView;import uteSet;import ;import alue;import y;import Event;import oup;import ayout;import iew;import y.R;import SnapHelper;import LayoutManager;import static ;import static _STATE_IDLE;public class BannerLayout extends FrameLayout { private int autoPlayDuration;//刷新间隔时间 private boolean showIndicator;//是否显⽰指⽰器 private RecyclerView indicatorContainer; private Drawable mSelectedDrawable; private Drawable mUnselectedDrawable; private IndicatorAdapter indicatorAdapter; private int indicatorMargin;//指⽰器间距 private RecyclerView mRecyclerView; private BannerLayoutManager mLayoutManager; private int WHAT_AUTO_PLAY = 1000; private boolean hasInit; private int bannerSize = 1; private int currentIndex; private boolean isPlaying = false; private boolean isAutoPlaying = true; private boolean isAutoPlaying = true; int itemSpace; float centerScale; float moveSpeed; protected Handler mHandler = new Handler(new ck() { @Override public boolean handleMessage(Message msg) { if ( == WHAT_AUTO_PLAY) { if (currentIndex == rentPosition()) { ++currentIndex; ScrollToPosition(currentIndex); ptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration); refreshIndicator(); } } return false; } }); public BannerLayout(Context context) { this(context, null); } public BannerLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BannerLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); } protected void initView(Context context, AttributeSet attrs) { TypedArray a = StyledAttributes(attrs, Layout); showIndicator = lean(Layout_showIndicator, true); autoPlayDuration = (Layout_interval, 4000); isAutoPlaying = lean(Layout_autoPlaying, true); itemSpace = (Layout_itemSpace, 20); centerScale = at(Layout_centerScale, 1.2f); moveSpeed = at(Layout_moveSpeed, 1.0f); if (mSelectedDrawable == null) { //绘制默认选中状态图形-- ⾃定义椭圆形指⽰器,圆⾓矩形 GradientDrawable drawable = (GradientDrawable) ources().getDrawable(52);// drawable .setColor(olor("#ff4400")); mSelectedDrawable = new LayerDrawable(new Drawable[]{drawable}); //绘制默认选中状态图形 -- 圆形// GradientDrawable selectedGradientDrawable = new GradientDrawable();// pe();// or();// e(dp2px(5), dp2px(5));// nerRadius(dp2px(5) / 2);// mSelectedDrawable = new LayerDrawable(new Drawable[]{selectedGradientDrawable}); } if (mUnselectedDrawable == null) { //绘制默认未选中状态图形 -- ⾃定义椭圆形指⽰器,圆⾓矩形 GradientDrawable drawable = (GradientDrawable) ources().getDrawable(53);// drawable .setColor(olor("#ff4400")); mUnselectedDrawable = new LayerDrawable(new Drawable[]{drawable}); //绘制默认未选中状态图形 -- 圆形// GradientDrawable unSelectedGradientDrawable = new GradientDrawable();// pe();// or();// e(dp2px(5), dp2px(5));// nerRadius(dp2px(5) / 2);// mUnselectedDrawable = new LayerDrawable(new Drawable[]{unSelectedGradientDrawable}); } indicatorMargin = dp2px(4); int marginLeft = dp2px(16); int marginRight = dp2px(0); int marginBottom = dp2px(11); int gravity = ; int o = (Layout_orientation, 0); int orientation = 0; if (o == 0) { orientation = NTAL; } else if (o == 1) { orientation = AL; } e(); //轮播图部分 mRecyclerView = new RecyclerView(context); LayoutParams vpLayoutParams = new LayoutParams(_PARENT, _PARENT); addView(mRecyclerView, vpLayoutParams); mLayoutManager = new BannerLayoutManager(getContext(), orientation); mSpace(itemSpace); terScale(centerScale); eSpeed(moveSpeed); outManager(mLayoutManager); new CenterSnapHelper().attachToRecyclerView(mRecyclerView); //指⽰器部分 indicatorContainer = new RecyclerView(context); LinearLayoutManager indicatorLayoutManager = new LinearLayoutManager(context, orientation, false); outManager(indicatorLayoutManager); indicatorAdapter = new IndicatorAdapter(); pter(indicatorAdapter); LayoutParams params = new LayoutParams(_CONTENT, _CONTENT);// y = | gravity; y = | CENTER;;// gins(marginLeft, 0, marginRight, marginBottom); gins(0, 0, 0, 0); addView(indicatorContainer, params); if (!showIndicator) { ibility(GONE); } } // 设置是否禁⽌滚动播放 public void setAutoPlaying(boolean isAutoPlaying) { Playing = isAutoPlaying; setPlaying(Playing); } public boolean isPlaying() { return isPlaying; } //设置是否显⽰指⽰器 public void setShowIndicator(boolean showIndicator) { dicator = showIndicator; ibility(showIndicator ? VISIBLE : GONE); } //设置当前图⽚缩放系数 public void setCenterScale(float centerScale) { Scale = centerScale; terScale(centerScale); } //设置跟随⼿指的移动速度 public void setMoveSpeed(float moveSpeed) { eed = moveSpeed; eSpeed(moveSpeed); } //设置图⽚间距 public void setItemSpace(int itemSpace) { ace = itemSpace; mSpace(itemSpace); } /** * 设置轮播间隔时间 * * @param autoPlayDuration 时间毫秒 */ public void setAutoPlayDuration(int autoPlayDuration) { ayDuration = autoPlayDuration; } public void setOrientation(int orientation) { entation(orientation); } /** * 设置是否⾃动播放(上锁) * * @param playing 开始播放 */ protected synchronized void setPlaying(boolean playing) { if (isAutoPlaying && hasInit) { if (!isPlaying && playing) { ptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration); isPlaying = true; } else if (isPlaying && !playing) { Messages(WHAT_AUTO_PLAY); isPlaying = false; } } } /** * 设置轮播数据集 */ public void setAdapter(r adapter) { hasInit = false; pter(adapter); bannerSize = mCount(); inite(bannerSize >= 3); setPlaying(true); crollListener(new llListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (dx != 0) { setPlaying(false); } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { int first = rentPosition(); Log.d("xxx", "onScrollStateChanged"); if (currentIndex != first) { currentIndex = first; } if (newState == SCROLL_STATE_IDLE) { setPlaying(true); } refreshIndicator(); } }); hasInit = true; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ion()) { case _DOWN: setPlaying(false); break; case _UP: case _CANCEL: setPlaying(true); break; } return chTouchEvent(ev); } @Override protected void onAttachedToWindow() { chedToWindow(); setPlaying(true); } @Override protected void onDetachedFromWindow() { chedFromWindow(); setPlaying(false); } @Override protected void onWindowVisibilityChanged(int visibility) { owVisibilityChanged(visibility); if (visibility == VISIBLE) { setPlaying(true); } else { setPlaying(false); } } /** * 标⽰点适配器 */ protected class IndicatorAdapter extends r { int currentPosition = 0; public void setPosition(int currentPosition) { tPosition = currentPosition; } @Override public lder onCreateViewHolder(ViewGroup parent, int viewType) { ImageView bannerPoint = new ImageView(getContext()); Params lp = new Params(_CONTENT, _CONTENT); gins(indicatorMargin, indicatorMargin, indicatorMargin, indicatorMargin); outParams(lp); return new lder(bannerPoint) { }; } @Override public void onBindViewHolder(lder holder, int position) { ImageView bannerPoint = (ImageView) ew; geDrawable(currentPosition == position ? mSelectedDrawable : mUnselectedDrawable); } @Override public int getItemCount() { return bannerSize; } } protected int dp2px(int dp) { return (int) imension(X_UNIT_DIP, dp, tem().getDisplayMetrics()); } /** * 改变导航的指⽰点 */ protected synchronized void refreshIndicator() { if (showIndicator && bannerSize > 1) { ition(currentIndex % bannerSize); DataSetChanged(); } } public interface OnBannerItemClickListener { void onItemClick(int position); }}图⼆的第⼆个效果:import t;import ces;import rray;import le;import ntDrawable;import rawable;import r;import e;import yCompat;import LayoutManager;import ationHelper;import erView;import uteSet;import ;import alue;import y;import y;import Event;import oup;import ayout;import iew;import y.R;import LayoutManager;import SnapHelper;import static ;import static _STATE_IDLE;public class BannerLayout2 extends FrameLayout { private int autoPlayDuration;//刷新间隔时间 private boolean showIndicator;//是否显⽰指⽰器 private RecyclerView indicatorContainer; private Drawable mSelectedDrawable; private Drawable mUnselectedDrawable; private IndicatorAdapter indicatorAdapter; private int indicatorMargin;//指⽰器间距 private RecyclerView mRecyclerView; private BannerLayoutManager mLayoutManager; private int WHAT_AUTO_PLAY = 1000; private boolean hasInit; private int bannerSize = 1; private int currentIndex; private boolean isPlaying = false; private boolean isAutoPlaying = true; int itemSpace; float centerScale; float moveSpeed; protected Handler mHandler = new Handler(new ck() { @Override public boolean handleMessage(Message msg) { if ( == WHAT_AUTO_PLAY) { if (currentIndex == rentPosition()) { ++currentIndex; ScrollToPosition(currentIndex); ptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration); refreshIndicator(); } } return false; } }); public BannerLayout2(Context context) { this(context, null); } public BannerLayout2(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BannerLayout2(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); } protected void initView(Context context, AttributeSet attrs) { TypedArray a = StyledAttributes(attrs, Layout); showIndicator = lean(Layout_showIndicator, true); autoPlayDuration = (Layout_interval, 4000); isAutoPlaying = lean(Layout_autoPlaying, true); itemSpace = (Layout_itemSpace, 20); centerScale = at(Layout_centerScale, 1.2f); moveSpeed = at(Layout_moveSpeed, 1.0f); if (mSelectedDrawable == null) { //绘制默认选中状态图形-- ⾃定义椭圆形指⽰器,圆⾓矩形 GradientDrawable drawable = (GradientDrawable) ources().getDrawable(521);// drawable .setColor(olor("#ff4400")); mSelectedDrawable = new LayerDrawable(new Drawable[]{drawable}); //绘制默认选中状态图形 -- 圆形// GradientDrawable selectedGradientDrawable = new GradientDrawable();// pe();// or();// e(dp2px(5), dp2px(5));// nerRadius(dp2px(5) / 2);// mSelectedDrawable = new LayerDrawable(new Drawable[]{selectedGradientDrawable}); } if (mUnselectedDrawable == null) { //绘制默认未选中状态图形 -- ⾃定义椭圆形指⽰器,圆⾓矩形 GradientDrawable drawable = (GradientDrawable) ources().getDrawable(522);// drawable .setColor(olor("#ff4400")); mUnselectedDrawable = new LayerDrawable(new Drawable[]{drawable}); //绘制默认未选中状态图形 -- 圆形// GradientDrawable unSelectedGradientDrawable = new GradientDrawable();// pe();// or();// e(dp2px(5), dp2px(5));// nerRadius(dp2px(5) / 2);// mUnselectedDrawable = new LayerDrawable(new Drawable[]{unSelectedGradientDrawable}); }// indicatorMargin = dp2px(4); indicatorMargin = (int) -0.2; int marginLeft = dp2px(16); int marginRight = dp2px(0); int marginBottom = dp2px(11); int gravity = ; int o = (Layout_orientation, 0); int orientation = 0; if (o == 0) { orientation = NTAL; } else if (o == 1) { orientation = AL; } e(); //轮播图部分 mRecyclerView = new RecyclerView(context); LayoutParams vpLayoutParams = new LayoutParams(_PARENT, _PARENT); addView(mRecyclerView, vpLayoutParams); mLayoutManager = new BannerLayoutManager(getContext(), orientation); mSpace(itemSpace); terScale(centerScale); eSpeed(moveSpeed); outManager(mLayoutManager); new CenterSnapHelper().attachToRecyclerView(mRecyclerView); //指⽰器部分 //指⽰器部分 indicatorContainer = new RecyclerView(context); LinearLayoutManager indicatorLayoutManager = new LinearLayoutManager(context, orientation, false); outManager(indicatorLayoutManager); indicatorAdapter = new IndicatorAdapter(); pter(indicatorAdapter); LayoutParams params = new LayoutParams(_CONTENT, _CONTENT);// y = | gravity;//指⽰器位置居左显⽰,可以设置上下左右的距离// y = | RIGHT;//设置指⽰器居右显⽰// y = | LEFT;//设置指⽰器居左显⽰ y = | CENTER;//设置指⽰器居中显⽰// gins(marginLeft, 0, marginRight, marginBottom); gins(0, 0, 0, marginBottom);// gins(0, 0, 0, 0); addView(indicatorContainer, params); if (!showIndicator) { ibility(GONE); } } // 设置是否禁⽌滚动播放 public void setAutoPlaying(boolean isAutoPlaying) { Playing = isAutoPlaying; setPlaying(Playing); } public boolean isPlaying() { return isPlaying; } //设置是否显⽰指⽰器 public void setShowIndicator(boolean showIndicator) { dicator = showIndicator; ibility(showIndicator ? VISIBLE : GONE); } //设置当前图⽚缩放系数 public void setCenterScale(float centerScale) { Scale = centerScale; terScale(centerScale); } //设置跟随⼿指的移动速度 public void setMoveSpeed(float moveSpeed) { eed = moveSpeed; eSpeed(moveSpeed); } //设置图⽚间距 public void setItemSpace(int itemSpace) { ace = itemSpace; mSpace(itemSpace); } /** * 设置轮播间隔时间 * * @param autoPlayDuration 时间毫秒 */ public void setAutoPlayDuration(int autoPlayDuration) { ayDuration = autoPlayDuration; } public void setOrientation(int orientation) { entation(orientation); entation(orientation); } /** * 设置是否⾃动播放(上锁) * * @param playing 开始播放 */ protected synchronized void setPlaying(boolean playing) { if (isAutoPlaying && hasInit) { if (!isPlaying && playing) { ptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration); isPlaying = true; } else if (isPlaying && !playing) { Messages(WHAT_AUTO_PLAY); isPlaying = false; } } } /** * 设置轮播数据集 */ public void setAdapter(r adapter) { hasInit = false; pter(adapter); bannerSize = mCount(); inite(bannerSize >= 3); setPlaying(true); crollListener(new llListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (dx != 0) { setPlaying(false); } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { int first = rentPosition(); Log.d("xxx", "onScrollStateChanged"); if (currentIndex != first) { currentIndex = first; } if (newState == SCROLL_STATE_IDLE) { setPlaying(true); } refreshIndicator(); } }); hasInit = true; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ion()) { case _DOWN: setPlaying(false); break; case _UP: case _CANCEL: setPlaying(true); break; } } return chTouchEvent(ev); } @Override protected void onAttachedToWindow() { chedToWindow(); setPlaying(true); } @Override protected void onDetachedFromWindow() { chedFromWindow(); setPlaying(false); } @Override protected void onWindowVisibilityChanged(int visibility) { owVisibilityChanged(visibility); if (visibility == VISIBLE) { setPlaying(true); } else { setPlaying(false); } } /** * 标⽰点适配器 */ protected class IndicatorAdapter extends r { int currentPosition = 0; public void setPosition(int currentPosition) { tPosition = currentPosition; } @Override public lder onCreateViewHolder(ViewGroup parent, int viewType) { ImageView bannerPoint = new ImageView(getContext()); Params lp = new Params(_CONTENT, _CONTENT);// gins(indicatorMargin, indicatorMargin, indicatorMargin, indicatorMargin); gins(0, 0, 0, 0); outParams(lp); return new lder(bannerPoint) { }; } @Override public void onBindViewHolder(lder holder, int position) { ImageView bannerPoint = (ImageView) ew; geDrawable(currentPosition == position ? mSelectedDrawable : mUnselectedDrawable); } @Override public int getItemCount() { return bannerSize; } } protected int dp2px(int dp) { return (int) imension(X_UNIT_DIP, dp, tem().getDisplayMetrics()); tem().getDisplayMetrics()); } /** * 改变导航的指⽰点 */ protected synchronized void refreshIndicator() { if (showIndicator && bannerSize > 1) { ition(currentIndex % bannerSize); DataSetChanged(); } } public interface OnBannerItemClickListener { void onItemClick(int position); }} : 图⼆的第⼆个效果的布局:

4.布局及属性:: 主布局:

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信