最后,我找到了一个适合我的解决方案。它涉及将视图抓取为位图,缩小视图,然后将其拉伸回原始大小。这给出了块状,像素化的外观。我可以通过改变缩放时尺寸的差异来精确控制它的阻挡程度。
确保在构造函数中设置了this.setDrawingCacheEnabled(true);
。
private static final float RESAMPLE_QUALITY = 0.66f; // less than 1, lower = worse quality
public void draw(Canvas canvas) {
super.draw(canvas);
Bitmap bitmap_old = this.getDrawingCache();
Bitmap bitmap_new = Bitmap.createScaledBitmap(bitmap_old, Math.round(bitmap_old.getWidth() * RESAMPLE_QUALITY), Math.round(bitmap_old.getHeight() * RESAMPLE_QUALITY), true);
Rect from = new Rect(0, 0, bitmap_new.getWidth(), bitmap_new.getHeight());
RectF to = new RectF(0, 0, bitmap_old.getWidth(), bitmap_old.getHeight());
canvas.drawBitmap(bitmap_new, from, to, null);
}