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

小智..

7

将您的蒙版图像放在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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值