2014-06-17 27 views
0

我已經實現了自定義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> 

搜索欄收聽

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

這個問題正在通過改變以下線

return new BitmapDrawable(bm); 

決心

return new BitmapDrawable(getResources(), bm);