gpt4 book ai didi

Android - 是否可以直接在层列表 XML 定义中声明 alpha 掩码?

转载 作者:IT老高 更新时间:2023-10-28 21:49:35 24 4
gpt4 key购买 nike

一个新手问题

我有这个 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"/>
</item>
<item>
<bitmap android:src="@drawable/mask" android:gravity="center"/>
</item>
</layer-list>

ImageView :

<ImageView
android:id="@+id/img_B"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/layers"/>

目前输出只是图像上的 png。我希望 png 充当蒙版,使用 png alpha channel 剪切图像,如下所示: enter image description here

这可能直接在 xml 中,还是我必须通过代码来完成?

感谢您的建议;)

更新:目前我使用代码替换整个 ImageView 实现了我的目标

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();

最佳答案

把你的蒙版图片放到drawable-nodpi文件夹中。

否则缩放会出错。

以下是来自应用的一些示例代码。在相机之后它添加了一个蒙版。

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;
}

}

关于Android - 是否可以直接在层列表 XML 定义中声明 alpha 掩码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8630365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com