Android自定义seekbar:无法在OnDraw方法中获得拇指的准确位置
我试图通过扩展AppCompatSeekBar来实现自定义SeekBar。我的意图是在拇指上画一些元素。我试图得到这个拇指的界限,但在第一次抽签时它是不准确的 以下是我对onDraw方法的操作:Android自定义seekbar:无法在OnDraw方法中获得拇指的准确位置,android,android-custom-view,android-seekbar,Android,Android Custom View,Android Seekbar,我试图通过扩展AppCompatSeekBar来实现自定义SeekBar。我的意图是在拇指上画一些元素。我试图得到这个拇指的界限,但在第一次抽签时它是不准确的 以下是我对onDraw方法的操作: protected void onDraw(Canvas canvas) { /*Rotates the seekbar, as I need it to be vertical*/ canvas.rotate(-90); canvas.translate(-getHeight(
protected void onDraw(Canvas canvas) {
/*Rotates the seekbar, as I need it to be vertical*/
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
super.onDraw(canvas);
canvas.save();
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(),R.color.accent));
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
Rect thumbBounds = this.getThumb().getBounds();
int offset = getThumbOffset();
Log.d(TAG,"ThumbOffset:"+offset+ " RectX:" + thumbBounds.centerX());
thumbBounds.offset(offset,0);
canvas.drawRect(thumbBounds, paint);
canvas.drawText(Integer.toString(getProgress()),thumbBounds.centerY(),-thumbBounds.centerX(),textPaint);
canvas.restore();
}
//Calculating the size of the text
text = String.valueOf(getProgress() - maxValue);
textPaint.getTextBounds(text,0,text.length(),textBounds);
//The height of the displacement area of our bar (the height - the padding - the thumb size)
int bottomPadding = getPaddingLeft() - getThumbOffset();
int topPadding = getPaddingRight() - getThumbOffset();
int height = getHeight() - bottomPadding - topPadding;
float progressRatio = (float) getProgress() / getMax();
float thumbOffset = getThumb().getIntrinsicWidth() * (.5f - progressRatio);
// In our rotated canvas, positive x is higher, positive Y is more to the left
float thumbX = progressRatio * height + bottomPadding + thumbOffset;
float thumbY = getWidth() / 2f;
//Since we rotate the canvas again when drawing the text, we need to translate the coords for the text
float textX = thumbY - textBounds.width() / 2f;
float textY = -(thumbX - textBounds.height() / 2f);
第一次加载时,看起来是这样的(不正确)在我拖动拇指后,看起来像这样(正确):
此外,如果我将活动最小化几次,边界就会变得疯狂:
因此,我假设我在动画中得到了几帧的拇指边界。我的问题是,我如何补偿这一点
谢谢虽然我找不到一种直接从拇指获得位置的好方法,但我找到了一种间接获得位置的好方法。我只需要调整垂直条的测量值。将此代码放入onDraw方法中:
protected void onDraw(Canvas canvas) {
/*Rotates the seekbar, as I need it to be vertical*/
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
super.onDraw(canvas);
canvas.save();
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(),R.color.accent));
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
Rect thumbBounds = this.getThumb().getBounds();
int offset = getThumbOffset();
Log.d(TAG,"ThumbOffset:"+offset+ " RectX:" + thumbBounds.centerX());
thumbBounds.offset(offset,0);
canvas.drawRect(thumbBounds, paint);
canvas.drawText(Integer.toString(getProgress()),thumbBounds.centerY(),-thumbBounds.centerX(),textPaint);
canvas.restore();
}
//Calculating the size of the text
text = String.valueOf(getProgress() - maxValue);
textPaint.getTextBounds(text,0,text.length(),textBounds);
//The height of the displacement area of our bar (the height - the padding - the thumb size)
int bottomPadding = getPaddingLeft() - getThumbOffset();
int topPadding = getPaddingRight() - getThumbOffset();
int height = getHeight() - bottomPadding - topPadding;
float progressRatio = (float) getProgress() / getMax();
float thumbOffset = getThumb().getIntrinsicWidth() * (.5f - progressRatio);
// In our rotated canvas, positive x is higher, positive Y is more to the left
float thumbX = progressRatio * height + bottomPadding + thumbOffset;
float thumbY = getWidth() / 2f;
//Since we rotate the canvas again when drawing the text, we need to translate the coords for the text
float textX = thumbY - textBounds.width() / 2f;
float textY = -(thumbX - textBounds.height() / 2f);
虽然我找不到一种直接从拇指得到位置的好方法,但我找到了一种间接得到位置的好方法。我只需要调整垂直条的测量值。将此代码放入onDraw方法中:
protected void onDraw(Canvas canvas) {
/*Rotates the seekbar, as I need it to be vertical*/
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
super.onDraw(canvas);
canvas.save();
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(),R.color.accent));
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
Rect thumbBounds = this.getThumb().getBounds();
int offset = getThumbOffset();
Log.d(TAG,"ThumbOffset:"+offset+ " RectX:" + thumbBounds.centerX());
thumbBounds.offset(offset,0);
canvas.drawRect(thumbBounds, paint);
canvas.drawText(Integer.toString(getProgress()),thumbBounds.centerY(),-thumbBounds.centerX(),textPaint);
canvas.restore();
}
//Calculating the size of the text
text = String.valueOf(getProgress() - maxValue);
textPaint.getTextBounds(text,0,text.length(),textBounds);
//The height of the displacement area of our bar (the height - the padding - the thumb size)
int bottomPadding = getPaddingLeft() - getThumbOffset();
int topPadding = getPaddingRight() - getThumbOffset();
int height = getHeight() - bottomPadding - topPadding;
float progressRatio = (float) getProgress() / getMax();
float thumbOffset = getThumb().getIntrinsicWidth() * (.5f - progressRatio);
// In our rotated canvas, positive x is higher, positive Y is more to the left
float thumbX = progressRatio * height + bottomPadding + thumbOffset;
float thumbY = getWidth() / 2f;
//Since we rotate the canvas again when drawing the text, we need to translate the coords for the text
float textX = thumbY - textBounds.width() / 2f;
float textY = -(thumbX - textBounds.height() / 2f);