Android-是否可以直接在层列表XML定义中声明alpha掩码?
新手问题 我将这个layers.xml用作ImageView的源。 还有两个图像,mask.png和image.jpg layers.xml:Android-是否可以直接在层列表XML定义中声明alpha掩码?,android,android-layout,png,mask,alphablending,Android,Android Layout,Png,Mask,Alphablending,新手问题 我将这个layers.xml用作ImageView的源。 还有两个图像,mask.png和image.jpg layers.xml: <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/image" android:gravity="center"/>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/image" android:gravity="center"/>
</item>
<item>
<bitmap android:src="@drawable/mask" android:gravity="center"/>
</item>
</layer-list>
.png
文件在创建时可以包含一个alpha通道,Android可以根据您的解释使用该通道隔离图像
在GIMP或Photoshop或任何您使用的图像编辑器中创建一个额外通道。这将是一个单色通道(256个从白色到黑色的阴影)。选择要遮罩的部分,单击alpha通道并用黑色填充选择区域。反转仍在alpha通道中的选择,并将其填充为白色。将
.png
文件保存并导出为带alpha的24位文件(有效为32位)。您的文件应正确渲染。将您的遮罩图像放在drawable Nodepi文件夹中
否则,缩放将是错误的
下面是一个应用程序的一些示例代码。在相机之后,它添加了一个遮罩
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK )
{
try
{
Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
State.mainActivity.getContentResolver(),
Uri.fromFile(Utils.tempFileForAnImage())
);
cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 256, 256);
Matrix m = new Matrix();
m.postRotate(Utils.neededRotation(Utils.tempFileForAnImage()));
// NOTE incredibly useful trick for cropping/resizing square
// http://stackoverflow.com/a/17733530/294884
cameraBmp = Bitmap.createBitmap(cameraBmp,
0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(),
m, true);
// so, cameraBmp is now a Bitmap. Let's add the mask!!
// see Shiomi Schwartz's original!! http://stackoverflow.com/questions/8630365
Bitmap mask = BitmapFactory.decodeResource(
getResources(),
R.drawable.mask_android_256);
// NOTE THE MASK ** MUST ** BE IN YOUR nodpi folder
Bitmap result = Bitmap.createBitmap( 256,256, Bitmap.Config.ARGB_8888);
Canvas cc = new Canvas();
cc.setBitmap(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
cc.drawBitmap(cameraBmp, 0, 0, null);
cc.drawBitmap(mask, 0,0, paint);
// so, cameraBmp is now a Bitmap but it has been masked
yourImageViewForTheUser.setImageBitmap(result);
// make a "baos" ... we want PNG in this case ..
ByteArrayOutputStream baos = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.PNG, 0, baos);
imageBytesRESULT = baos.toByteArray();
// typically you want the result as image bytes, example to send to Parse
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return;
}
}
这是一个很好的方式来告诉我这里!我不确定,但让我四处看看,然后再联系你。你必须把这个作为一个公认的答案,因为这救了我!给出的答案并没有回答原来的问题,我仍然想知道是否可以使用图层列表来声明遮罩图层。创建alpha通道图像很容易,我问的是如何将此alpha通道用作mask@Erik顺便说一下,这不是一个问题:D
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();
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK )
{
try
{
Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
State.mainActivity.getContentResolver(),
Uri.fromFile(Utils.tempFileForAnImage())
);
cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 256, 256);
Matrix m = new Matrix();
m.postRotate(Utils.neededRotation(Utils.tempFileForAnImage()));
// NOTE incredibly useful trick for cropping/resizing square
// http://stackoverflow.com/a/17733530/294884
cameraBmp = Bitmap.createBitmap(cameraBmp,
0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(),
m, true);
// so, cameraBmp is now a Bitmap. Let's add the mask!!
// see Shiomi Schwartz's original!! http://stackoverflow.com/questions/8630365
Bitmap mask = BitmapFactory.decodeResource(
getResources(),
R.drawable.mask_android_256);
// NOTE THE MASK ** MUST ** BE IN YOUR nodpi folder
Bitmap result = Bitmap.createBitmap( 256,256, Bitmap.Config.ARGB_8888);
Canvas cc = new Canvas();
cc.setBitmap(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
cc.drawBitmap(cameraBmp, 0, 0, null);
cc.drawBitmap(mask, 0,0, paint);
// so, cameraBmp is now a Bitmap but it has been masked
yourImageViewForTheUser.setImageBitmap(result);
// make a "baos" ... we want PNG in this case ..
ByteArrayOutputStream baos = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.PNG, 0, baos);
imageBytesRESULT = baos.toByteArray();
// typically you want the result as image bytes, example to send to Parse
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return;
}
}