自定义viewpager圆点
自定义viewpager圆点
public class IndicatorView extends LinearLayoutCompat {
private Context context;
private Drawable selectColor;
private Drawable normalColor;
private float alpha;
public IndicatorView(Context context) {
super(context);
this.context = context;
}
public IndicatorView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
public IndicatorView initView(int indicatorCount,int selectPosition,Drawable selectDrawble,Drawable normalDrawble,float alpha){
this.selectColor = selectDrawble;
this.normalColor = normalDrawble;
this.alpha = alpha;
for (int i=0;i<indicatorCount;i++){
AppCompatImageView ivIndicator = new AppCompatImageView(context);
LinearLayoutCompat.LayoutParams lp = new LinearLayoutCompat.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.leftMargin = i == 0 ? 0 : dip2px(context,6);
ivIndicator.setLayoutParams(lp);
ivIndicator.setBackgroundDrawable(i == selectPosition ? selectDrawble : normalDrawble);
ivIndicator.setAlpha(i == selectPosition ? 1 : alpha);
addView(ivIndicator);
}
return this;
}
public void changeIndicator(int position){
int count = getChildCount();
for (int i=0;i<count;i++){
AppCompatImageView ivIndecator = (AppCompatImageView) getChildAt(i);
ivIndecator.setBackgroundDrawable(normalColor);
ivIndecator.setAlpha(alpha);
}
AppCompatImageView ivIndecator = (AppCompatImageView) getChildAt(position);
ivIndecator.setBackgroundDrawable(selectColor);
ivIndecator.setAlpha(1.0f);
}
public static class Builder{
private Context context;
private int position;
private int indicatorCount;
private Drawable selectColor;
private Drawable normalColor;
private float alpha;
public Builder setContext(Context context){
this.context = context;
return this;
}
public Builder setSelectPosition(int position){
this.position = position;
return this;
}
public Builder setIndicatorCount(int indicatorCount){
this.indicatorCount = indicatorCount;
return this;
}
public Builder setSelectColor(Drawable selectColor){
this.selectColor = selectColor;
return this;
}
public Builder setNormalColor(Drawable normalColor){
this.normalColor = normalColor;
return this;
}
public Builder setNormalAlpha(float alpha){
this.alpha = alpha;
return this;
}
public IndicatorView builder(){
return new IndicatorView(context).initView(indicatorCount,position,selectColor,normalColor,alpha);
}
}
public static int dip2px(Context context, float dpValue) {
return (int) ((dpValue * context.getResources().getDisplayMetrics().density) + 0.5f);
}
}
indicator = new IndicatorView.Builder()
.setContext(MainActivity.this)
//数量
.setIndicatorCount(4)
.setSelectPosition(0)
.setSelectColor(getResources().getDrawable(R.drawable.sel_shap))
.setNormalColor(getResources().getDrawable(R.drawable.nomal_shap))
.setNormalAlpha(0.6f)
.builder();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
indicator.setLayoutParams(layoutParams);
//添加到布局
main_ider.addView(indicator);
ViewPager滑动执行indicator.changeIndicator(arg0);
本文地址:https://blog.csdn.net/qq_37912106/article/details/110237814
上一篇: JQuery重置form
下一篇: 枚举类(Python技巧)