There are many posts how to draw a dotted/dashed line or draw a border around circle. but I cannot find any information how to fill it. For example with DashPathEffect one can draw a stroke.
Is there any simple tool android provides similar to JS createPattern() ?
There is BitmapShader, which will allow you to create patterns. You can use it like this:
Paint paint = new Paint();
Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
paint.setShader(shader);
Then you use paint to draw on the canvas, and it will be filled with the specified pattern from the bitmap.
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 trying to create a Black screen with a transparent Hole in the middle of the screen. Here is what i have tried.
#Override
public void draw(Canvas canvas)
{
Paint myPaint = new Paint();
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint = new Paint();
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
}
The second paint, shows black color instead of transparent. How can i punch a transparent hole in MY SemiBlack Canvas?
you didn't save the canvas, try the code below
Paint myPaint = new Paint();
int sc = canvas.saveLayer(mBlackRect.left, mBlackRect.top,
mBlackRect.right, mBlackRect.bottom, myPaint,
Canvas.ALL_SAVE_FLAG);
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
myPaint.setXfermode(null);
canvas.restoreToCount(sc);
You can not really "punch" a hole by "removing pixels" from something already drawn, at least not with a hardware layer. And if you use a software layer, it will be bad for performance.
What you want to do is draw your shape with an alpha mask applied to your paint. A mask will prevent some parts of the shape to be drawn on the canvas, like cutting a piece of paper and stick it on a wall before spreading the painting.
To apply an alpha mask to your paint, you first need to create a bitmap containing the "hole" shape (programmatically or by loading a custom image from resources), then create a BitmapShader from this bitmap with the proper Xfermode (depending if you want the transparent part in your mask bitmap to be cut out or the non-transparent part) and finally apply this shader to your paint before drawing the semitransparent rectangle or anything you want.
Be careful with performance: only create the Paint object once (do not allocate any object in onDraw() because this method gets called up to 60 times per second on the UI thread), and recreate the alpha mask bitmap only when the bounds of your View/Drawable change (if its dimensions depend on the View dimensions of course, otherwise you just create it once).
I'm sorry if I don't have time to give you ready-to-use code but I think you should find plenty of information about the technique I just described and you can start experimenting and figuring out the solution by yourself which is more rewarding I think ;)
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 have a bitmap that spans the whole screen that will function as texture for a Path object that I need to draw to my canvas. I then have a background image that this textured path needs to be drawn on top of.
I tried using the PorterDuff modes, but nothing seemed to work correctly. I was having a hard time figuring out exactly how the PorterDuff modes act, because none of them seem to act the way I always thought they were supposed to function.
I've figured out a way to texture the path with this test code:
Paint paint = new Paint();
//draw the texture
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.texture),0,0,paint);
//construct the Path with an inverse even-odd fill
Path p = new Path();
p.setFillType(Path.FillType.INVERSE_EVEN_ODD);
p.addCircle(this.getHeight()/2, this.getWidth()/2, 200, Path.Direction.CCW);
//use CLEAR to remove inverted fill, thus showing only the originally filled section
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
//draw path
canvas.drawPath(p, paint);
But I can't figure out how to then place that on top of a background image. Am I just using the PorterDuff modes wrong?
Maybe my questions could lead you to your solution:
The paint used in your drawPath call:
What is the paint style?
What is the paint stroke width?
What is the paint stroke/fill color?
If you are not using a stoke/fill color, but a texture instead, where is the call to the paint's setShader (using a BitmapShader)?
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);