Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to programmatically draw smooth stroke around a button in Android?
    primarykey
    data
    text
    <p>I am trying to draw a smoothed antialiased stroke around a gradient-filled button but the corners gets really ugly. This is my code:</p> <pre><code>import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.Shape; public class StrokeShapeDrawable extends ShapeDrawable { Paint fillpaint; Paint strokepaint; private static final int WIDTH = 1; public StrokeShapeDrawable(Shape s) { super(s); fillpaint = this.getPaint(); strokepaint = new Paint(fillpaint); strokepaint.setAntiAlias(true); strokepaint.setStyle(Paint.Style.STROKE); strokepaint.setStrokeWidth(WIDTH); strokepaint.setARGB(255, 0, 0, 0); } @Override protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint) { shape.draw(canvas, fillpaint); shape.draw(canvas, strokepaint); } public void setFillColour(int c) { fillpaint.setColor(c); } } </code></pre> <p>And the button creation:</p> <pre><code> ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { LinearGradient lg = new LinearGradient(0, 0, 0, mLoginBtn.getHeight(), new int[] { 0xfff6f6f6, 0xffd2d3d5, 0xffc0c1c4, 0xffc0c1c4}, new float[] { 0, 0.45f, 0.55f, 1 }, Shader.TileMode.REPEAT); return lg; } }; float[] roundedCorner = new float[] {15, 15, 15, 15, 15, 15, 15, 15}; StrokeShapeDrawable b = new StrokeShapeDrawable(new RoundRectShape(roundedCorner, null, null)); b.setShaderFactory(sf); mLoginBtn.setBackgroundDrawable((Drawable)b); </code></pre> <p>My button looks like this:</p> <p><img src="https://i.stack.imgur.com/GOdTc.png" alt="Button"></p> <p>Not very nice, anyone has any ideas?</p> <p>Also I do not want to use a XML file since I am not sure it's possible to achieve two gradients with 50 precent each of the button without using dp's to specify gradient position (since I want to have 50-50 gradients).</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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