Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Is it possible to declare an alpha mask directly within layer-list XML definition?
    primarykey
    data
    text
    <p>A newbie question</p> <p>I have this layers.xml that I use as a source for an ImageView. And two images, mask.png and image.jpg</p> <p>layers.xml:</p> <pre><code>&lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item&gt; &lt;bitmap android:src="@drawable/image" android:gravity="center"/&gt; &lt;/item&gt; &lt;item&gt; &lt;bitmap android:src="@drawable/mask" android:gravity="center"/&gt; &lt;/item&gt; &lt;/layer-list&gt; </code></pre> <p>ImageView:</p> <pre><code>&lt;ImageView android:id="@+id/img_B" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/layers"/&gt; </code></pre> <p>At the moment the output is just the png over the image. I would like the png to act as a mask, clipping the image using the png alpha channel like so: <img src="https://i.stack.imgur.com/6MrKK.jpg" alt="enter image description here"></p> <p>Is that possible directly within the xml, or do I have to do it by code?</p> <p>Thanks for your advice ;)</p> <p>update: at the moment I achieved my goal using code to replace the entire ImageView</p> <pre><code>ImageView img = (ImageView) findViewById(imgID); Canvas canvas = new Canvas(); Bitmap mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.img); Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask); Bitmap result = Bitmap.createBitmap(mainImage.getWidth(), mainImage.getHeight(), Bitmap.Config.ARGB_8888); canvas.setBitmap(result); Paint paint = new Paint(); paint.setFilterBitmap(false); canvas.drawBitmap(mainImage, 0, 0, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null); img.setImageBitmap(result); img.invalidate(); </code></pre>
    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.
 

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