2013-02-07 125 views
5

我能夠繪製具有尖銳邊緣的矩形,現在我需要使尖銳邊緣變圓。帶圓角的繪製正方形

如何做到這一點?

這是我的代碼:

public void drawShape(Canvas canvas, Renderer renderer, float x, float y, 
     int seriesIndex, Paint paint) { 
    float halfShapeWidth = shape_width/2; 
    canvas.drawRect(x , y - halfShapeWidth, x + SHAPE_WIDTH, y + halfShapeWidth, paint); 
    } 

如何通過傳遞相同的參數,使這個圓角的矩形?

回答

6

您可以使用drawRoundRect

您需要打包在RectF的位置和尺寸,然後才能調用此函數。

-5
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = pixels; 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } 
26

好吧,我解決了它自己通過使用此代碼:

RectF r = new RectF(1,2,3,4); 
canvas.drawRoundRect(r, 0, 0, mPaint); 

希望它會幫助別人。