1

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

SeekBar progBar = (SeekBar)FindViewById(Resource.Id.seekBar1);
        progBar.ProgressChanged += (object sender, SeekBar.ProgressChangedEventArgs e) =>
        {
            if (e.FromUser)
            {
                Drawable d = ContextCompat.GetDrawable(this, Resource.Drawable.thumb);
                Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.thumb);
                Bitmap bmp = bitmap.Copy(Bitmap.Config.Argb8888, true);// this line gives me a System.NullReferenceException: Object reference not set to an instance of an object. error
                Canvas c = new Canvas(bmp);
                Canvas c = new Canvas(bmp);
                string text = Integer.ToString(progBar.Progress);
                Paint p = new Paint();
                p.SetTypeface(Typeface.DefaultBold);
                p.TextSize = 14;
                p.Color = Color.White;
                int width = (int)p.MeasureText(text);
                int yPos = (int)((c.Height / 2) - ((p.Descent() + p.Ascent()) / 2));
                c.DrawText(text, (bmp.Width - width) / 2, yPos, p);
                progBar.SetThumb(new BitmapDrawable(Resources, bmp));
            }
        };

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

4

1 回答 1

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));

使用画布创建位图工作

于 2016-10-03T16:35:06.873 回答