I am trying to use Canvas.drawLine method to draw a polygon
Here's the code that I am using
Canvas canvas = new Canvas(cache);
Paint paint = new Paint();
paint.setStrokeWidth(16);
paint.setColor(this.currentDrawing.getColor());
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
systemCanvas.drawBitmap(cache, 0, 0, paint);
paint.setStrokeCap(Cap.ROOUND);
canvas.drawLine(from.getLeft(), from.getTop(), to.getLeft(), to.getTop(), paint);
And this is the output that I am getting:
Notice the way the lines render, they break on the round shapes and don't join smoothly. I understand why is it happening but I don't know how to make it smooth and consistent.
Any help is appreciated.
You may also want to do this
mPaint.setAntiAlias(true);
Please check this answer out because it is similar to your question:
Android How to draw a smooth line following your finger
Please let me know if this helps!
You need to activate hardware acceleration. If you don't activate this, you can not use method of antialias, cap, join and etc.
Related
I would like to fill each Rect with a small unique image. Is there a best way to do this? Right now they are drawn with using a Paint object.
I will answer my question for future reference then. Kotlin doesn't have the exact same solution, but you can draw bitmaps into rects similar to this way:
canvas.drawBitmap(image, Rect(0, 0, image!!.width, image!!.height), rect, null)
I have a bitmap object.
I have a Region object that represents a small portion of the bitmap;
I want to remove drawing from the bitmap object of that particular region and make that portion transparent..
How to do it? any help....
I am using android api-level 8..
You can simply make a pixel transparent by using mBitmap.setPixel (100,100,Color.TRANSPARENT);, so basic idea is to iterate over all the pixel to make it transparent, but if you have to iterate over too many pixels, it might be slow.
OR
You can use PorterDuffXferMode to make a portion transparent,
For an example create a paint object as mentioned below and pass it to the canvas:
Paint mPaint = new Paint();
mPaint.setXferMode(new PorterDuffXferMode(PorterDuff.Mode.CLEAR));
You can pass it to the canvas as described below:
Canvas c = new Canvas(mBitmap);
c.drawCircle(cx, cy, radius, paint);
It is for the circle but hope you will get the hint to do it for the custom region as per your need.
If still it is not working then you might have to disable Hardware Acceleration for that particular View. For more information, refer this Google DOC.
Hope this will give you some hint.
I want to draw something similar to this shape on android canvas:
I think the best way is to define this shape as the intersection of 2 circles and a line. What's the proper way to create this with android canvas?
UPDATE
I am now using PorterDuff as smith324 suggested:
*Note: code slightly simplified for clarity.
darkPaint = new Paint();
darkPaint.setColor(Color.rgb(50, 50, 50));
lightPaint = new Paint();
lightPaint.setColor(Color.rgb(200, 200, 200));
atopPaint= new Paint(GameDrawingPanel.darkPaint.getColor());
atopPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
atopPaint.setAntiAlias(true);
atopPaint.setStrokeWidth(2);
_boardCanvas.drawCircle(x, y, radius, darkPaint);
_boardCanvas.drawCircle(x, y, radius_small, lightPaint);
_boardCanvas.drawRect(0,0,height,width,atopPaint);
I forget exactly which modes would be of use here (XOR should work), but essentially you need to use a PorterDuff transfer mode to achieve this. Try drawing the concentric circles atop one another, then a dividing rectangle on one side.
http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html
http://www.svgopen.org/2005/papers/abstractsvgopen/
This is probably an easy one for Android experts. I am trying to draw into a Bitmap via a Canvas. I want exact colors, no anti-aliasing, and lines located at exact absolute coordinates within the Bitmap. Sometimes the lines are in the correct position and sometimes they are offset by 1 pixel. I have a feeling this has something to do with scaling. But I'm not sure. Here's the code:
Paint mPaint = new Paint();
mPaint.setColor(Paint.WHITE);
mPaint.setStrokeWidth(0);
mPaint.setAntiAlias(false);
mPaint.setDither(false);
mPaint.setStyle(Paint.Style.STROKE);
drawingContext.mycanvas.drawLine(20, 0, 10, 10, mPaint);
This actually draws a line from (19,0) to (10,9). Why?
I'll answer. I had to give up on this and write a Bresenham algorithm, setting pixels one by one. That works OK for what I'm doing. I suspect a drawLine bug when drawing left to right, bottom to top lines.
Hi all:
I'm writing a class that inherit from TextView, and override its onDraw() method, but in the method, my invoke of canvas.drawText() doesn't seems to work, the code just like below:
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.WHITE);
paint.setTextSize(20);
String text = "hello";
canvas.drawText(text, 0, 0, paint);
}
It isn't drawing anything because the text coordinates are bottom left. Since you're trying to draw on 0,0, it will draw above the screen.
Try changing the last line to:
canvas.drawText(text, 0, 20, paint);
Excellent suggestions all around, great job guys really. Next time though it would be nice if you ask the guy in a comment or something whether or not he's tried the completely obvious before posting it as an answer. Do you really think that the second he got to a point that wasn't working he just came straight to Stack Overflow without experimenting?
I do have an alternate suggestion, that crazily enough is based on the entire question and not just the part that could be answered without much actual knowledge.
I would recommend trying your drawText call on a Canvas that's not in a TextView subclass as that way it won't be overridden by the several hundred lines of code in TextView that manage it's drawable state.