使用位图作为布局的 alpha
Using bitmap as an alpha for Layout
我想在布局上使用位图作为 aplha。例如,如果我使用一个带有背景颜色的布局和一个 Imageview 或在此布局之上的另一个布局,我想使用 aplha 蒙版显示一个形状。
一张照片胜过一千个字:
我已经调查了 Canvas.drawBitmap
,但我不知道我该如何处理它
你觉得可能吗?谢谢
PS : 这只是一个例子,alpha 位图实际上要复杂得多(我不能只放一个 TextView)
这是一个适合您的功能:
public static Bitmap makeTransparent(Bitmap bit, Bitmap mask) {
int width = bit.getWidth();
int height = bit.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixels = new int [ bmp.getHeight()*bmp.getWidth()];
bit.getPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),bmp.getHeight());
bmp.setPixels(allpixels, 0, width, 0, 0, width, height);
Bitmap bmpM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixelsM = new int [ bmpM.getHeight()*bmpM.getWidth()];
mask.getPixels(allpixelsM, 0, bmpM.getWidth(), 0, 0, bmpM.getWidth(),bmpM.getHeight());
bmpM.setPixels(allpixelsM, 0, width, 0, 0, width, height);
for(int i =0; i<bmp.getHeight()*bmp.getWidth();i++) {
int A = (allpixelsM[i] >> 16) & 0xff;
int R = (allpixels[i] >> 16) & 0xff;
int G = (allpixels[i] >> 8) & 0xff;
int B = (allpixels[i]) & 0xff;
allpixels[i] = Color.argb(A, R, G, B);
}
bmp.setPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
return bmp;
}
我想在布局上使用位图作为 aplha。例如,如果我使用一个带有背景颜色的布局和一个 Imageview 或在此布局之上的另一个布局,我想使用 aplha 蒙版显示一个形状。 一张照片胜过一千个字:
我已经调查了 Canvas.drawBitmap
,但我不知道我该如何处理它
你觉得可能吗?谢谢
PS : 这只是一个例子,alpha 位图实际上要复杂得多(我不能只放一个 TextView)
这是一个适合您的功能:
public static Bitmap makeTransparent(Bitmap bit, Bitmap mask) {
int width = bit.getWidth();
int height = bit.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixels = new int [ bmp.getHeight()*bmp.getWidth()];
bit.getPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),bmp.getHeight());
bmp.setPixels(allpixels, 0, width, 0, 0, width, height);
Bitmap bmpM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixelsM = new int [ bmpM.getHeight()*bmpM.getWidth()];
mask.getPixels(allpixelsM, 0, bmpM.getWidth(), 0, 0, bmpM.getWidth(),bmpM.getHeight());
bmpM.setPixels(allpixelsM, 0, width, 0, 0, width, height);
for(int i =0; i<bmp.getHeight()*bmp.getWidth();i++) {
int A = (allpixelsM[i] >> 16) & 0xff;
int R = (allpixels[i] >> 16) & 0xff;
int G = (allpixels[i] >> 8) & 0xff;
int B = (allpixels[i]) & 0xff;
allpixels[i] = Color.argb(A, R, G, B);
}
bmp.setPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
return bmp;
}