在blackberry中, 默认创建的渐变色背景都是矩形的, 有时我们需要绘制圆角矩形的渐变色背景, 所以就自己定义一个工厂类, 用来创建特殊的渐变色背景
/**
* @author YeYong
* @date 2010-7-21 PM10:42:46
*/
public class SfBackgroundFactory {
/**
* 创建渐变色的圆角矩形背景
* @param colorTopLeft - Starting color at top-left corner of background.
* @param colorTopRight - Starting color at top-right corner of background.
* @param colorBottomRight - Starting color at bottom-right corner of background.
* @param colorBottomLeft - Starting color at bottom-left corner of background.
* @param arcWidth Width of arc used to round the four corners.
* @param arcHeight Height of arc used to round the four corners.
* @return
*/
public static Background createLinearGradientRoundBackground(final int colorTopLeft,
final int colorTopRight, final int colorBottomRight, final int colorBottomLeft,
final int arcWidth, final int arcHeight) {
final Background bg = BackgroundFactory.createLinearGradientBackground(colorTopLeft,
colorTopRight, colorBottomRight, colorBottomLeft);
return new Background() {
public boolean isTransparent() {
return bg.isTransparent();
}
public void draw(Graphics g, XYRect xyRect) {
int width = xyRect.width;
int height = xyRect.height;
XYRect rect = new XYRect(0, 0, width, height);
Bitmap bg1 = new Bitmap(width, height);
Graphics g2 = Graphics.create(bg1);
BackgroundFactory.createLinearGradientBackground(colorTopLeft, colorTopRight,
colorBottomRight, colorBottomLeft).draw(g2, rect);
Bitmap bg2 = new Bitmap(width, height);
Graphics g3 = Graphics.create(bg2);
g3.setColor(Color.BLACK);
g3.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
g3.rop(Graphics.ROP2_DSo, 0, 0, width, height, bg1, 0, 0);
g.setColor(Color.WHITE);
g.fillRoundRect(xyRect.x, xyRect.y, width, height, arcWidth, arcHeight);
g.rop(Graphics.ROP2_DSa, xyRect.x, xyRect.y, width, height, bg2, 0, 0);
}
};
}
}