android 畫一對漂亮的括號。是()() 這種括號。注意是在canvas上畫出來的 ,不是用字符代替的。
這是因為我需要根據需要不同尺寸的括號。但是用drawArc畫出的括號很丑。以下是主要代碼。如何才能畫一個比較漂亮的括號呢。
public static final int bracketWidth = ViewTools.dip2px(7);
private static Paint bracketPaint = new Paint();
static {
bracketPaint.setStrokeWidth(ViewTools.dip2px(1.5f));
bracketPaint.setStyle(Paint.Style.STROKE );
}
public static void drawLeftBracket(Canvas canvas, int x, int y, int height) {
x += bracketWidth / 2;
RectF rect = new RectF(x, y, x + bracketWidth, y + height);
canvas.drawArc(rect, 90, 180, false, bracketPaint);
}
問題解決了。采用一張比較大的PNG括號圖片,根據需要進行放縮節OK。
private static final int textBracketMaxHeight = ViewTools.dip2px(20);
private static final int textBracketMaxWidth = ViewTools.dip2px(10);
public static void drawLeftBracketPng(Canvas canvas, int x, int y,
int height){
Bitmap btp;
if (height > textBracketMaxHeight) {
btp = ViewTools.zoom(leftBracketPng, textBracketMaxWidth, height);
}else{
float scale = (float)height/leftBracketPng.getHeight();
btp = ViewTools.zoom(leftBracketPng, scale, scale);
}
canvas.drawBitmap(btp, x, y, new Paint());
}
public static void drawRightBracketPng(Canvas canvas, int x, int y,
int height){
Bitmap btp;
if (height > textBracketMaxHeight) {
btp = ViewTools.zoom(rightBracketPng, textBracketMaxWidth, height);
}else{
float scale = (float)height/rightBracketPng.getHeight();
btp = ViewTools.zoom(rightBracketPng, scale, scale);
}
canvas.drawBitmap(btp, x, y, new Paint());
}