How to draw a bitmap with a given color set as transparent?
For example I want all white pixels to be transparent.
You need to set the Alpha value for the paint you're passing to the Bitmap.
http://developer.android.com/reference/android/graphics/Paint.html#setAlpha%28int%29
Values vary from 0-255
EDIT:
Paint p = new Paint();
//Set Blue Color
p.setColor(Color.WHITE);
//Set transparency roughly at 50%
p.setAlpha(125);
you need to check every pixel of image and change its color.
you will get your answer in this Post
Another approach is drawing in trasparent color on your canvas (drawing holes).
The bitmapu needs alpha channel.
//Set transparent paint - you need all of these three
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
paint.setColor(Color.TRANSPARENT);
// Do you wanna soften?
// Set paint transparency:
// 0 = transparent ink, no visible effect
// 255 = full ink, hole in the bitmap
p.setAlpha(192);
// Do you want some blur?
// Set blur radius in pixel
paint.setMaskFilter(new BlurMaskFilter(10, Blur.NORMAL));
Related
I wanted to apply gray scale effect to a bitmap using brush strokes, i.e. user can use brush to apply gray scale effect to particlar area of bitmap as shown below:
I displayed bitmap on a view using canvas.drawBitmap(...) method. I learnt to draw brush strokes on bitmap using path and paint instances, I just want to apply Gray Scale effect to bitmap on which brush strokes are drawn.
my Paint object is defined as :
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(this.paintStyle);
paint.setStrokeWidth(this.paintStrokeWidth);
paint.setStrokeCap(this.lineCap);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.MITER);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
paint.setAlpha(255);
But it doesn't draw any strokes. If I remove paint.setColorFilter(...) and instead use paint.setColor(...), then i can see strokes on bitmap.
I tried using paint.setXferMode(...) but it didnt work.
In my view's onDraw() i used
canvas.drawPath(path, paint);
Can somebody please point me to the right direction? Thanks in advance!
I am using following code to apply font color when user click on perticular color like RED.
mPaint.setColor(getResources().getColor(R.color.color2));
And color2 in color.xml file is
<color name="color2">#FF3C00</color>
Now I am facing problem while applying following color.
I using canvas to draw paint on touching it in my application and i want to draw something like attached screen on canvas. I am able to draw it but It looks like solid color (I mean full circle but not dot dot inside)
Please help me to find this.
You can use BitmapShader for achieve that..
Here is sample code.. Try this code, I hope it will work..
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.shader);
//Initialize the BitmapShader with the Bitmap object and set the texture tile mode
BitmapShader mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
fillPaint.setStyle(Paint.Style.FILL);
//Assign the 'fillBMPshader' to this paint
fillPaint.setShader(mBitmapShader);
//Draw the fill of any shape you want, using the paint object.
canvas.drawCircle(posX, posY, 100, fillPaint);
I would like to draw bitmap(with specified color) on canvas.
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
// create bitmap
canvas.drawBitmap(bitmap, 0, 0, paint);
Well, the bitmap is visible on the canvas, but color of drawable didn't change.
Where is the problem?
I would like to draw bitmap(with specified color) on canvas.
A bitmap contains an image and drawing an image in single color doesn't make any sense. What do you expect it to do? Draw a red rectangle? Shapes can be drawn with color, not images...
The Color attribute of your Paint will be ignored. That Paint parameter is used to pass other settings such as anti-aliasing.
I hope this clarifies.
paint.setColor(Color.RED) is irrelevant. If your image comes with an alpha channel and you want it to be drawn in a single color, use ColorFilter instead:
paint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
Is it possible to draw unfilled figures on Android? By default circles and rectangles are filled.
You need to change the Paint style to stroke if you just want an outline with no fill:
Paint p = new Paint();
p.setStyle(Paint.Style.STROKE);
I'm developing an android app where i need to capture text and save it as a transparent image. Capturing the text has been done but making a transparent png file is where i'm stuck as i'm not familiar with image pixel manipulation at all. Here's what I have so far... i first create a blank bitmap and fill it with a white background, then i set the paint's transparency to 0 (full transparency) and then draw the source bitmap into the destination bitmap using the XOR modes.. but when i run the app all i see is a blank white image. i'll be glad if someone points out what i'm doing wrong and how to fix it. Thanks in advance.
b = Bitmap.createBitmap(tw, th,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Rect dest = new Rect(0,0,b.getWidth(),b.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, b.getWidth(), b.getHeight(), paint);
paint.setAlpha(0);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(bmp,null,dest,paint);
Have you looked at : How to change a bitmap's opacity?
Seems like
paint.setAlpha(0);
won't do anything as you need to set the alpha channel to something greater than 0...
Use:
Color.argb(0,0,0,0)
The first parameter is the alpha. Set it to 0 for complete transparency.