欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義輪播圖效果

 更新時間:2022年08月15日 17:10:13   作者:吹著空調(diào)哼著歌  
這篇文章主要為大家詳細介紹了Android自定義輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android自定義輪播圖的具體代碼,供大家參考,具體內(nèi)容如下

定義Banner

主要使用ViewPager實現(xiàn)滑動

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();

? ? }


}

定義定時器Handler

主要處理ViewPager的滾動 開啟定時任務(wù) ViewPager自動滾動

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);
? ? ? ? }
? ? }

}

繪制一個下標(biāo)指示器

主要根據(jù)需求自行繪制 可有可無 和ViewPager關(guān)聯(lián)在一起 實現(xiàn)聯(lián)動

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;//每個的寬度加上中間間距的寬度
? ? ? ? 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android基礎(chǔ)之獲取LinearLayout的寬高

    Android基礎(chǔ)之獲取LinearLayout的寬高

    LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失。有的時候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • Kotlin與Java相互調(diào)用的完整實例

    Kotlin與Java相互調(diào)用的完整實例

    Kotlin的設(shè)計過程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過來在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • Android編程實現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例

    Android編程實現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例

    這篇文章主要介紹了Android編程實現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能,涉及Android權(quán)限控制及通訊錄相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • android studio 3.4配置Android -jni 開發(fā)基礎(chǔ)的教程詳解

    android studio 3.4配置Android -jni 開發(fā)基礎(chǔ)的教程詳解

    這篇文章主要介紹了android studio 3.4配置Android -jni 開發(fā)基礎(chǔ),本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Android啟動屏實現(xiàn)左右滑動切換查看功能

    Android啟動屏實現(xiàn)左右滑動切換查看功能

    這篇文章主要介紹了Android啟動屏實現(xiàn)左右滑動切換查看功能的相關(guān)資料,針對新功能屬性介紹和啟動屏進行詳細講解,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)

    Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)

    這篇文章主要為大家介紹了Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android實現(xiàn)懸浮窗的簡單方法實例

    Android實現(xiàn)懸浮窗的簡單方法實例

    相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時候,點擊Home鍵,視頻小窗口仍然會在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實現(xiàn)懸浮窗的簡單方法,需要的朋友可以參考下
    2021-09-09
  • Android自定義Gradle插件的詳細過程

    Android自定義Gradle插件的詳細過程

    Groovy語言是一種jvm語言,最終也是編譯成class文件然后在jvm上執(zhí)行,所以所有的Java語言的特性Groovy都支持,我們可以完全混寫Java和Groovy,對Android自定義Gradle插件相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • 利用Android實現(xiàn)光影流動特效的方法詳解

    利用Android實現(xiàn)光影流動特效的方法詳解

    Flutter 的畫筆類 Paint 提供了很多圖形繪制的配置屬性,來供我們繪制更豐富多彩的圖形。本篇我們引入一個 Paint 類新的屬性:maskFilter,再結(jié)合之前的 shader 和動畫,制作出光影流動特效,感興趣的可以嘗試一下
    2022-07-07
  • Android ImageView Src 和Background 區(qū)別

    Android ImageView Src 和Background 區(qū)別

    這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論