Note that there are some explanatory texts on larger screens.

plurals
  1. POPorterDuffXfermode DST_IN not working as expected
    primarykey
    data
    text
    <p>So I'm trying to speed up some drawing we're doing (drawing a portion of an arc with alpha transparency) and was attempting to cache the entire arc into a separate bitmap, and show it selectively with an alpha mask. </p> <p>From the research I've done (the Xfermodes API demo for Android, <a href="http://www.ibm.com/developerworks/java/library/j-mer0918/" rel="nofollow noreferrer">this example</a>, and <a href="https://sites.google.com/site/drjohnbmatthews/composite" rel="nofollow noreferrer">this tool</a>), if I have for example the following two graphics:</p> <p><img src="https://i.stack.imgur.com/t7aw7.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/nNZNM.png" alt="enter image description here"></p> <p>and draw using the following:</p> <pre><code>Xfermode DST_IN = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); canvas.drawBitmap(circle, 0, 0, paint); paint.setXfermode(DST_IN); canvas.drawBitmap(arc, 0, 0, paint); paint.setXfermode(null); </code></pre> <p>I should get this result:</p> <p><img src="https://i.stack.imgur.com/7D7Ba.png" alt="enter image description here"></p> <p>Where the destination image (the circle) is clipped to the area where the source image (the arc) is drawn. Instead, I get the full circle. If I just draw the arc, it appears in the correct location, and if I use DST_OUT instead, I get the inverse of the expected result (the other three quadrants of the circle). </p> <p>I've also made sure to disable hardware rendering for this view, in case there was an issue with this Xfermode, but it doesn't make a difference.</p> <p>I broke it out into a separate project at the simplest level trying to get it to work, and using the following code, I still have the same problem:</p> <pre><code>public class ClippedView extends View { private Xfermode DST_IN, DST_OUT; private Paint paint; public ClippedView(Context context) { super(context); init(); } private void init() { setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); paint = new Paint(Paint.ANTI_ALIAS_FLAG); DST_IN = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); DST_OUT = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(Color.GREEN); canvas.drawRect(0, 0, getWidth() / 2, getHeight() / 2, paint); paint.setColor(Color.BLACK); paint.setXfermode(DST_IN); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint); paint.setXfermode(null); } } </code></pre> <p>Am I using it wrong? Am I just missing something? Have I found a bug? :)</p>
    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.
 

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