Android layout - how to draw a hole in a rectangle - java

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.

Related

JavaFX: Setting a border within a rectangle to keep the width and height

So, I have a grid of rectangles in my scene and I want to give it a border to visually seperate it to the others. I am currently using grid.setGridLinesVisible(true); and that works fine, but I am sure this isn't supposed to be used for that.
I tried setting a border with nodes[j][i].setStroke(Color.BLACK); and this works too, but now my whole grid is getting a lot bigger because it's drawing the border on the outside of the rectangle and therefore resizing it.
Is there a way to draw a border/stroke inside of a rectangle to keep the transformation?
I already looked up the documentation but there isn't such a function.
Thanks for the help!
You can use setStrokeType for your rectangles to draw the border inside using StrokeType.Inside:
nodes[j][i].setStrokeType(StrokeType.Inside);

how to visualize a rectangle

I have two rectangles in Libgdx but they seem not to overlap right. Is there a way so I see where the rectangles are?
I have this:
Body2.setSize(72, 72);
Body2.setPosition(20, squiddyY);
Body.setHeight(Assets.sprite_body.getHeight());
Body.setPosition(pipe1X, Assets.sprite_body.getY()+Assets.sprite_body.getHeight());
if(Body2.overlaps(Body)){
squiddyY=1000;
}
but it looks like body is half size of sprite_body(or the Y is just too low?) and is a bit behind it (the X location of sprite_body is pipe1X). Maybe i'm putting it at the wrong place so visualizing it will really help.
You can draw shapes with ShapeRenderer
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html
The link includes examples.

svg brightness/transparency effects and filters (Java Android)

I'm using SVG files in my Game and i want to add transparency and brightness effects to them. So what i want is that a sprite becomes more and more transparent until it's invisible. This looks much smoother than a just disappearing sprite. This allows also smooth transitions between the levels where a white screen gets less transparent and then again more. I hope you understand what i mean. Besides this a brightness effect would be nice, too. A sprite can get brighter or darker during the game. Nevertheless i could create a workaround for this using the transparency effect, but yeah, i have none :/
I use this library:
https://androidsvg.googlecode.com/hg/doc/reference/packages.html
I render my svgs on this way:
svg.renderToCanvas(pCanvas);
However i can also convert it to pictures:
Picture pic = svg.renderToPicture();
Maybe this information will help you.
Does there exist something what could help me? And if not, have you an idea how can i solve the problem?
EDIT:So iwant my SVGs behave like these ones:
http://scratch.mit.edu/projects/24634091/
You could use an ImageView and setAlpha(), as Frank says. Or it may just be easier to render the sprites to the canvas in your draw() method.
If you are writing a game, then you may find that rendering SVGs in your game loop may not be fast enough. So you might have to pre-render all your SVG sprites to Bitmaps (ie on startup) and draw them to your Canvas.
Paint p = new Paint();
p.setAlpha(alpha);
canvas.drawBitmap(sprite, x,y, p);

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

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);

Java - drawing a scaled up shape with line thickness of 1

When you use Graphics2D.scale(); and draw a shape, the outline thickness is also scaled.
Is there a way to draw it without the line thickness being scaled? Perhaps there's another efficient way to scale it other than using the above function?
This question is similar. It looks like you have to mess around with a Stroke object to set the right line width.
You're going to have to save your drawing as a list of line vectors, and scale and render the drawing at various sizes to maintain the line thickness you want.
I've just found a solution to my own question. I've no idea how efficient it is but it works as intended:
Area area = new Area(myShape); //myShape is the shape I want to scale
AffineTransform at = new AffineTransform();
at.scale(2,2);
area = area.createTransformedArea(at);
graphics2d.draw(area); //graphics2d is the Graphics2D instance to do the drawing
Perhaps someone could enlighten me as to whether or not this is a good approach to take?

Categories