I'm using two ways to display buttons and edit texts in my application : one is by using a xml shape drawable, and applying it to the background of a button/edittext, the other is by using a canvas in a custom view.
I expect the result to be exactly the same, but it's not. You can see below the tiny difference between the respective strokes :
Both are round rectanlges with a 2dp stroke width, and a 8dp corners radius. Here is the code for each of them :
Left, the drawable shape used as a background :
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/transparent"/>
<corners android:radius="@dimen/button_cornersRadius"/>
<stroke
android:width="@dimen/button_strokeWidth"
android:color="@color/blue_light"/>
</shape>
Right, the onDraw method of the custom view :
protected void onDraw(Canvas canvas) {
canvas.drawPath(drawingPath,strokePaint);
canvas.drawText(text,demiWidth,textHeight,textPaint);
}
Whith :
strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaint .setStyle(Paint.Style.STROKE);
strokePaint .setStrokeWidth((int)(2*densityObtainedWithDisplayMetrics)));
strokePaint .setColor(the exact same as above);
And I observe tiny differences when displaying gradients with the same supposedly equivalent ways : The shape drawable has a nicer display, the colors look better, the lines are not blurred.
Could someone explain me why / how to work around it? If I was to use only one way, it would be the second, but I don't want to since it doesn't look clean. Thanks for any help!
EDIT :
Surprisingly, this difference occurs only when the stroke width of the custom layout is odd. With even values the display is exactly the same!