Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between shape drawable and canvas.draw, workaround?
    text
    copied!<p>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.</p> <p>I expect the result to be exactly the same, but it's not. You can see below the tiny difference between the respective strokes :</p> <p><img src="https://i.stack.imgur.com/z48WU.png" alt="enter image description here"></p> <p>Both are round rectanlges with a 2dp stroke width, and a 8dp corners radius. Here is the code for each of them :</p> <p>Left, the drawable shape used as a background :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"&gt; &lt;solid android:color="@color/transparent"/&gt; &lt;corners android:radius="@dimen/button_cornersRadius"/&gt; &lt;stroke android:width="@dimen/button_strokeWidth" android:color="@color/blue_light"/&gt; &lt;/shape&gt; </code></pre> <p>Right, the onDraw method of the custom view :</p> <pre><code>protected void onDraw(Canvas canvas) { canvas.drawPath(drawingPath,strokePaint); canvas.drawText(text,demiWidth,textHeight,textPaint); } </code></pre> <p>Whith :</p> <pre><code> strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); strokePaint .setStyle(Paint.Style.STROKE); strokePaint .setStrokeWidth((int)(2*densityObtainedWithDisplayMetrics))); strokePaint .setColor(the exact same as above); </code></pre> <p>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.</p> <p>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!</p> <p>EDIT :</p> <p>Surprisingly, this difference occurs only when the stroke width of the custom layout is odd. With even values the display is exactly the same!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload