Как добавить цвет фона для StaticLayout в Android

#android #staticlayout

#Android #staticlayout

Вопрос:

Я хочу добавить цвет фона для приведенного ниже StaticLayout background color в качестве transperentblock для четкого отображения текста может ли кто-нибудь помочь мне, как я могу этого добиться

Код

  public static Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
        // prepare canvas
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);
        TextPaint paint = new TextPaint(Paint.LINEAR_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(false);
        paint.setFilterBitmap(false);
        paint.setDither(true);
        paint.setColor(Color.rgb(255, 255, 255));
        paint.setFakeBoldText(true);
        paint.setTextSize(8);
        int textWidth = canvas.getWidth() - (int) (16 * scale);
        // init StaticLayout for text
        StaticLayout textLayout = new StaticLayout(gText, paint,
                canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                false);
        // get height of multiline text
        int textHeight = textLayout.getHeight();
        // get position of text's top left corner
        float x = (bitmap.getWidth() - textWidth);
        float y = (bitmap.getHeight() - textHeight);
        // draw text to the Canvas Left
        canvas.save();
        //canvas.translate(x, y);
        canvas.translate((canvas.getWidth() / 2) - (textLayout.getWidth() / 2), y);
        textLayout.draw(canvas);
        canvas.restore();
        return bitmap;
    }
 

Комментарии:

1. итак, вы хотите прозрачный цвет, верно? тогда почему вы используете rgb

Ответ №1:

 public static int argb (int alpha, 
                int red, 
                int green, 
                int blue)
 

Возвращает значение color-int из компонентов alpha, red, green, blue. Эти значения компонентов должны быть, но проверка диапазона не выполняется, поэтому, если они находятся вне диапазона, возвращаемый цвет не определен.

поэтому используйте Color.argb(0,0,0,0)

Полностью прозрачный — «#00000000»