05-20-2010 12:28 PM
Greetings again,
I'm trying to draw a rounded rectangle with a bitmap texture inside of it and transparent corners. I've got an implementation that works, but it's so sloppy that I was hoping to find a better way to do it.
The approach that I took was to draw the bitmap first, and then use Graphics.clear() to clear out the points in the corners.
I used this algorithm to assist in finding the points that needed to be cleared: http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/c
Can you think of an easier way to do this that uses existing code like the drawRoundRect or drawArc methods?
Solved! Go to Solution.
05-20-2010 02:37 PM
You can ask the Graphics object to drawTexturedPath with your Bitmap as one of the parameters.
Since you need rounded rectangle, your path will have to include various types of points.
For example, if your top left corner is (x, y) and the radius of your arc is r, your path will include 3 points:
(x, y + r), (x + (int) (r * (1. - Math.sqrt(2.))), y + (int) (r * (1. - Math.sqrt(2.)))) and (x + r, y). The first and the third point will have CURVEDPATH_END_POINT type while the middle point will have CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT type.
Other corners will need similar treatment. I also vaguely remember seeing an advice to call
graphics.setDrawingStyle(DRAWSTYLE_AAPOLYGONS, false) prior to your drawTexturedPath - search the forums for more details.
Good luck!
05-20-2010 02:58 PM
Thank you, that's incredibly helpful. If you have a moment take a look at my other post about colors ![]()
10-18-2010 01:20 PM
I had spend quite some time to google how to draw round corners for images in blackberry and it seems like there is no sample code anywhere.
Hope the following piece of code help a little bit.
int w = bmp.getWidth();
int h = bmp.getHeight();
int arc = 14;
int xPts[] = {0, 0, arc, //top left corner
w-arc, w, w, //top right corner
w, w, w-arc, //bottom right corner
arc, 0 , 0}; //bottom left corner
int yPts[] = {arc, 0, 0, //top left corner
0, 0, arc, //top right corner
h-arc, h, h, //bottom right corner
h, h, h-arc}; //bottom left corner
//To be honest, i don't quite get what the following 5 lines are doing... ^_^
int fAngle = Fixed32.toFP(0);
int dvx = Fixed32.cosd(fAngle);
int dux = -Fixed32.sind(fAngle);
int dvy = Fixed32.sind(fAngle);
int duy = Fixed32.cosd(fAngle);
byte end = Graphics.CURVEDPATH_END_POINT;
byte curve = Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT
//each corner has 3 points:
//the 2 end points and the middle point is draw as a curve.
byte types [] = { end, curve, end,
end, curve, end,
end, curve, end,
end, curve, end};
g.drawTexturedPath(xPts, yPts, types, null, 0, 0, dvx, dux, dvy, duy, bmp);
12-14-2010 01:17 PM
Is there no way to apply a "mask shape" to a bitmap to reach the same result?
For example, if i have a rectangle image, is there no way to apply a roundedrectangle as shape, to have as result my image with rounded borders?
04-08-2011 01:34 AM
12-26-2012 09:02 AM
from next time please post your code in proper fomate.
so it is easy to read.
Regards