In most of the android applications, the border of the circles are beautiful and the pixels in part of border of the circles are monotonic and beautiful. But when I draw a circle using the following code and when I see the result in my device, the pixels in part of border of the circle is like bitten apple.Such as when an ant has eaten around an apple.
My Code:
class Circle extends View {
Paint paint;
Circle(Context context) {
super(context);
paint = new Paint();
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawCircle(50,50,30,paint);
}
}
Turn on antialiasing to achieve it:
paint.setAntiAlias(true);
Please check this demo How to draw a circle in Android
set ANTI_ALIAS_FLAG flag true
For more info you may visit:
https://developer.android.com/reference/android/graphics/Canvas.html
Related
I'm using custom Drawable with animation to draw function diagram...
and I use PorterDuff.Mode.SRC_IN PorterDuffXfermode to draw its brightness bar (to clear the overlaped track)
with no overscroll effect, the brightness bar thumb is well drawn.
but if overscroll when animating, the thumb turn white.
pseudocode....
public class NotificationTileDrawable extends Drawable {
PorterDuffXfermode xfer = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
//...
#Override
public void draw(Canvas canvas) {
//draw other...
//draw track
canvas.drawRoundRect(...);
paint.setXfermode(xfer);
//draw thumb
canvas.drawRoundRect(...);
paint.setXfermode(null);
//draw other...
}
//other abstract methods...
}
how should i do to fix this? (without disable overscroll effect)
Thx
I am writing a custom renderer for Xamarin.Forms. I am overriding the class ImageRenderer to do some tweaks to the image.
However, I want to overlay some parts of the image with circles, so I override the method void OnDraw(Canvas canvas).
Then I modify the Canvas a bit by drawing some circles:
protected override void OnDraw(Canvas canvas)
{
var paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
foreach (var mapObject in _control.PointSource)
{
canvas.DrawCircle(mapObject.Location.X, mapObject.Location.Y, 100 / _scaleFactor, paint);
}
base.OnDraw(canvas);
}
However, everything is drawn on the background. The actual image is always on top. How can one draw circles on top of the image?
https://developer.xamarin.com/api/member/Android.Views.View.OnDraw/
In the docs they say the following:
the canvas on which the background will be drawn
So the current behaviour was expected.
I managed to fix it by moving the circle logic to the function: void Draw(Canvas canvas).
I have 3 ImageViews. 1st and 2nd connected with red line. Also I have simple button. Here is a picture:
I want to connect 2nd & 3rd ImageViews with new Path line and change the first line color (for example to Green) when i clicking my button. Here is parts of my code:
public class SkillPath extends View {
Paint paint;
Path path;
... constructors
#Override
protected void onDraw(Canvas canvas) {
addPath (canvas);
}
//Here is my RED line
void addPath (Canvas canvas){
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
path.moveTo(110, 110);
path.lineTo(210, 110);
canvas.drawPath(path, paint);
Log.d ("Page 2","onDraw");
}
I can get all coordinates of all Views, but how can I redraw existing canvas? I suspect, I need to use invalidate(), but I do not know enough to do this. Need help.
The invalidate() method forces the View to be re-drawn.
So just apply the modification you need on your canvas and call invalidate() on the related View after these modifications.
Actually my problem is I want to add text view to surface view at a particular position whether it may be corner or centre.In this I am getting surface view dynamically and I have to add text view to this surface view dynamically please help me
Another approach would be to write the text directly into the SurfaceView. To do this, you can create your custom implementation of SurfaceView by extending it. Then, rewrite the onDraw() method to draw your text on top of it.
Haven't tested to see if this works, but trye something like:
public class MySurfaceView extends SurfaceView {
Paint paint;
public MySurfaceView() {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(25);
setWillNotDraw(false);
}
public class onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Your Text", 10, 10, paint);
}
}
I would like to send each time different Drawable shape to view and then add it to my activity.
The problem is I can't find a way to add the drawable shape into the canvas when I'm overriding the onDraw method.
The design supposed to be reusable, I can draw rect first and then draw circle...
I want to find a way to send the view different shape. Is it possible?
#Override
protected void onDraw(Canvas canvas) {
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
// canvas.drawOval(ballBounds, paint);
canvas.drawRect(ballBounds, paint);
}
Although, your question needs more explanation on what you need, but with what I could understand is you want rect, or circle on desire, here it is,
Add, global variable int ShapeStructure = 0;
#Override
protected void onDraw(Canvas canvas)
{
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
if(ShapeStructure == 0){
canvas.drawOval(ballBounds, paint);
}else{
canvas.drawRect(ballBounds, paint);
}
}
Now that, you have a variable at your disposal, you can control what you want to paint.
Hope, this helps, let me know if you want something else.