1.ScrollBarView
public class ScrollBarView extends View {
private Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private RectF mBgRect = new RectF();
private Float mRadius = 0f;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private RectF mRect = new RectF();
private int viewWidth = 0;
private int mBgColor = getResources().getColor(R.color.scrollbar_color);
private int mIndicatorColor = getResources().getColor(R.color.scroll_color);
Float ratio = 0.5f; //长度比例
public ScrollBarView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ScrollBarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public void init(AttributeSet attrs){
@SuppressLint("CustomViewStyleable") TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HIndicator);
mBgColor = typedArray.getColor(R.styleable.HIndicator_hi_bgColor, mBgColor);
mIndicatorColor = typedArray.getColor(R.styleable.HIndicator_hi_indicatorColor, mIndicatorColor);
typedArray.recycle();
mBgPaint.setColor(mBgColor);
mBgPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mIndicatorColor);
mPaint.setStyle(Paint.Style.FILL);
}
public void set(Float value) {
ratio = value;
invalidate();
}
Float progress = 0f; //滑动进度比例
public void setProgress(Float value) {
progress = value;
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
mBgRect.set(0f, 0f, w * 1f, h * 1f);
mRadius = h / 2f;
}
/**
* 设置指示器背景进度条的颜色
* @param color 背景色
*/
public void setBgColor(int color) {
mBgPaint.setColor(color);
invalidate();
}
/**
* 设置指示器的颜色
* @param color 指示器颜色
*/
public void setIndicatorColor(int color) {
mPaint.setColor(color);
invalidate();
}
/**
* 绑定recyclerView
*/
public void bindRecyclerView(RecyclerView recyclerView) {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
float offsetX = recyclerView.computeHorizontalScrollOffset();
float range = recyclerView.computeHorizontalScrollRange();
float extend = recyclerView.computeHorizontalScrollExtent();
float progres = offsetX * 1.0f / (range - extend);
progress = progres; //设置滚动距离所占比例
setProgress(progress);
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制背景
canvas.drawRoundRect(mBgRect, mRadius, mRadius, mBgPaint);
//计算指示器的长度和位置
float leftOffset = viewWidth * (1f - ratio) * progress;
float left = mBgRect.left + leftOffset;
float right = left + viewWidth * ratio;
mRect.set(left, mBgRect.top, right, mBgRect.bottom);
//绘制指示器
canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);
}
}
2.定义资源文件
<declare-styleable name="HIndicator">
<attr name="hi_bgColor" format="color"></attr>
<attr name="hi_indicatorColor" format="color"></attr>
</declare-styleable>
3.ScreenWidthHelper
/**
* RecycleView 占 屏 幕 的 宽 度
*/
public class ScreenWidthHelper {
//reclyview左间距
private static final int left = 85;
//每个item所占宽度
private static final int itemWidth = 230;
Context context;
public ScreenWidthHelper(Context context) {
this.context = context;
}
public int getwidth(){
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int widthPixels = displayMetrics.widthPixels;
float density = displayMetrics.density;
int screenWidth = (int) (widthPixels / density); //屏幕的宽度
int i = left * 2; //recycleview距离右侧和左侧的距离
int recywidth = screenWidth - i; //recycleview在屏幕的大小
int entry = recywidth / itemWidth; // 一屏能显示几个条目
return entry;
}
}
4.activity引用
private ScrollBarView mHindicator;
mHindicator = findViewById(R.id.hIndicator);
//绑定reclyview
mHindicator.bindRecyclerView(mReclyview);
screenWidth(appList);
//指 示 器 显 示 或 隐 藏
private void screenWidth(List<AppInfo> appInfoList) {
ScreenWidthHelper screenWidthMapper = new ScreenWidthHelper(this);
if (appInfoList.size() > screenWidthMapper.getwidth()){
mHindicator.setVisibility(View.VISIBLE);
}else {
mHindicator.setVisibility(View.GONE);
}
}
5.布局文件
<com.my.demo.ui.view.ScrollBarView
android:id="@+id/hIndicator"
android:layout_width="108dp"
android:layout_height="10dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="534dp"/>