PictureProgressBar
PictureProgressBar copied to clipboard
如何去除控件左边所留间距
右边边距通过onMeasure()已去除 ,左边未找到解决方法,谢谢回复
你好,去除边距的话,会导致一些问题,不然的话我一早就把边距去掉了:
- (左边)导致进度图片中心一开始是不在进度条的零点,如果改为设置在零点,那么图片就会有一部分超出View边界,无法显示
- (右边)导致进度图片中心走到最后的时候不是在进度条的终点,当然也可以改为走到终点,那么图片就会有一部分超出View边界,无法显示 如果上面的你都可以解决或者不在乎,可以这样改掉边距: 在onMeasure去掉右边距:
//重写onMeasure,以自定义获取进度条的宽高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
//在这里实现计算需要wrap_content时需要的宽
width = halfDrawableWidth * 2;
}
if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
//在这里实现计算需要wrap_content时需要的高
height = halfDrawableHeight * 2;
}
progressWidth = width;
//如果不是自定义设置进度条高度,就直接把高度当作进度条高度
if (!isSetBar) {
progressHeight = height;
}
//注释掉这句
//如果有图片,就为图片预留空间
// if (drawable != null || drawableIds != null) {
// progressWidth = width - halfDrawableWidth;
// }
Log.d(TAG, "onMeasure: progressWidth " + progressWidth);
//传入处理后的宽高
setMeasuredDimension(width, height);
}
左边距在drawBar()里面改掉减去图片半宽:
//画进度条
private void drawBar(Canvas canvas) {
if (backgroundDrawable != null && barDrawable != null) {
//图片进度条
canvas.save();
canvas.translate(0, y - progressHeight / 2 + progressHeightOffset);
rectFBG.set(0, 0,
progressWidth , progressHeight);
rectFPB.set(0, 0,
x, progressHeight);
// rectFBG.set(0, 0, getWidth(), getHeight());
canvas.drawRect(rectFBG, paintBackGround);
canvas.drawRect(rectFPB,paintBar);
canvas.restore();
} else {
//非图片背景处理
rectFBG.set(0, y - progressHeight / 2 + progressHeightOffset,
progressWidth, y + progressHeight / 2 + progressHeightOffset);
rectFPB.set(0, y - progressHeight / 2 + progressHeightOffset,
x, y + progressHeight / 2 + progressHeightOffset);
if (isRound) {
canvas.drawRoundRect(rectFBG, roundX, roundY, paintBackGround);
if (x > halfDrawableWidth * 2) {
canvas.drawRoundRect(rectFPB, roundX, roundY, paintBar);
}
} else {
canvas.drawRect(rectFBG, paintBackGround);
canvas.drawRect(rectFPB, paintBar);
}
}
}
直接替换上面这两个方法就行了 如果你有解决上面那两个方法的问题,也希望提出意见,例如我想图片到达终点之后,让它不动,只让进度条动,但是觉得这样设计不是很好,欢迎提出意见,哈哈