Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way is to do it in <code>Canvas</code> using <code>PorterDuff</code> operations and/or <code>Shaders</code>. Let's say your <code>Bitmap</code> is available and stored in <code>mBitmap</code>.</p> <p>Option 1: Using Shaders.</p> <pre><code>@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); // Load the bitmap as a shader to the paint. final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); final Shader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); paint.setShader(shader); // Draw a circle with the required radius. final float halfWidth = canvas.getWidth()/2; final float halfHeight = canvas.getHeight()/2; final float radius = Math.max(halfWidth, halfHeight); canvas.drawCircle(halfWidth, halfHeight, radius, paint); } </code></pre> <p>Option 2: Using PorterDuff mode.</p> <pre><code>@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); // Create a circular path. final float halfWidth = canvas.getWidth()/2; final float halfHeight = canvas.getHeight()/2; final float radius = Math.max(halfWidth, halfHeight); final Path path = new Path(); path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW); final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawPath(path, paint); } </code></pre> <p>Note:</p> <ol> <li>It's not good to create objects inside onDraw() calls. Hence you should have your paint and shader initially somewhere else. This could probably be done when you set the image bitmap to the view.</li> <li><code>Canvas</code> might need to be saved and restored when it is not backed by a hardware texture. The general ideas around it are not mentioned here.</li> <li>Remember to add <code>setWillNotDraw(false);</code> to the constructor.</li> </ol> <p>Additional References:</p> <ol> <li><a href="https://sriramramani.wordpress.com/2012/12/21/shaders/">https://sriramramani.wordpress.com/2012/12/21/shaders/</a> has information on <code>Shaders</code>.</li> <li><a href="http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/ShapedButton.java">http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/ShapedButton.java</a> uses <code>Path</code> to curved button in Firefox for Android.</li> <li><a href="http://sriramramani.wordpress.com/2012/08/27/constructing-squishy-buttons/">http://sriramramani.wordpress.com/2012/08/27/constructing-squishy-buttons/</a> has information on <code>Canvas</code> saving, restoring and special cases for pre-ICS.</li> </ol>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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