Android自定義輪播圖效果
本文實(shí)例為大家分享了Android自定義輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
定義Banner
主要使用ViewPager實(shí)現(xiàn)滑動(dòng)
public class Banner extends FrameLayout { ? ? public Context context; ? ? private @LayoutRes ? ? int layoutId = R.layout.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) { ? ? ? ? this.adapter = adapter; ? ? ? ? pager.setAdapter(adapter); ? ? ? ? indicatorView.setPager(pager); ? ? } ? ? private void init(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? this.context = context; ? ? ? ? inflate = LayoutInflater.from(context).inflate(layoutId, this, true); ? ? ? ? pager = inflate.findViewById(R.id.banner_pager); ? ? ? ? indicatorView = inflate.findViewById(R.id.indicatorView); ? ? ? ? mHandler = new AutoHandler(pager); ? ? ? ? mHandler.start(); ? ? } ? ? public abstract static class BannerAdapter extends PagerAdapter { ? ? ? ? private List list; ? ? ? ? private Context context; ? ? ? ? public BannerAdapter(List list, Context context) { ? ? ? ? ? ? this.list = list; ? ? ? ? ? ? this.context = context; ? ? ? ? } ? ? ? ? @Override ? ? ? ? public int getCount() { ? ? ? ? ? ? if (null == list || list.size() == 0) { ? ? ? ? ? ? ? ? return 1; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? return list.size(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? @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 = LayoutInflater.from(context).inflate(getLayout(), container, false); ? ? ? ? ? ? ? ? container.addView(inflate); ? ? ? ? ? ? ? ? setView(inflate, position); ? ? ? ? ? ? ? ? return inflate; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ImageView imageView = new ImageView(context); ? ? ? ? ? ? ? ? container.addView(imageView); ? ? ? ? ? ? ? ? if (list.size() > 0) { ? ? ? ? ? ? ? ? ? ? Glide.with(context).load(list.get(position)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? .apply(new RequestOptions().centerCrop()) ? ? ? ? ? ? ? ? ? ? ? ? ? ? .into(imageView); ? ? ? ? ? ? ? ? } else { // ? ? ? ? ? ? ? ? ? ?Glide.with(context) // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.load(R.mipmap.ic_launcher) // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.apply(new RequestOptions().centerCrop()) // ? ? ? ? ? ? ? ? ? ? ? ? ? ?.into(imageView); ? ? ? ? ? ? ? ? ? ? imageView.setBackgroundResource(R.mipmap.ic_launcher); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return imageView; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? @Override ? ? ? ? public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { ? ? ? ? ? ? container.removeView((View) object); ? ? ? ? } ? ? ? ? protected abstract void setView(View inflate, int position); ? ? ? ? protected abstract int getLayout(); ? ? } }
定義定時(shí)器Handler
主要處理ViewPager的滾動(dòng) 開啟定時(shí)任務(wù) ViewPager自動(dòng)滾動(dòng)
public class AutoHandler extends Handler { ? ? private ViewPager pager; ? ? public static int TIME = 1000 * 2; ? ? public boolean stopHandler; ? ? public AutoHandler(ViewPager pager) { ? ? ? ? this.pager = pager; ? ? } ? ? @Override ? ? public void handleMessage(@NonNull Message msg) { ? ? ? ? super.handleMessage(msg); ? ? ? ? switch (msg.what) { ? ? ? ? ? ? case 100: ? ? ? ? ? ? ? ? if (!stopHandler) { ? ? ? ? ? ? ? ? ? ? sendEmptyMessageDelayed(100, TIME); ? ? ? ? ? ? ? ? ? ? int position = pager.getCurrentItem() + 1; ? ? ? ? ? ? ? ? ? ? if (position >= pager.getAdapter().getCount()) { ? ? ? ? ? ? ? ? ? ? ? ? position = 0; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? pager.setCurrentItem(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); ? ? ? ? } ? ? } }
繪制一個(gè)下標(biāo)指示器
主要根據(jù)需求自行繪制 可有可無 和ViewPager關(guān)聯(lián)在一起 實(shí)現(xiàn)聯(lián)動(dòng)
public class IndicatorView extends View { ? ? private Context context; ? ? private ValueAnimator valueAnimator; ? ? private float value; ? ? public int indiWidth; ? ? public int indiHeight; ? ? public int indiDivide; ? ? //0圓角 ? 1直角 ? ? 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) { ? ? ? ? this.pager = pager; ? ? ? ? this.adapter = pager.getAdapter(); ? ? ? ? dataSetObserver = new DataSetObserver() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onChanged() { ? ? ? ? ? ? ? ? super.onChanged(); ? ? ? ? ? ? ? ? setCount(adapter.getCount()); ? ? ? ? ? ? ? ? setCurPosition(pager.getCurrentItem()); ? ? ? ? ? ? } ? ? ? ? }; ? ? ? ? if (null != adapter) { ? ? ? ? ? ? setCount(adapter.getCount()); ? ? ? ? ? ? setCurPosition(pager.getCurrentItem()); ? ? ? ? ? ? adapter.registerDataSetObserver(dataSetObserver); ? ? ? ? } ? ? ? ? pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { ? ? ? ? ? ? @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) { ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? pager.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) { ? ? ? ? ? ? ? ? if (oldAdapter != newAdapter) { ? ? ? ? ? ? ? ? ? ? adapter = newAdapter; ? ? ? ? ? ? ? ? ? ? setCount(adapter.getCount()); ? ? ? ? ? ? ? ? ? ? setCurPosition(viewPager.getCurrentItem()); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? public void setCount(int count) { ? ? ? ? this.count = count; ? ? ? ? if (count <= 1) { ? ? ? ? ? ? setVisibility(INVISIBLE); ? ? ? ? } ? ? ? ? requestLayout(); ? ? ? ? postInvalidate(); ? ? } ? ? public void setCurPosition(int curPos) { ? ? ? ? /*記錄上次記錄*/ ? ? ? ? //lastPos = this.curPos; ? ? ? ? this.curPosition = curPos; ? ? ? ? //postInvalidate(); ? ? ? ? valueAnimator.start(); ? ? } ? ? 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) { ? ? ? ? this.context = context; ? ? ? ? valueAnimator = ValueAnimator.ofFloat(0, 1f); ? ? ? ? valueAnimator.setDuration(200); ? ? ? ? valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator animation) { ? ? ? ? ? ? ? ? value = (float) animation.getAnimatedValue(); ? ? ? ? ? ? ? ? postInvalidate(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndicatorView); ? ? ? ? indiHeight = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_height, getResources().getDimension(R.dimen.dp4)); ? ? ? ? indiWidth = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_width, getResources().getDimension(R.dimen.dp4)); ? ? ? ? indiDivide = (int) typedArray.getDimension(R.styleable.IndicatorView_indi_divier, getResources().getDimension(R.dimen.dp4)); ? ? ? ? normalColor = typedArray.getColor(R.styleable.IndicatorView_indi_color, Color.parseColor("#66dddddd")); ? ? ? ? selectColor = typedArray.getColor(R.styleable.IndicatorView_indi_color_select, Color.parseColor("#eedddddd")); ? ? ? ? mode = typedArray.getInteger(R.styleable.IndicatorView_indi_mode, 0); ? ? ? ? curPosition = 0; ? ? ? ? count = 0; ? ? ? ? paint = new Paint(); ? ? ? ? paint.setAntiAlias(true); ? ? ? ? paint.setColor(normalColor); ? ? } ? ? @Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? ? ? width = indiWidth * count + (count - 1) * indiDivide;//每個(gè)的寬度加上中間間距的寬度 ? ? ? ? height = indiHeight; ? ? ? ? setMeasuredDimension(width, height); ? ? } ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? int x = i * (indiDivide + indiWidth); ? ? ? ? ? ? if (mode == 0) { ? ? ? ? ? ? ? ? paint.setColor(normalColor); ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ? ? ? ? ? ? ? ? ? ? if (curPosition == i) { ? ? ? ? ? ? ? ? ? ? ? ? paint.setColor(selectColor); ? ? ? ? ? ? ? ? ? ? ? ? if (curPosition > lastPosition) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x, 0, x + value * indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRoundRect(x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, indiHeight / 2, indiHeight / 2, paint); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (mode == 1) { ? ? ? ? ? ? ? ? ? ? paint.setColor(normalColor); ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x, 0, x + indiWidth, indiHeight, paint); ? ? ? ? ? ? ? ? ? ? if (curPosition == i) { ? ? ? ? ? ? ? ? ? ? ? ? paint.setColor(selectColor); ? ? ? ? ? ? ? ? ? ? ? ? if (curPosition > lastPosition) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x, 0, x + value * indiWidth, indiHeight, paint); ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawRect(x + (1 - value) * indiWidth, 0, x + indiWidth, indiHeight, paint); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
在Activity中使用
banner.setAdapter(new Banner.BannerAdapter(strings, this) { ? ? ? ? ? ? @Override ? ? ? ? ? ? protected void setView(View inflate, int position) { ? ? ? ? ? ? ? ? ImageView img = inflate.findViewById(R.id.img); ? ? ? ? ? ? ? ? Glide.with(MainActivity.this).load(strings.get(position)) ? ? ? ? ? ? ? ? ? ? ? ? .apply(new RequestOptions().centerCrop()) ? ? ? ? ? ? ? ? ? ? ? ? .into(img); ? ? ? ? ? ? } ? ? ? ? ? ? @Override ? ? ? ? ? ? protected int getLayout() { ? ? ? ? ? ? ? ? return R.layout.img; // ? ? ? ? ? ? ? ?return 0; ? ? ? ? ? ? } });
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android實(shí)現(xiàn)炫酷輪播圖效果
- Android使用viewpager實(shí)現(xiàn)自動(dòng)無限輪播圖
- Android實(shí)現(xiàn)ViewPage輪播圖效果
- Android ViewPager實(shí)現(xiàn)輪播圖效果
- Android開發(fā)實(shí)現(xiàn)的自動(dòng)換圖片、輪播圖效果示例
- Android如何使用RecyclerView打造首頁輪播圖
- android實(shí)現(xiàn)banner輪播圖無限輪播效果
- 簡單實(shí)現(xiàn)android輪播圖
- Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖
相關(guān)文章
Android基礎(chǔ)之獲取LinearLayout的寬高
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來排列所有的widgets或者其他的containers,超過邊界時(shí),某些控件將缺失或消失。有的時(shí)候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11Kotlin與Java相互調(diào)用的完整實(shí)例
Kotlin的設(shè)計(jì)過程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過來在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下2021-12-12Android編程實(shí)現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能,涉及Android權(quán)限控制及通訊錄相關(guān)操作技巧,需要的朋友可以參考下2017-07-07android studio 3.4配置Android -jni 開發(fā)基礎(chǔ)的教程詳解
這篇文章主要介紹了android studio 3.4配置Android -jni 開發(fā)基礎(chǔ),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09Android啟動(dòng)屏實(shí)現(xiàn)左右滑動(dòng)切換查看功能
這篇文章主要介紹了Android啟動(dòng)屏實(shí)現(xiàn)左右滑動(dòng)切換查看功能的相關(guān)資料,針對(duì)新功能屬性介紹和啟動(dòng)屏進(jìn)行詳細(xì)講解,感興趣的小伙伴們可以參考一下2016-01-01Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)
這篇文章主要為大家介紹了Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android實(shí)現(xiàn)懸浮窗的簡單方法實(shí)例
相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)懸浮窗的簡單方法,需要的朋友可以參考下2021-09-09利用Android實(shí)現(xiàn)光影流動(dòng)特效的方法詳解
Flutter 的畫筆類 Paint 提供了很多圖形繪制的配置屬性,來供我們繪制更豐富多彩的圖形。本篇我們引入一個(gè) Paint 類新的屬性:maskFilter,再結(jié)合之前的 shader 和動(dòng)畫,制作出光影流動(dòng)特效,感興趣的可以嘗試一下2022-07-07Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09