Android fill path of arc is on wrong side - android

I have a path that is a serie of arc with first arc clockwise, next arc
is counterclockwise, etc.... The last arc join the first in a circle way.
Somthing like this:
(
)
(
)
(
But in circle and each arc touch perfectly the next.
When i use fill, it fill only aproximately the half part of each circle, like if i stoke a line between the start and end point of each circle. The filled part is internal to every arc.
What i want is to fill the inner part of this shape composed of all this arc. Is there some params i have miss ?
Some code:
Path path = new Path();
// for simplicity let's say i have a couple of
path.addArc(rect, (float)startingAngle, sweepAngle);
Paint paintPath = new Paint();
paintPath.setColor(0xFFFFFFFF);
paintPath.setStyle(Paint.Style.FILL);
Bitmap tempBitmap = Bitmap.createBitmap(this.getMeasuredWidth(), this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(tempBitmap);
canvas.drawPath(path, paintPath);
canvas.drawBitmap(tempBitmap, 0, 0, paintPath);
imageViewackground.setImageBitmap(tempBitmap);

Try using
path.arcTo(rect, (float)startingAngle, sweepAngle);
or
path.arcTo(rect, (float)startingAngle, sweepAngle,true);

Related

How to select particular region in bitmap and remove background color in Android

I'm developing an Android application in which user can upload his photo and apply some color filters and save to his gallery. I need user to select some region in bitmap and change its background color and save to gallery. How can I get this?
The following example assumes your area is bound by a triangle.
There are also addRect addCircle, addOval, etc for a path.
Canvas canvas = new Canvas(bmp);
Path p = new Path();
p.moveTo(x1, y1);
p.lineTo(x2, y2);
p.lineTo(x3, y3);
p.close();
canvas.clipPath(p);
canvas.drawColor(myColour);
For a rectangular region the code is much simpler:
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(myColour); // Style.FILL by default
canvas.drawRect(left, top, right, bottom, paint);

Android bitmap with border

I have this function, with this function I can show an image with a little rotation.
I'm trying to display a white border arround the bitmap.
Matrix m = new Matrix();
m.postRotate( rotation, center.x, center.y );
m.postTranslate( ( position.x - center.x ) - xOffset , position.y - ( center.x ) );
// set the current position to the updated position
positionMatrix.set( m );
renderAnimation();
c.drawBitmap( this.bitmap , positionMatrix, paint );
I'm trying to add the white border with this function: reference: stackoverflow border
RectF targetRect = new RectF(left+10, top+10, left + scaledWidth, top + scaledHeight);
Bitmap dest = Bitmap.createBitmap(this.bitmap.getWith() +20, this.bitmap.getHeight() +20, this.bitmap.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(this.bitmap, null, targetRect, null);
c.drawBitmap( this.bitmap , positionMatrix, paint );
But, not works, can some help me
I think you should follow these steps:
Create a bitmap that width = yourImageWidth + boderThick and height= yourImageHeight + boderThick
Canvas draw a White rectangle (draw your background first)
Canvas draw your image (you need to center your image)
Maybe you made a mistake when calculating the side, or draw in a wrong order. Remember to use the same canvas when drawing. In your code i see you use c.draw and canvas.draw... That may cause the problem.
Refer to the code below:
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, 200, 200, paint);//draw your bg
canvas.drawBitmap(bitmap, 20, 20, paint);//draw your image on bg
Sorry, i don't have much time to check your calculated size. I hope this can help.

Line intersection rendering in Android

In my Android application I will retrieve data from a server where some coordinates will be returned back. Then I use these coordinates to create lines and draw them in the view.
I want a line rendered in different manners. For example: line rendering
The line at the top is the original line, and I want it rendered as the shapes at the below.
And there are some lines which intersect with each other. Then the intersection may be rendered as follows:
The manner of intersection rendering at the left is what I want.
So I wonder whether the Android graphics api supports these kinds of operations?
If you're using the Android Canvas to do this drawing your path twice, with a different stroke size and color. Here's an example which creates a Bitmap with an image similar to what you want :
// Creates a 256*256 px bitmap
Bitmap bitmap = Bitmap.createBitmap(256, 256, Config.ARGB_8888);
// creates a Canvas which draws on the Bitmap
Canvas c = new Canvas(bitmap);
// Creates a path (draw an X)
Path path = new Path();
path.moveTo(64, 64);
path.lineTo(192, 192);
path.moveTo(64, 192);
path.lineTo(192, 64);
// the Paint to draw the path
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
// First pass : draws the "outer" border in red
paint.setColor(Color.argb(255, 255, 0, 0));
paint.setStrokeWidth(40);
c.drawPath(path, paint);
// Second pass : draws the inner border in pink
paint.setColor(Color.argb(255, 255, 192, 192));
paint.setStrokeWidth(30);
c.drawPath(path, paint);
// Use the bitmap in the layout
((ImageView) findViewById(R.id.image1)).setImageBitmap(bitmap);

Draw a border (Paint) on current clip (created by different Region.Op)

I want to draw an image into shape of a Path and then add border on the Path. I was able to clip the image with Path but can't find a way to add border on it. I though it would be simple because the API supports Paint object on Canvas.draw* methods.
I asked another question at: Draw bitmap on current clip in canvas with border (Paint) and I accepted the answer. However, after that I found that I need to do a little bit more complicated processing. Because I use two options for clipping instead of one.
Below is my code to clip and image with two different Region.Op parameters
Bitmap srcImage = BitmapFactory.decodeStream(getAssets().open("panda.jpg"));
Bitmap bitmapResult = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Path path = new Path();
// This is my border
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
Canvas canvas = new Canvas(bitmapResult);
// Overlay two rectangles
path.addRect(10, 10, 70, 70, Path.Direction.CCW);
path.addRect(40, 40, 120, 120, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.INTERSECT);
// Draw the circle
path.reset();
path.addCircle(40, 80, 20, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.DIFFERENCE);
// The image is drawn within the area of two rectangles and a circle
// Although I suppose that puting Paint object into drawBitmap() method will add a red border on result image but it doesn't work
canvas.drawBitmap(srcImage, 0, 0, paint);
((ImageView)this.findViewById(R.id.imageView1)).setImageBitmap(bitmapResult);
Here is the result from my code: http://i.stack.imgur.com/8j2Kg.png
And this is what I expect: http://i.stack.imgur.com/iKhIr.png
Do I miss anything to make it work ?

Fill canvas outside rectangle

I want to fill the area outside a rectangle on a canvas. I use
canvas.drawRect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y, paint);
to draw the rectangle, but can't figure out how to fill outside the rectangle/clip.
Thanks
Geoff
Thanks ted and trojanfoe - the neatest solution I've come up with is
Point pTopLeft = new Point();
Point pBotRight = new Point();
//TODO:set x,y for points
Rect rHole = new Rect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y);
//assume current clip is full canvas
//put a hole in the current clip
canvas.clipRect(rHole, Region.Op.DIFFERENCE);
//fill with semi-transparent red
canvas.drawARGB(50, 255, 0, 0);
//restore full canvas clip for any subsequent operations
canvas.clipRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight())
, Region.Op.REPLACE);
You aren't going to fill outside the clip; that's the kind of thing clip is there to prevent! If you want to fill the space outside a rect and inside the drawing layer bounds, construct four auxiliary rects:
Rect above = new Rect(0, 0, canvas.getWidth(), pTopLeft.y);
Rect left = new Rect(0, pTopLeft.y, pTopLeft.x, pBotRight.y);
Rect right = new Rect(pBotRight.x, pTopLeft.y, canvas.getWidth(), pBotRight.y);
Rect bottom = new Rect(0, pBotRight.y, canvas.getWidth(), canvas.getHeight());
Then fill these.
ICS and above ...
canvas.clipRect(rHole, Region.Op.DIFFERENCE);
XOR, Difference and ReverseDifference clip modes are
ignored by ICS if hardware acceleration is enabled.
Just disable 2D hardware acceleration in your view:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Reference Android: Howto use clipRect in API15
You can't draw outside of the Canvas; that area belongs to the parent View. Do you have the ability to subclass the parent View and do your drawing in that class instead?
If you want to draw outside of the Canvas clip then you'll have to invalidate() the areas you are interested in.

Categories

Resources