Canvas drawText, how to rotate and how to change "anchor-point"? - java

Good evening,
It is about an android app.
I would like to use the method Canvas.drawText(...). But I don't know how I can rotate the text. I need the text at a certain position at a certain angle. Do you know how I can achieve this?
Next question, usually the point to which the position coordinates are refering is the lower left corner. I want to change this "anchor-point" to the lower center. Is that possible? The pivot-point of the rotation should be the same.
I guess simple questions, but I don't know how to do this. Thanks for help.

here is a basic example of drawing text on a path, to get it how you want you should mess with the path and the paint Path, Paint
Paint paint = new Paint();
Path path = new Path();
paint.setColor(Color.BLACK);
path.moveTo(canvas.getWidth()/2, 0);
path.lineTo(canvas.getWidth()/4, 400);
canvas.drawTextOnPath("text manipulated", path, 0, 0, paint);

Related

My sprite rotation doesn' t stay in place

So I want my sprite to rotate at my players position, it rotates, but appears some place to the left of the player. I cannot find anything on the internet that helped me with this.
Here's my code
at.setToRotation(Math.PI / 2, Main.p.x, Main.p.y / 2);
if (!rotate) {
g.setTransform(at);
rotate = true;
}
g.drawImage(item0, Main.p.x + 1, Main.p.y - 15, null);
Yes I know it's not the best code, I'm still a beginner, don't be too harsh please.
drawImage might use the sprites center to set it. Try adding half of the width of the sprite to x.
EDIT
Or maybe the setToRotation function uses the center point of the picture.

Android layout - how to draw a hole in a rectangle

I need to create a rounded rectangle with a semi-circular section cut out of the bottom of it - how do I go about this? Sorry for the extremely open ended question - I just have no idea where to start looking to figure it out.
Thanks
You can use PorterDuffXfermodes...
Paint clear = new Paint(Paint.ANTI_ALIAS_FLAG);
clear.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Then just draw with this paint.
And for the semi circular thing, check out canvas.drawArc() and pathes.

How to apply gradient background in andengine

I've been looking around for awhile now, and can't seem to find out how to set a scene's background as a gradient... it's hard to find solid Andengine-related answers,
I guess my options are:
using a sprite from a gradient image I've created myself (which can't be the best way)
using a gradient xml resource (but I don't know how to create a sprite from a resId, and I'm confused on how to make the gradient fit the camera)
or some other andengine built-in method
Any help is appreciated.
The following code inside your activity class (onCreateScene or onPopulateScene) should set a red/blue gradient as your background.
Gradient g = new Gradient(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, this.getVertexBufferObjectManager());
g.setGradient(Color.RED, Color.BLUE, 1, 0);
this.setBackground(new EntityBackground(g));

Curved sides of triangle path?

I am drawing a triangle on a Canvas, something like:
canvas.moveTo(0, 30);
canvas.lineTo(40, 0);
canvas.lineTo(40, 40);
canvas.lineTo(40, 40);
canvas.lineTo(0, 30);
And get proper triangle on my Canvas. But I need to curve the sides a little and fill that path with specific color. What is the easiest way to do this? Drawing arcs? But how to fill the object?
Thanks!
EDIT: I noticed you were using android's canvas, not HTML Canvas, sorry. The concept is exactly the same except you'll call quadTo() instead of quadraticCurveTo(), so my example should still get you going.
Also on android you use canvas.drawPath(path, paint) and pass in a paint that has its Paint.style set to FILL_AND_STROKE.
You will want to construct the path, fill() it, then stroke() it in order to get both a filled path with the stroke outline.
To get that particular shape, the easiest way is to draw two quadratic curves. A quadratic curve first needs a control point x,y then the end point x,y. The control point for both curves should be around the middle of your desired triangle. Here is an example:
ctx.fillStyle = "lightgray";
ctx.moveTo(0, 100);
ctx.quadraticCurveTo(50, 50, 50, 0);
ctx.quadraticCurveTo(50, 50, 100, 100);
ctx.lineTo(0, 100);
ctx.fill();
ctx.stroke();
Here is that example live for you.

Draw segments of a bitmap using Canvas

I have a picture (resource) that I would like to use for my application in Android. But I only want to draw specific segments of it. My initial thought is to turn it into a bitmap and specify which pixels need to be drawn and where. I tried canvas.drawBitmap(bitmap, src, dst, null); but it doesn't seem to work. Maybe I am not using it right.
Just wondering if it is possible at all, and what can I use to achieve this?
Thanks!
src = new Rect(20,40,20,40);
dst = new Rect(20,40,20,40);
canvas.drawBitmap(background, offset, 0, null);
canvas.drawBitmap(bitmap, src, dst, null);
I was hoping to see the area specified at src's coordinates to be drawn into the area specified by dst's coordinates, but I don't see anything, other than the background.
dst should be where you want to draw the image in the canvas, and the src should be the Rect you want crop from.
You might want to use a format that supports an alpha channel or load the bitmap as well as a greyscale image for the alpha channel, construct an image from both and draw that. Try Java's Graphics2D object. Here's an article that should get you started.

Categories