如何在SeekBar thumb中显示Android SeekBar progressChanged值?

时间:2016-10-02 17:22:42

标签: android bitmapdrawable gradientdrawable

我想在Android SeekBar的拇指中显示当前进度点的值。这是我到目前为止所尝试的:

SIGINT

然后当我得到Drawable d的类型时,它是一个Gradient Drawable。我研究过但我找不到将渐变Drawable转换为位图的方法。

1 个答案:

答案 0 :(得分:0)

Drawable d = ContextCompat.GetDrawable(this, Resource.Drawable.thumb);
        Canvas c = new Canvas();
        Bitmap bitmap = Bitmap.CreateBitmap(d.IntrinsicWidth, d.IntrinsicHeight, Bitmap.Config.Argb8888);
        c.SetBitmap(bitmap);
        d.SetBounds(0, 0, d.IntrinsicWidth, d.IntrinsicHeight);
        d.Draw(c);
        //Bitmap bmp = bitmap.Copy(Bitmap.Config.Argb8888, true);
        string text = Integer.ToString(progress) + "%";
        Paint p = new Paint();
        p.SetTypeface(Typeface.CreateFromAsset(Assets, "fonts/Brandon_bld.otf"));
        p.TextSize = 18;
        p.Color = Color.White;
        int width = (int)p.MeasureText(text);
        int yPos = (int)((c.Height / 2) - ((p.Descent() + p.Ascent()) / 2));
        c.DrawText(text, (bitmap.Width - width) / 2, yPos, p);
        progBar.SetThumb(new BitmapDrawable(Resources, bitmap));

使用画布创建位图工作