0

我想为下面的 staticLayout 背景颜色添加背景颜色作为透明块以清楚地显示文本可以有人帮助我如何实现这一点

代码

 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;
    }
4

1 回答 1

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

从 alpha、red、green、blue 分量返回一个 color-int。这些组件值应该是 ,但没有执行范围检查,因此如果它们超出范围,则返回的颜色是未定义的。

所以使用 Color.argb(0,0,0,0)

完全透明 - “#00000000”

于 2021-02-16T06:56:57.820 回答