2016-12-01 27 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