Я пытаюсь разработать функцию наклона Instagram для своих приложений.
У меня есть два растровых изображения и одно рисование. Первый - это пустое растровое изображение. Второе - размытое растровое изображение. Третий - это изображение с белым фоном с прозрачным кружком.
Я создал пустое растровое изображение с Canvas и нарисовал размытое изображение на Canvas, затем я нарисовал третью краску на этом Canvas с помощью DST_IN. Я ожидаю, что прозрачная область будет заменена чистым изображением, а белый фон будет заменен размытым изображением.
Спасибо за вашу помощь. Мой код показан ниже
Bitmap emptyBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(emptyBmp);
//Convert original bmp to blurBmp
Bitmap blurBmp = fastblur(mContext, bmp, 8);
canvas.drawBitmap(blurBmp, 0, 0, null);
//Draw white background with transparent circle
Paint p = drawPaint(fTiltX, fTiltY, fTiltRadius, emptyBmp);
canvas.drawRect(0, 0, bmp.getWidth(), bmp.getHeight(), p);
emptyBmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
emptyBmp.recycle();
blurBmp.recycle();
bmp.recycle();
OutputStream outputStream = null;
FastBlur
public Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}
DrawPaint
public Paint drawPaint(float tiltX, float tiltY, float tiltRadius, Bitmap bmp) {
Paint paint = new Paint();
float tiltGradientRadius = tiltRadius / 2.0f;
double bitmapResizedRatio = 1;
int colors[] = new int[4];
colors[0] = 0x00ffffff;
colors[1] = 0x00ffffff;
colors[2] = 0xffffffff;
colors[3] = 0xffffffff;
float tiltRadius1;
tiltRadius1 = tiltRadius - tiltGradientRadius;
tiltRadius1 = Math.min(tiltRadius1, bmp.getWidth());
tiltRadius1 = Math.max(tiltRadius1, 0.0f);
float tiltRadius2 = tiltRadius;
tiltRadius2 = Math.min(tiltRadius2, bmp.getWidth());
tiltRadius2 = Math.max(tiltRadius2, 0.0f);
float positions[] = new float[4];
positions[0] = 0.0f;
positions[1] = tiltRadius1 / tiltRadius;
positions[2] = tiltRadius2 / tiltRadius;
positions[3] = 1.0f;
float shaderTiltX = (float) (tiltX / bitmapResizedRatio);
float shaderTiltY = (float) (tiltY / bitmapResizedRatio);
float shaderTiltRadius = (float) (tiltRadius / bitmapResizedRatio);
RadialGradient shader = new RadialGradient(shaderTiltX, shaderTiltY, shaderTiltRadius, colors, positions, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
//shiftCanvas.drawRect(0, 0, bmp.getWidth(), bmp.getHeight(), paint);
return paint;
}
Это то, что я ожидал. Изображение размыто вокруг прозрачного круга: ![This is what I expected. The image is blur around the transparent circle.](https://i.stack.imgur.com/VrN1d.jpg)
Это мои приложения. Изображение размыто вокруг черного круга. Круг должен быть прозрачным (игнорируйте синий круг): ![This is my apps. The image is blur around the black circle. The circle should be transparent](https://i.stack.imgur.com/yDmlX.jpg)