在View上创建视频阻止外观

时间:2012-11-08 15:17:39

标签: android android-view android-video-player

我正在创建一个可以从网络播放视频文件的活动。那部分运作良好。

在视频的某些位置,我需要覆盖视频的视图。视图将具有基本的UI元素,主要是按钮和一些文本(无法预先确定)。

我希望叠加层看起来好像是视频的一部分,而不一定是原生的UI元素。我遇到的主要问题是因为视频质量稍差,它会有很多“阻塞”,而UI元素的绘制非常清晰。

有人可以建议如何在UI元素中添加“阻止”吗?

我的想法是我可以覆盖父元素的draw(),并以某种方式使用过滤器来创建效果,但我将如何做到这一点超出了我。

1 个答案:

答案 0 :(得分:0)

最后,我找到了一个适合我的解决方案。它涉及将视图作为位图抓取,缩小它,然后使用拉伸将其恢复到原始大小。这给出了块状,像素化的外观。通过在缩放时改变大小差异,我可以准确地控制它的阻塞方式。

确保在构造函数中设置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);
}