2016-12-01 43 views
1

我想使用畫布在設備的中心繪製4個角邊緣。如何繪製4角邊緣設備的中心

像這樣

enter image description here

我想這

Paint myPaint = new Paint(); 
myPaint.setColor(Color.GREEN); 
myPaint.setStyle(Paint.Style.STROKE); 
canvas.drawRect(
     getLeft()+(getRight()-getLeft())/4, 
     getTop()+(getBottom()-getTop())/4, 
     getRight()-(getRight()-getLeft())/4, 
     getBottom()-(getBottom()-getTop())/3,myPaint); 

我的結果

enter image description here

+3

使用[drawPath](https://developer.android.com/reference/android/graphics/Canvas.html# drawPath(android.graphics.Path,android.graphics.Paint)而不是drawRect。 – Blackbelt

+1

使用矢量Drawable for this .. http://stackoverflow.com/a/40217644/6893465 –

+1

...或一個簡單的9補丁 –

回答

0

我繪製此使用9補丁圖像....

Bitmap b= BitmapFactory.decodeResource(getResources(), R.drawable.bg_scanner_frame); 
    paint.setColor(Color.RED); 
    canvas.drawBitmap(b, width/2-b.getWidth()/2, height/2-b.getHeight()/2, paint); 

enter image description here