How to add number scale on android SeekBar

六月ゝ 毕业季﹏ 提交于 2019-12-06 05:54:07

问题


I'm using the out of the box android SeekBar component. Below I would like to add the numbers form 1 to 5 showing the progress of the SeekBar. I have problem distributing the numbers correctly on the seek bar.

Just like the image below


回答1:


For drawing text over the seekbar thumb use this function

  public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
        Paint paint = new Paint(); 
        paint.setStyle(Style.FILL);  
        paint.setColor(Color.BLACK); 
        paint.setTextSize(20); 
        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }

This function will call as

seekbar.setThumb(writeOnDrawable(R.drawable.thumbimage, mytext)); put thumbimage.png file in res/drawable/ and 'mytext' is the string which you want to write on top of that drawable

For complete conversation see the below link

how to set the seek bar thumb with a layout or with a TextView?

Hope this will be helpful to you.



来源:https://stackoverflow.com/questions/15360591/how-to-add-number-scale-on-android-seekbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!