在运行时写入文本后,SeekBar上的Thumb大小会减小

时间:2014-06-17 09:49:37

标签: android android-seekbar

我已经实现了一个自定义SeekBar并设置了自定义拇指图像。当进度发生变化时,我会在拇指上显示它。最初,它正在正确显示。当我向前,向后滑动时,拇指的大小会减小。

以下是相同的代码。

XML组件

<SeekBar
                android:id="@+id/sb_timelimit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"
                android:max="60"
                android:maxHeight="6dp"
                android:minHeight="6dp"
                android:progress="10"
                android:progressDrawable="@drawable/timelimitprogress"
                android:thumb="@drawable/timecircle" />

图层列表

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@android:id/background"
    android:drawable="@drawable/greyprocessbar"/>
<item android:id="@android:id/progress">
    <scale
        android:drawable="@drawable/orangeprocessbar"
        android:scaleWidth="100%" />
</item>

Seekbar听众

new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            int value = seekBar.getProgress(); //this is the value of the progress bar (1-100)
            //value = progress; //this should also work
            String valueString = value + " sec"; //this is the string that will be put above the slider
            seekBar.setThumb(writeOnDrawable(R.drawable.timecircle, valueString));
        }
    });

private BitmapDrawable writeOnDrawable(int drawableId, String text){

    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId, options).copy(Bitmap.Config.ARGB_8888, true);

    if(paint == null){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    paint.setStyle(Style.FILL);  
    paint.setColor(getResources().getColor(R.color.grey_text)); //Change this if you want other color of text
    paint.setTextSize(30); //Change this if you want bigger/smaller font

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 10, bm.getHeight()/4, paint); //Change the position of the text here

    return new BitmapDrawable(bm);
}

参考截图

寻求改变之前

enter image description here

寻求改变后

enter image description here

任何人都知道如何解决此问题?提前谢谢。

1 个答案:

答案 0 :(得分:1)

通过更改以下行

解决了此问题
return new BitmapDrawable(bm);

return new BitmapDrawable(getResources(), bm);