1

我试图在图像上绘制一个字符串,代码有效,但未获得透明度我使用了多个 alpha 值,但不起作用。

paint.setAlpha(alpha);

有人可以告诉我透明度的值范围是多少,或者我在这里做错了什么

public static Bitmap drawtext(Bitmap src, String txt,int alpha) {
        int w = src.getWidth();
        int h = src.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(src, 0, 0, null);
        Paint paint = new Paint();
        paint.setAlpha(alpha);
        paint.setColor(Color.RED);
        paint.setTextSize(18);
        paint.setAntiAlias(true);
        paint.setUnderlineText(true);
        canvas.drawText(txt, 20, 25, paint);

        return result;
    }
4

2 回答 2

4

请参阅:http: //developer.android.com/reference/android/graphics/Paint.html#setColor(int)

setColor 将覆盖您在该调用之前设置的 alpha 值。那应该工作:

paint.setColor(Color.RED);
paint.setAlpha(alpha);
于 2013-07-05T17:43:03.990 回答
0

试试这个代码或在这里下载演示

  public Bitmap drawText(String text, int textWidth, int color) {

            // Get text dimensions
            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            textPaint.setStyle(Paint.Style.FILL);
            textPaint.setColor(Color.parseColor("#ff00ff"));
            textPaint.setTextSize(30);

            StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

            // Create bitmap and canvas to draw to
            Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
            Canvas c = new Canvas(b);

            // Draw background
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(color);
            c.drawPaint(paint);

            // Draw text
            c.save();
            c.translate(0, 0);
            mTextLayout.draw(c);
            c.restore();

            return b;
        }

将返回的图像设置为 imageview

//background transparent

        int colorT=Color.TRANSPARENT;
        Bitmap img1=drawText(text,width,colorT);
        img2.setImageBitmap(img1);
于 2017-02-01T05:21:31.720 回答