Android图像缩放
我正在为 1.6 制作一个应用程序,但我遇到了 android 图像缩放问题,我有一张 480x295 的图片。
在中型屏幕上,它可以正确显示,但在大屏幕(480x800 或 480x854)上,它不会填满屏幕,Android 会使图像放大 1.5 倍,即只有 720x442。
由于 800 实际上是 1.67,854 是 1.78,所以我显然可以在drawable-hdpi 文件夹中包含大图像,但图像已经是 1.5mb,这比人们似乎喜欢的要大,而且我不能使用 app2sd,因为我想支持1.6。
有什么建议吗?
我只能想到三个选择: 1) 包含较大的图像(但这可能会限制销售,并且明显增加 apk 大小)
2) 制作 2 个版本,似乎是一个很好的解决方案,只是更难实现。
3) 更改为 1.5,并自行处理所有缩放。
编辑: 更多详情: 我正在使用画布和表面视图进行绘图 图片加载代码:
backgroundBMP = BitmapFactory.decodeResource(getResources(), R.drawable.background, null);
和绘图代码:
canvas.drawBitmap(backgroundBMP, 0, 0, null);
I'm making an app for 1.6 and I'm having a problem with android image scaling, I have a picture that's 480x295.
On a medium screen this appears correctly, but on a large screen (480x800 or 480x854) it doesn't fill the screen, it android makes the image 1.5x bigger, which is only 720x442.
As 800 is actually 1.67 and 854 is 1.78, I can obviously just include large images for the drawable-hdpi folder, but the image is already 1.5mb, which is larger than people seem to like, and I can't use app2sd as I want to support 1.6.
Any suggestions?
I can only think of three options:
1) Include the larger images (but that limits sales probably, and obviously increases the apk size)
2) Make 2 versions, seems a good solution, just harder to implement.
3) Change to 1.5, and handle all my scaling myself.
EDIT:
More details:
I'm drawing using canvas and surfaceview
Image loading code:
backgroundBMP = BitmapFactory.decodeResource(getResources(), R.drawable.background, null);
And the drawing code:
canvas.drawBitmap(backgroundBMP, 0, 0, null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想自己缩放图像,请将单个副本放入:
确保该资源不在其他文件夹中。
像这样加载位图:
像这样调整它的大小:
然后将其绘制到画布上:
这是完成工作所需的最少信息。
附加说明
通常,Android 会根据当前屏幕密度从 res/drawable 文件夹之一中选择图像。
大多数图像格式没有密度信息,因此 Android 根据加载位图的文件夹来假设位图的密度。
这意味着它可以自行缩放,以使加载的位图的密度与画布密度相匹配。
Android 在您加载位图时检查并匹配一次密度,并在您绘制位图时再次检查和匹配密度。
当您进行自己的扩展时,您通常希望防止操作系统干扰。有几种方法可以做到这一点:
1。使用 res/drawable-nodpi
这是我在上面的快速示例中概述的策略。如果您想将图像打包为资源,并且仍然自己控制缩放,请将其放在这里,并且仅放在这里:
如果您需要支持 Cupcake,请在每个文件夹中放置一个副本:
解码器会自动提供选项以防止结垢。然后,Android 将图像密度设置为与屏幕相匹配,因此当您绘制它时它也不会缩放。 编译器将对 res/drawable- 中的图像应用无损压缩xxx 文件夹。
2.使用 res/raw
如果将其放入,则可以使编译器不影响图像:
但是您需要小心防止在运行时缩放:
3。使用资产文件夹
如果将图像放在资产文件夹中,而不是res中,则可以使用真实的文件夹结构来组织它们。像这样加载位图资源:
编译器不会接触它,它会在不缩放的情况下加载,并且它的密度设置为与屏幕匹配,因此当您绘制它时它也不会缩放。
(性能说明:Android 加载图像之前,必须找到该文件。资源比资产的定位速度更快,因此如果将图像打包为资产,可能会出现一些性能损失。)
If you want to scale an image yourself, put a single copy in:
Make sure that resource is in no other folders.
Load the bitmap like this:
Resize it like this:
Then draw it to your canvas:
That's the least you need to know to get the job done.
Additional notes
Normally, Android chooses your image from one of the res/drawable folders based on the current screen density.
Most image formats don't have density information, so Android makes assumptions about the density of the bitmap based on the folder it was loaded from.
That means it can do it's own scaling to match the density of the loaded bitmap with the canvas density.
Android checks and matches densities once when you load the bitmap, and again when you draw the bitmap.
When you are doing your own scaling you generally want to prevent the OS from interfering. There are several ways you can do it:
1. Use res/drawable-nodpi
This is the strategy I outlined in the quick example above. If you want to package your image as a resource, and still control the scaling yourself, put it here, and only here:
If you need to support Cupcake, put a copy in each of these folders instead:
The decoder is automatically provided with options to prevent scaling. Android then sets the image density to to match the screen, so it won't scale when you draw it either. The compiler will apply lossless compression to images placed in res/drawable-xxx folders.
2. Use res/raw
You can leave the image untouched by the compiler if you put it in:
But then you need to be careful to prevent scaling at runtime:
3. Use the assets folder
If you put your images in the assets folder, instead of in res, you can use a real folder structure to organize them. Load a bitmap asset like this:
The compiler won't touch it, it loads without scaling, and it's density is set to match the screen, so it won't get scaled when you draw it either.
(PERFORMANCE NOTE: Before Android loads an image, it has to locate the file. A resource is faster to locate than an asset, so there may be some performance penalty if you package your images as assets.)
如果您想缩放位图,可以通过在
canvas.drawBitmap()
之前使用canvas.scale(scaleX,scaleY);
来实现。您可以计算纵横比并将其用作比例值。需要补充的一件重要事情是,您可以使用
来使用“移动到 SD 卡”功能,并且仍然支持较低的 Android 版本,具体由minSdkVersion
。If you wanna scale your
Bitmap
you can do that by usingcanvas.scale(scaleX, scaleY);
beforecanvas.drawBitmap()
. You could calculate the aspect ratio and use that as your scale value.One important thing to add is that you can use the "Move to SD card" feature using
<uses-sdk android:minSdkVersion="3" />
and still support lower Android versions, specified byminSdkVersion
.正如 Viktor 所建议的,您可以执行 Canvas.scale()。您还可以使用 BitmapDrawable(设置为视图的背景)绘制位图,并且将为您完成缩放。
Like suggested by Viktor, you can do a Canvas.scale(). You could also draw the bitmap using a BitmapDrawable (set as the View's background) and the scale will be done for you.