AndroidBannerView通用封裝詳解

之前封裝過一個,但總覺得不夠優(yōu)雅,就有了這個通用封裝,很簡潔,不知道夠不夠優(yōu)雅,不過原來那個有跟隨指示器和絲滑滑動效果,感興趣可以看一下。

創(chuàng)新互聯(lián)公司專注于嶺東網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供嶺東營銷型網(wǎng)站建設(shè),嶺東網(wǎng)站制作、嶺東網(wǎng)頁設(shè)計、嶺東網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造嶺東網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供嶺東網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

封裝過程

1、自定義屬性

selectPoint:選中指示器圖標(biāo)
normalPoint:未選中指示器圖標(biāo)
pointWidth:圖標(biāo)寬度
switchTime:輪播間隔事件
location:指示器位置,下中或下右

<declare-styleable name="NewBannerView">
    <attr name="selectPoint" format="reference" />
    <attr name="normalPoint" format="reference" />
    <attr name="pointWidth" format="dimension" />
    <attr name="switchTime" format="integer" />
    <attr name="location">
      <enum name="CENTER" value="0" />
      <enum name="RIGHT" value="1" />
    </attr>
  </declare-styleable>

2、初始化View
初始化ViewPager和指示器組合View

3、綁定數(shù)據(jù)源
通過setImageData設(shè)置輪播圖數(shù)據(jù)源

4、綁定點(diǎn)擊事件
通過OnPageClickListener綁定點(diǎn)擊事件

5、開啟關(guān)閉輪播
start和stop方法開啟和關(guān)閉輪播

用法

xml中

<com.goldou.lovesee.view.NewBannerView
    android:id="@+id/bannerView"
    android:layout_width="match_parent"
    app:selectPoint="@drawable/red_point"
    app:normalPoint="@drawable/gray_point"
    app:switchTime="4000"
    app:location="RIGHT"
    android:layout_height="200dp" />

activity中

int[] imageList = {R.drawable.me_top, R.drawable.me_top, R.drawable.me_top, R.drawable.me_top};
    NewBannerView bannerView = view.findViewById(R.id.bannerView);
    bannerView.setImageData(imageList);
    bannerView.start();
    bannerView.setOnPageClickListener(new NewBannerView.OnPageClickListener() {
      @Override
      public void onPageClick(int position) {
        Toast.makeText(getActivity(), position + "", Toast.LENGTH_SHORT).show();
      }
    });

BannerView

public class NewBannerView extends RelativeLayout implements View.OnClickListener {
  private Context context;
  private int selectPoint, normalPoint;
  private float pointWidth = 7;
  private int location;
  private int CENTER = 0, RIGHT = 1;
  private int lastPosition = 0;
  private ViewPager viewPager;
  private int switchTime = 5000;
  private int[] images;
  private OnPageClickListener onPageClickListener;

  private Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
      if (msg.what == 101) {
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        start();
      }
      return false;
    }
  });

  public NewBannerView(Context context) {
    this(context, null);
  }

  public NewBannerView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public NewBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    initAttr(attrs);
  }

  private void initAttr(AttributeSet attrs) {
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NewBannerView);
    selectPoint = array.getResourceId(R.styleable.NewBannerView_selectPoint, R.mipmap.ic_launcher_round);
    normalPoint = array.getResourceId(R.styleable.NewBannerView_normalPoint, R.mipmap.ic_launcher_round);
    pointWidth = array.getDimension(R.styleable.NewBannerView_pointWidth, pointWidth);
    location = array.getInteger(R.styleable.NewBannerView_location, RIGHT);
    switchTime = array.getInteger(R.styleable.NewBannerView_switchTime, switchTime);
    array.recycle();
  }

  public void setImageData(final int[] images) {
    this.images = images;
    final LinearLayout ll_point = new LinearLayout(context);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    layoutParams.bottomMargin = 20;
    if (location == CENTER) {
      layoutParams.addRule(CENTER_HORIZONTAL, RelativeLayout.TRUE);
    } else {
      layoutParams.addRule(ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
      layoutParams.rightMargin = 20;
    }
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(UIUtil.dip2px(pointWidth), UIUtil.dip2px(pointWidth));
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(UIUtil.dip2px(pointWidth), UIUtil.dip2px(pointWidth));
    params1.leftMargin = 0;
    params2.leftMargin = UIUtil.dip2px(pointWidth);
    for (int i = 0; i < images.length; i++) {
      ImageView point = new ImageView(context);
      if (i == 0) {
        point.setBackgroundResource(selectPoint);
        point.setLayoutParams(params1);
      } else {
        point.setBackgroundResource(normalPoint);
        point.setLayoutParams(params2);
      }
      ll_point.addView(point);
    }

    viewPager = new ViewPager(context);
    viewPager.setAdapter(new BannerAdapter());
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        position = position % images.length;
        if (lastPosition == position) {
          return;
        }
        ll_point.getChildAt(position).setBackgroundResource(selectPoint);
        ll_point.getChildAt(lastPosition).setBackgroundResource(normalPoint);
        lastPosition = position;
      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });
    LayoutParams layoutParams1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    addView(viewPager, layoutParams1);
    addView(ll_point, layoutParams);
  }

  public void start() {
    handler.sendEmptyMessageDelayed(101, switchTime);
  }

  public void stop() {
    handler.removeCallbacksAndMessages(null);
  }

  private int getCurrentItem() {
    return viewPager.getCurrentItem() % images.length;
  }

  @Override
  public void onClick(View view) {
    onPageClickListener.onPageClick(getCurrentItem());
  }

  public interface OnPageClickListener {
    void onPageClick(int position);
  }

  public void setOnPageClickListener(OnPageClickListener onPageClickListener) {
    this.onPageClickListener = onPageClickListener;
  }

  private class BannerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
      return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      position = position % images.length;
      ImageView imageView = new ImageView(context);
      imageView.setImageResource(images[position]);
      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView.setOnClickListener(NewBannerView.this);
      container.addView(imageView);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((View) object);
    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞標(biāo)題:AndroidBannerView通用封裝詳解
URL標(biāo)題:http://bm7419.com/article16/jdisdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、域名注冊、Google、網(wǎng)站導(dǎo)航、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)