Android Drawable 由于缩放而看起来很丑

发布于 2024-10-29 16:25:16 字数 1374 浏览 1 评论 0原文

我正在编写一个视图,它应该显示一个似乎“永远不会结束”的可绘制对象。 它应该是显示尺寸的两倍或三分之一,并且在显示中缓慢移动。

因此,我研究了谷歌的一些示例代码,发现了重要的线条

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    canvasWidth = width;
    canvasHeight = height;
    float sf = backgroundImage.getWidth() / canvasWidth;
    backgroundImage = Bitmap.createScaledBitmap(backgroundImage,
        (int) (canvasWidth * sf), canvasHeight, true);
    }

来重新缩放图像,而不是

        // decrement the far background
        backgroundXPos = backgroundXPos - DELTAMOVE;
        // calculate the wrap factor for matching image draw
        int newFarX = backgroundImage.getWidth() - (-backgroundXPos);
        // if we have scrolled all the way, reset to start
        if (newFarX <= 0) {
            backgroundXPos = 0;
            // only need one draw
            canvas.drawBitmap(backgroundImage, backgroundXPos, 0, null);

        } else {
            // need to draw original and wrap
            canvas.drawBitmap(backgroundImage, backgroundXPos, 0, null);
            canvas.drawBitmap(backgroundImage, newFarX, 0, null);
        }

绘制移动图像。图像已经在移动了,这很好。

但是,这就是我的问题的重点,图像看起来非常丑陋。其原始尺寸为 960*190 像素 x 240ppi。它应该在高度为 80dip 且宽度为“fill_parent”的视图内绘制。

它在所有设备上看起来应该相同(并且良好)。我尝试了很多,但我不知道如何让照片看起来好看。

感谢您的帮助。 此致, 直到

I am writing a View that should show a drawable that seems to "never end".
It should be twice or third the displaysize and move slow through the display.

Therefore I studied some samplecode by Google and found the important Lines

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    canvasWidth = width;
    canvasHeight = height;
    float sf = backgroundImage.getWidth() / canvasWidth;
    backgroundImage = Bitmap.createScaledBitmap(backgroundImage,
        (int) (canvasWidth * sf), canvasHeight, true);
    }

To rescale the image and than

        // decrement the far background
        backgroundXPos = backgroundXPos - DELTAMOVE;
        // calculate the wrap factor for matching image draw
        int newFarX = backgroundImage.getWidth() - (-backgroundXPos);
        // if we have scrolled all the way, reset to start
        if (newFarX <= 0) {
            backgroundXPos = 0;
            // only need one draw
            canvas.drawBitmap(backgroundImage, backgroundXPos, 0, null);

        } else {
            // need to draw original and wrap
            canvas.drawBitmap(backgroundImage, backgroundXPos, 0, null);
            canvas.drawBitmap(backgroundImage, newFarX, 0, null);
        }

To draw the moving image. The images is already moving, it's fine.

But, and this is the point of my question, the image looks very ugly. Its original is 960*190 pixels by 240ppi. It should be drawn inside a view with 80dip of height and "fill_parent" width.

It should look same (and good) on all devices. I have tried a lot but I don't know how to make the picture look nice.

Thanks for your help.
Best regards,
Till

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

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

发布评论

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

评论(2

岁月染过的梦 2024-11-05 16:25:16

既然你说这是一个永无止境的可绘制的,那么你可能正在编写某种游戏。如果您的图像是像素艺术类型,那么您不需要任何缩放;像素艺术类型图像无法缩放并保持其清晰的外观(您可以尝试使用最近邻插值< /a> 并缩放到原始值的整数倍,有时可能会起作用,但有时您仍然需要手动调整)。这是一种罕见的情况,您实际上需要针对不同的屏幕分辨率拥有不同的图像资源。

否则,您可能想使用矢量图像,但是如果 - 正如您所说 - 您的原始图像是高分辨率图像,那么矢量图像在这里可能没有多大帮助。

顺便说一句,您可能想显示一些屏幕截图。 “看起来很丑”就像说我的代码不起作用一样有用。

Since you're saying that it's a never ending drawable, probably you're writing a game of some sort. If your image is a pixel-art type, then you don't want any scaling; pixel-art-type images cannot be scaled and keep its crisp look (you can try using nearest neighbor interpolation and scaling to an integer multiple of the original, which sometimes might work, but sometimes you will still need manual tweaks). This is the rare case where you actually would need to have different image resource for different screen resolutions.

Otherwise you might want to use a vector image, but if -- as you said -- your original is a high resolution image, then vector image probably won't help much here.

btw, you probably want to show some screenshot. "Looks ugly" is just as helpful as saying my code does not work.

兔姬 2024-11-05 16:25:16

只是猜测,但不要将空绘画传递给您的drawBitmap()调用,而是尝试在禁用位图过滤的情况下进行绘画:

Paint p = new Paint();
p.setFilterBitmap(false);
canvas.drawBitmap(backgroundImage, backgroundXPos, 0, p);

希望有所帮助。

Just a guess, but instead of passing a null paint to your drawBitmap() calls, try making a paint with bitmap filtering disabled:

Paint p = new Paint();
p.setFilterBitmap(false);
canvas.drawBitmap(backgroundImage, backgroundXPos, 0, p);

Hope that helps.

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