Android canvas circle color blur - android

I want to draw a circle (it is more but I have problems with the circle) on a canvas with the View Class.
I have started with this link: http://mindtherobot.com/blog/272/
The onMeasure() function is the same and I am scaling the same. In my emulator and on my Nexus 7 everything looks perfect but on my LG it isn´t
It looks loke the color of the circle is blurred.
The code is:
protected void onDraw(Canvas canvas) {
float scale = (float) getWidth();
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(scale, scale);
drawCircle(canvas);
canvas.restore();
}
private void drawCircle(Canvas canvas) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.drawOval(rimRect, debug);
canvas.restore();
}
private void initDrawingTools() {
float O1x = -0.5f;
float O1y = -0.5f;
float O2x = 0.5f;
float O2y = 0.5f;
rimRect = new RectF(O1x, O1y, O2x, O2y);
debug = new Paint();
debug.setColor(Color.RED);
}

use AntiAlias property for Paint. it protect the image from blurring and gives smoothing edges
debug = new Paint();
debug.setAntiAlias(true);
debug.setColor(Color.RED);

Related

emoji can't rotate with canvas when text include emoji

I am rotating and scaling canvas using canvas.setMatrix() and then drawing text on canvas using canvas.drawText();. That's working as expected. But when I try to draw text that contains emojis (android device's default emojis), they don't get rotated. Everything else in the text gets rotated as expected. Also drawing and scaling works as expected for emojis, it's just the rotating that doesn't work,and I find it only appears in android7.0 or below 7.0.
public void draw(Canvas canvas) {
if (TextUtils.isEmpty(mText)) {
drawCursor(canvas);
return;
}
float allTextHeight = mTextLineHeight * mLines.length;
float y = (mHeight - allTextHeight) / 2;
float offset = mPaint.getFontMetrics().ascent;
canvas.save();
canvas.translate(mWidth / 2, 0);
float initY = y;
for (String line : mLines) {
drawText(canvas, line, 0f, y - offset, mPaint, initY);
y += mTextLineHeight;
}
canvas.restore();
}

How to draw an arrow with a circle trajectory with animation on canvas android?

guys
I try to implement an black arrow which is on head of arc. The arrow animates with the arc, and has circle trajectory. I already implement the red arc with animation based on different values.
Please see the attachment.
How can I implement the black arrow on top of red arc? since if I do the same way as red arc animation, the black arrow will print the trajectory which is not desired.
Thanks in advance!
Leon
all you need is Canvas.
Here is example.
This is the your draw class:
public class CircleView extends View
{
public CircleView(Context context) {
super(context);
path = new Path();
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float angle = 270;
float radius = canvas.getWidth()/3;
float x = canvas.getWidth()/2;
float y = canvas.getHeight()/2;
final RectF oval = new RectF();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(25);
oval.set(x - radius, y - radius, x + radius,y + radius);
// Draw circle
paint.setColor(Color.RED);
canvas.drawArc(oval, 135, angle, false, paint);
paint.setColor(Color.BLUE);
canvas.drawArc(oval, angle, 405-angle, false, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
float l = 1.2f;
float a = angle*(float)Math.PI/180;
// Draw arrow
path.moveTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius);
path.lineTo(x+ (float)Math.cos(a+0.1) *radius*l, y + (float)Math.sin(a+0.1) * radius*l);
path.lineTo(x+ (float)Math.cos(a-0.1) *radius*l, y + (float)Math.sin(a-0.1) * radius*l);
path.lineTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius);
canvas.drawPath(path, paint);
}
private Path path;
private Paint paint;
}
This is activity for it:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CircleView(this));
}
}
For angle == 200 you will see image like this:
Working copy for IntelliJ Idea: https://github.com/weerf/android_circle

Canvas Rotation

