Android自定义view二Banner

自定义view第二篇,无限自动轮播的banner

public class BannerView extends LinearLayout implements ViewPager.OnPageChangeListener{

private Context mContext;

private ViewPager viewPager;
private LinearLayout pointsView;
private RelativeLayout allViewRl;

private List imageUrls;
private List webUrls;
private int selectedPointResId;
private int defaultPointResId;

private List pointViews;

private int curPagePosition;//当前显示页面pos

private static final int WHAT_PAGE_CHANGE = 1000;
private static final int TIME_PAGE_CHANGE = 10 * 1000;

private Class clazz; //要跳转的web也没class

private int imageHeight = 200;//图片高度
private int distanceForPoints = 20;//图片高度

private OnClickPicPositionListener listener;

private Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case WHAT_PAGE_CHANGE:
                if(viewPager != null){
                    viewPager.setCurrentItem(curPagePosition + 1);
                    mHandler.sendEmptyMessageDelayed(WHAT_PAGE_CHANGE,TIME_PAGE_CHANGE);
                }
                break;
        }

    }
};

public BannerView(Context context) {
    this(context,null,0);
}

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

public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mContext = context;
    initView();

}

/**
 * 初始化view
 */
private void initView() {
    View view = LayoutInflater.from(mContext).inflate(R.layout.view_banner,this);
    viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    pointsView = (LinearLayout) view.findViewById(R.id.pointsView);
    pointViews = new ArrayList<>();
    viewPager.addOnPageChangeListener(this);

   // imageHeight = (int) mContext.getResources().getDimension(R.dimen.dp_130);
    imageHeight = App.screenWidth * 7 / 16;
    allViewRl = (RelativeLayout) view.findViewById(R.id.allViewRl);
    LinearLayout.LayoutParams bannerParams = new LinearLayout.LayoutParams(App.screenWidth,imageHeight);
    allViewRl.setLayoutParams(bannerParams);
}

/**
 * 设置广告位的图片地址数据,
 * @param imageUrls 图片地址集合
 * @param webUrls 点击图片跳转地址集合
 * @param selectedPointResId 被选中的点的状态
 * @param defaultPointResId 默认状态
 */
public void setImageUrls(List imageUrls,List webUrls,int selectedPointResId,int defaultPointResId,Class clazz){
    this.imageUrls = imageUrls;
    this.webUrls = webUrls;
    this.selectedPointResId = selectedPointResId;
    this.defaultPointResId = defaultPointResId;
    this.clazz = clazz;

    initPicsAndPoints();
    viewPager.setAdapter(new MyBannerAdapter());
    curPagePosition = imageUrls.size() * 1000;
    viewPager.setCurrentItem(curPagePosition);
}

/**
 * 设置banner图片的高度 和状态点的间距
 * @param imageHeight
 */
public void setHeightAndDistance(int imageHeight,int distanceForPoints){
    this.imageHeight = imageHeight;
    this.distanceForPoints = distanceForPoints;
}

/**
 * 广告位开始滚动
 */
public void startMove(){
    if(!mHandler.hasMessages(WHAT_PAGE_CHANGE)){
        mHandler.sendEmptyMessageDelayed(WHAT_PAGE_CHANGE,TIME_PAGE_CHANGE);
    }
}

/**
 * 停止滚动
 */
public void stopMove(){
    if(mHandler.hasMessages(WHAT_PAGE_CHANGE)){
        mHandler.removeMessages(WHAT_PAGE_CHANGE);
    }
}

/**
 * 初始化点
 */
private void initPicsAndPoints() {
    pointViews.clear();
    pointsView.removeAllViews();
    ImageView pointView;
    for (int i = 0; i < imageUrls.size(); i++) {
        pointView = new ImageView(mContext);
        if(i == 0){
            pointView.setImageResource(selectedPointResId);
        }else{
            pointView.setImageResource(defaultPointResId);
        }
        pointView.setPadding(distanceForPoints,0,0,0);
        pointViews.add(pointView);
        pointsView.addView(pointView);
    }
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    curPagePosition = position;
    changePointState(position % pointViews.size());

}

@Override
public void onPageScrollStateChanged(int state) {
    switch (state){
        case ViewPager.SCROLL_STATE_DRAGGING:
            stopMove();
            break;
        case ViewPager.SCROLL_STATE_IDLE:
            startMove();
            break;
    }

}

/**
 * 改变点的状态选中
 * @param position
 */
private void changePointState(int position){
    for (int i = 0; i < pointViews.size(); i++) {
        if(i == position){
            pointViews.get(i).setImageResource(selectedPointResId);
        }else{
            pointViews.get(i).setImageResource(defaultPointResId);
        }
    }

}

/**
 * 跳转网页
 */
private void startActivity(String webUrl) {
    Intent intent = new Intent(mContext,clazz);
    intent.putExtra(SimpleWebActivity.WEB_URL,webUrl);
    mContext.startActivity(intent);
}


public class MyBannerAdapter extends PagerAdapter {

    private LinearLayout.LayoutParams params;
    private ImageView imageView;
    private int position;

    public MyBannerAdapter(){
        params = new LinearLayout.LayoutParams(App.screenWidth,imageHeight);
    }

    @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, final int position) {
        final int currentItem = position % imageUrls.size();
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(webUrls != null && webUrls.size() > 0){
                    if(listener != null){
                        listener.onClickPicPosition(currentItem);
                    }
                }
            }
        });
        ImageUtil.loadImg(mContext,imageView,imageUrls.get(currentItem));
        container.addView(imageView);
        return imageView;
    }

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

}

public interface OnClickPicPositionListener{
    void onClickPicPosition(int position);
}

public void setOnClickPicPositionListener(OnClickPicPositionListener listener){
    this.listener = listener;
}

}

你可能感兴趣的:(Android,Android,自定义控件)