I need to draw a custom shape like the following.
I am trying to draw this on a Canvas for a custom view that i am trying to make. The custom view will represent a fuel level indicator.
Any ideas as to how i can achieve this drawing?
Draw what you are asking using Path, using moveTo(float x, float y) ,lineTo (float x, float y) and close() methods.
The clip the canvas to that path ( using clipPath (Path path) method of the Canvas ).
Then to make some of it red, just draw a red rectangle of appropriate height and length.
Related
I have a 3x3 game board and I need to draw a path from check to check.
game board:
Using below functions I can draw path with center point of checks.
Canvas function:
drawPath(Path path, Paint paint)
Path function:
lineTo(float x, float y)
draw path like:
However, path are overlapped if path passes through a point over one time.
like this(left: current, right: expected), overlapped at corner:
and this(left: current, right: expected), overlapping lines and corner:
Is there a library or technique to due with this kind of problem(draw the right non-overlapped path)?
I have custom View that should paint a bitmap. I've created a simple line(bmp 1x50 px) to check where will be drawn exactly. In my onDraw(Canvas c) I'm using this line of code:
canvas.drawBitmap(bitmap, 0, 0, paint);
Where paint is simple Paint object, without any fancy settings. The problem is that this line shows at the middle of the view, not on top, as I thought it would be.
I'm setting View properties in layout xml:
<com.example.CustomView
android:id="#+id/costom_view"
android:layout_width="230px"
android:layout_height="188px"
android:layout_marginTop="10px"
custom:srcDraw="#drawable/menu_indicators_level_draw_up" />
This custom property is the ID of the bitmap I use to draw on canvas.
Why won't the View behave properly? And why does my canvas have a size if 800x450 (size of the screen i suppose) and not 230x188 like I defined in the layout?
And very important thing, I want to multiply the same bitmap on this view many times. At the end of work I want to draw this line from bottom to top of my View.
i think what you should use to draw the line above the bitmap is the follwing :
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
and give it the right coordinates to be drawn on top of the bitmap .
Hope that helps .
Imagine that I have a rectangle image. How could I create a style like the next one?
I mean, cropping the image into a circle, add the border, the shadow and the gross /shine effect. Until now, I only have tried this snippet code to crop the image: Cropping circular area from bitmap in Android but just that. I have no idea how to do the remaining components in Android.
An easy way to achieve this effect is to use Canvas.drawCircle() and a BitmapShader:
BitmapShader s = new BitmapShader(myPhoto, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setShader(s);
myCanvas.drawCircle(centerX, centerY, radius, p);
To do the shadow, simply call Paint.setShadowLayer() on the paint (this will only work if you draw the effect into an offscreen Bitmap or if your View uses a software layer – set by calling View.setLayerType() –).
The border can be drawn by drawing another circle on top, using the Paint.Style.STROKE style (that you can set by calling Paint.setStyle()).
Finally you can draw the gloss by drawing a circle, oval or Path on top of your very first circle. You'll need to use a LinearGradient shader on your paint and you'll also need to clip the gloss. You can do this in two ways:
If you are drawing the entire effect into a Bitmap, which is what I would recommend, simply set the paint's Xfermode to a new PorterDuffXfermode(PorterDuff.Mode.SRC_IN).
If you are drawing the effect directly on screen you can simply use Canvas.clipPath() to set a circular clip. Note that this will work with hardware acceleration only as of Android 4.3.
My goal right now is create a bitmap that is a non-rectangular shape, that I can also move. I have created a path that I can use as with canvas's clipPath method. Is it possible to move that clipPath around?
Also, am I doing this the best way, or is there a better way to accomplish this?
Here's my draw function:
public void draw(Canvas c){
// Paint object, for outline of clip Path.
Paint p = new Paint();
p.setStyle(Style.STROKE);
p.setColor(Color.RED);
// A currently defined path to clip the bitmap with
Path clipPath = new Path();
clipPath.moveTo(top_left.getX() + nodes.getNodeVals('L').getX(), top_left.getY() + nodes.getNodeVals('T').getY());
clipPath.addPath(outline);
c.save(); // Save the canvas (rotations, transformations, etc)
c.clipPath(clipPath); // Create a clip region
c.drawPath(clipPath, p); // Draw that clip region in red
c.drawBitmap(img, top_left.getX(), top_left.getY(), null); // Draw the bitmap in the clip
c.restore(); // Restore the canvas (rotations, transformations, etc)
}
The clipPath.moveTo line is where I'm having my problem, I believe. Basically, it should be creating a new path that is at the location defined with the x and y values of moveTo (I believe I have those set correctly elsewhere). The path is created beforehand, and stored into outline, and the addPath part should be adding the outline to clipPath.
Thanks in advance!
I'm not entirely sure if I understand exactly what it is you are trying to do, but if you simply want to offset the path from its original position, moveTo is not the way to go since the coordinates of a path you add will be retained.
Instead, you can add the offset coordinates in your addPath:
//clipPath.addPath(outline);
clipPath.addPath(outline, dx, dy);
where dx and dy are your offsets.
i am having an image in canvas.i want to get color at particular pixels and replace with user defined color.is it possible to do for abitmap placed in android canvas?
Thanks in advance..
Unfortunately Canvas is more like a write-only object: you can draw on the Bitmap, but you can't really get almost any information on the Bitmap. But if you have access to the Bitmap itself, this is a very easy task: just use Bitmap.getPixel(int x, int y) to get the color of the pixels and then draw whatever you need on the Canvas or even just call Bitmap.setPixel(int x, int y, int color) without creating any Canvas at all.
Sure. just bitmap.getPixel(x, y) and setPixel(x, y, color). Make sure that the bitmap your working on is mutable.
Image in Canvas with touch events