更改缩放 SurfaceView 中的插值方法

发布于 2025-01-06 03:07:55 字数 1397 浏览 7 评论 0 原文

可以以低分辨率渲染视图,然后使用 SurfaceHoldersetFixedSize() 方法。然而,缩放是通过某种插值完成的,导致一切都变得模糊。
有没有什么方法可以将插值方法更改为最近邻居或只是将其关闭?

以下是我的意思的示例,在全屏视图中使用 4x4 表面制作:
我想要什么

如果有人感兴趣的话,用于生成正确图像的示例代码:

public class ScaleView extends SurfaceView implements SurfaceHolder.Callback {
    private final static float[] points = {0,0, 2,0, 4,0, 1,1, 3,1, 0,2, 2,2, 4,2, 1,3, 3,3};
    private Paint white;
    public ScaleView(Context context) {
        super(context);
        white = new Paint();
        white.setColor(0xFFFFFFFF);
        getHolder().addCallback(this);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
        Canvas c = holder.lockCanvas();
        try{
            c.drawPoints(points, white);
        }finally{
            holder.unlockCanvasAndPost(c);
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){
        holder.setFixedSize(4, 4);
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){}
}

注意:上面的方形图案只是我想避免的效果的一个示例。我正在寻找一种通用的解决方案,可以应用于任何内容的任何视图(带有表面支架)。

It is possible to render a view at a low resolution, and then scale it up to fit the actual size of your view using the setFixedSize() method of a SurfaceHolder. However, the scaling is done with some kind of interpolation, causing everything to blur.
Is there any method for changing the method of interpolation to nearest neighbour or just turning it off?

Here is an example of what I mean, Made with a 4x4 surface in a fullscreen-view:
What I want What I get
Left image: This is how I want the result to look (here achieved by drawing a nonfiltered bitmap)
Right image: This is what happens when the 4x4 canvas is scaled to fullscreen.

Sample code for generating the right image if anyone's interested:

public class ScaleView extends SurfaceView implements SurfaceHolder.Callback {
    private final static float[] points = {0,0, 2,0, 4,0, 1,1, 3,1, 0,2, 2,2, 4,2, 1,3, 3,3};
    private Paint white;
    public ScaleView(Context context) {
        super(context);
        white = new Paint();
        white.setColor(0xFFFFFFFF);
        getHolder().addCallback(this);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
        Canvas c = holder.lockCanvas();
        try{
            c.drawPoints(points, white);
        }finally{
            holder.unlockCanvasAndPost(c);
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){
        holder.setFixedSize(4, 4);
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){}
}

Note: The square pattern above is just an example of the effect I want to avoid. I am looking for a general solution that can be applied to any view (with a surfaceholder) with any content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2025-01-13 03:07:55

抱歉,这不是你能控制的。没有定义在这些表面上进行什么样的缩放——这取决于硬件及其配置方式。此外,这种极端的缩放确实应该避免,因为在某些情况下硬件无法做到这一点,因此您最终会陷入较慢的路径。 (例如,如果将表面放入叠加层中,则许多硬件叠加引擎无法进行这种极端缩放。)

Sorry, you can't control this. It is not defined what kind of scaling will be done on these surfaces -- it depends on the hardware and how it is configured. Also, this kind of extreme scaling really should be avoided, since in some cases hardware can't do it so you will end up in slower paths. (For example if surfaces are being put into overlays, many hardware overlay engines can't do that kind of extreme scaling.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文