Android - 如何通过 alpha masking 提高图像翻译的性能?
在每个触摸事件上调用它来渲染 alpha 掩码位图:
......
Canvas canvas = new Canvas();
Bitmap bleed = BitmapFactory.decodeResource(resources, R.drawable.bleed);
Bitmap photoBG = BitmapFactory.decodeResource(resources, R.drawable.photo_bg);
Bitmap mask = BitmapFactory.decodeResource(resources, R.drawable.mask);
Bitmap result = Bitmap.createBitmap(bleed.getWidth(), bleed.getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(photoBG, 0, 0, paint);
canvas.drawBitmap(selectedImage, matrix, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
canvas.drawBitmap(bleed, 0, 0, paint);
myImageView.setImageBitmap(result);
bleed.recycle();
mask.recycle();
img.invalidate();
}
我有这样的代码,我 结果图像没问题,但是当我拖动图像时,性能非常慢,我附上了解释性图像和我的应用程序屏幕截图(注意:应用程序的背景是灰色的)。
我应该坚持此代码(在画布上绘图)还是有更好的方法来实现我的目标?
I have this code that I call on each touch event that render an alpha masked bitmap:
...
Canvas canvas = new Canvas();
Bitmap bleed = BitmapFactory.decodeResource(resources, R.drawable.bleed);
Bitmap photoBG = BitmapFactory.decodeResource(resources, R.drawable.photo_bg);
Bitmap mask = BitmapFactory.decodeResource(resources, R.drawable.mask);
Bitmap result = Bitmap.createBitmap(bleed.getWidth(), bleed.getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(photoBG, 0, 0, paint);
canvas.drawBitmap(selectedImage, matrix, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
canvas.drawBitmap(bleed, 0, 0, paint);
myImageView.setImageBitmap(result);
bleed.recycle();
mask.recycle();
img.invalidate();
}
...
The result image is ok but when I drag the image around, the performance is really slow, I've attached an explanatory image and my app screen capture (Note: the background is gray at the app).
Should I stick to this code (drawing on a canvas) or is there a better way achieving my goal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次触摸都会创建 4 个位图,这就是性能问题。这些都是非常昂贵的调用(尤其是decode*() 调用。)仅创建/加载 btimap 一次:)
You are creating 4 bitmaps on every touch, that's your performance issue. These are very expensive calls (especially the decode*() calls.) Create/load the btimaps only once :)