如何在Android中使用TextView创建自定义SeekBar?

时间:2014-03-22 12:46:16

标签: android android-xml android-custom-view

enter image description here

enter image description here

如何在Android中使用TextViewSeekBar创建可移动的ProgressBar

1 个答案:

答案 0 :(得分:3)

您可以创建一个包含文本的位图,而不是textview。像这样的东西

SeekBar timerBar = (SeekBar) findViewById(R.id.seekBarTimer);
if (timerBar != null) {
    timerBar.setMax((int) (Settings.countdownSeconds + 1));
    timerBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar timerBar, int arg1, boolean arg2) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seek_thumb);
            Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            Canvas c = new Canvas(bmp);
            String text = Integer.toString(timerBar.getProgress());
            Paint p = new Paint();
            p.setTypeface(Typeface.DEFAULT_BOLD);
            p.setTextSize(14);
            p.setColor(0xFFFFFFFF);
            int width = (int) p.measureText(text);
            int yPos = (int) ((c.getHeight() / 2) - ((p.descent() + p.ascent()) / 2));
            c.drawText(text, (bmp.getWidth()-width)/2, yPos, p);
            timerBar.setThumb(new BitmapDrawable(getResources(), bmp));
        }
    });
    timerBar.setProgress(0);
}