Can someone explain exactly how rotation of the canvas about a point works ?
I have lines and I want to draw text parallel to each line. I have worked out the trigonometry required to calculate the angle of the line and it`s centre point.
When I try rotate the canvas about the start point of the line, then draw the text and restore, I am always getting strange offsets which obviously means I do not quiet get how the rotation is working ...
Can someone explain what happens when you rotate the canvas about a point and how the then drawing on co-ords X,Y translates back ?
see the following class:
class V extends View {
private Paint mPaint;
public V(Context context) {
super(context);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(0xffeeeeee);
mPaint.setTextSize(24);
}
#Override
protected void onDraw(Canvas canvas) {
float x = 100;
float y = 50;
float dx = 60;
float dy = 40;
canvas.drawLine(x, y, x + dx, y + dy, mPaint);
canvas.save();
float degrees = (float) (180 * Math.atan2(dy, dx) / Math.PI);
canvas.rotate(degrees, x, y);
canvas.drawText("text", x, y, mPaint);
canvas.restore();
}
}
now i hope everything should be clear how canvas rotation works...

How to draw ellipse's arc on android?

I want to draw an arc of ellipse in my android app.
I used this code:
#Override
protected void onDraw(Canvas canvas) {
canvas.save();
angle=45; //angle of ellipse major axis with X-axis.
startAngle=0; //start angle of arc
sweepAngle=90; //sweep angle of arc
a = 200; //major axis of ellipse
b = 100; //minor axis of ellipse
canvas.rotate(angle, center.x, center.y);
//draw the arc
canvas.drawArc(rect, startAngle - angle, sweepAngle, true, paintLine);
paintLine.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 0));
canvas.drawOval(rect, paintLine);
canvas.restore();
paintLine.setPathEffect(null);
}
I receive this shape:
The arc I need should start and end at the red point at this image:
Please tell me what mistake did I make.
Thanks.
When android draws an elliptic arc, it is as if it does so by first drawing a circular arc over the supplied sweep angle. Then it scales this circular arc so that it fits the specified ellipse. The the start and sweep-angles of the ellipse will be different from the start and sweep-angles specified. To get a wanted-angle, one must specify an actual-angle as tan(actual-angle) = (a/b)*tan(wanted-angle)
public class Main3Activity extends Activity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = new View(this) {
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
float angle=45; //angle of ellipse major axis with X-axis.
// These are the wanted angles
float startAngle_ = (float)Math.toRadians(315); //start angle of arc
float endAngle_ = (float)Math.toRadians(45); //end angle of arc
float a = 200; //major axis of ellipse
float b = 100; //minor axis of ellipse
float xc = getWidth()/2f;
float yc = getHeight()/2f;
// These are the angles to use
float startAngle = (float)Math.toDegrees(
Math.atan2(a*Math.sin(startAngle_),b*Math.cos(startAngle_)));
float endAngle = (float)Math.toDegrees(
Math.atan2(a*Math.sin(endAngle_),b*Math.cos(endAngle_)));
float sweepAngle = endAngle - startAngle;
RectF rect = new RectF(xc-a,yc-b,xc+a,yc+b);
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setStyle(Paint.Style.STROKE);
canvas.rotate(angle,xc,yc);
//draw the arc
canvas.drawArc(rect, startAngle, sweepAngle, true, paintLine);
paintLine.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 0));
canvas.drawOval(rect, paintLine);
canvas.restore();
paintLine.setPathEffect(null);
}
};
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
addContentView(view,layout);
}
This question is nearly seven years old! About time to update the documentation?

autozoom canvas to fit bitmap

In my custom views OnDraw method I draw a Bitmap to the center of the Canvas with
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect r = canvas.getClipBounds();
displayWidth = r.right;
displayHeight = r.bottom;
camera.applyToCanvas(canvas);
float zW = (float)bitmapWidth / (float)displayWidth;
float zH = (float)bitmapHeight / (float)displayHeight;
float z = 0.0f;
if (zW>1 || zH>1) {
z = Math.max(zW, zH);
}
canvas.drawColor(Color.DKGRAY);
canvas.drawBitmap(bitmap, (displayWidth/2.0f - (bitmapWidth)/2.0f), (displayHeight/2.0f - bitmapHeight/2.0f), paint);
if (z>0) {
camera.translate(z, -z, z);
}
}
If the Bitmap is larger in height or width is larger then Canvas size (displayWidth, displayHeight), how can I use the Camera class to autozoom to fit the Bitmap and center it to the Canvas. Any ideas?
Try to create a Matrix instance and initialize it using
public boolean setRectToRect (RectF src, RectF dst, Matrix.ScaleToFit stf)
You need to do this only once and keep the matrix in memory. Note that Matrix.ScaleToFit define the CENTER value.
Later when you draw the bitmap use this version of the drawBitmap:
public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)

Categories

Resources