我已经阅读了关于将.png应用为ImageView蒙版(Android how to apply mask on ImageView?)的帖子,但我很好奇是否可以在没有.png文件的情况下完成。
我有一个占据整个屏幕的ImageView。我想要的是掩盖它中间的一个正方形。但是我不想使用实际的.png文件并将其用作掩码。我想通过制作适当大小的ImageView然后使用ImageView作为蒙版来“绘制”正方形。
这可能吗?
答案 0 :(得分:1)
难道你不能只绘制一个圆形渐变而不是png的位图吗?我抓住了makeRadGrad,您可以将其用作您链接的answer中的位图:
private Bitmap mImage = ...;
private Bitmap mMask = ...; // png mask with transparency
private int mPosX = 0;
private int mPosY = 0;
private final Paint maskPaint;
private final Paint imagePaint;
public CustomView (final Context context) {
maskPaint = new Paint();
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
imagePaint = new Paint();
imagePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
mImage = //set image
mMask = makeRadGrad(mImage.width()); //we'll just assume you have a perfectly square image
}
/* TODO
if you have more constructors, make sure you initialize maskPaint and imagePaint
Declaring these as final means that all your constructors have to initialize them.
Failure to do so = your code won't compile.
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawBitmap(mMask, 0, 0, maskPaint);
canvas.drawBitmap(mImage, mPosX, mPosY, imagePaint);
canvas.restore();
}
private Bitmap makeRadGrad(int size) {
int radius = (int) (size/2f);
RadialGradient gradient = new RadialGradient(radius, radius, radius, 0xFF00FF00,
0x00000000, android.graphics.Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setDither(true);
p.setShader(gradient);
Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
c.drawCircle(radius, radius, radius, p);
return bitmap;
}
答案 1 :(得分:1)
是的,这是可能的。
在您引用的答案中,您会看到他们如何设置Paint
进行屏蔽然后调用
canvas.drawBitmap(mMask, 0, 0, maskPaint);
没有理由你不能用
做同样的事情canvas.drawRoundRect(left, top, right, bottom, radius, radius, maskPaint);
答案 2 :(得分:0)
感谢您的帮助。
我认为我会分享我的确切解决方案,因为我的目标是创建一个占据整个屏幕并在顶部区域有一个方形切口的位图。
这段代码可以解决问题:
private void createMask()
{
int dimension = 300;
ImageView mask = new ImageView(_activity);
//Create the square to serve as the mask
Bitmap squareMask = Bitmap.createBitmap(dimension,dimension, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(squareMask);
Paint black = new Paint();
black.setStyle(Paint.Style.FILL);
black.setColor(Color.BLACK);
canvas.drawRect(0.0f,0.0f,(float)dimension,(float)dimension,black);
//create the darkness bitmap
Bitmap darknessView = Bitmap.createBitmap((int)screenWidthPX(),(int)screenHeightPX(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(darknessView);
black.setStyle(Paint.Style.FILL);
black.setColor(Color.BLACK);
canvas.drawRect(0.0f,0.0f,(int)screenWidthPX(),(int)screenHeightPX(),black);
//create the masked version of the darknessView
Bitmap darknessViewMasked = Bitmap.createBitmap((int)screenWidthPX(),(int)screenHeightPX(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(darknessViewMasked);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawBitmap(darknessView,0,0,null);
canvas.drawBitmap(squareMask,0,0,paint);
paint.setXfermode(null);
_darknessView.setImageBitmap(darknessViewMasked);
root.addView(_darknessView);
}