Search code examples
androidgraphicspaint

Drawing Trasparent Text on Bitmap in Android


Im trying to draw a string on top of an Image,The code works but the transparency is not obtained i have used several values for alpha,but does not work.

paint.setAlpha(alpha);

Can some one tell me what are the range of values for transparency or what im doing wrong here

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

Solution

  • See: http://developer.android.com/reference/android/graphics/Paint.html#setColor(int)

    The setColor will overwrite the alpha value you just set before that call. That should work:

    paint.setColor(Color.RED);
    paint.setAlpha(alpha);