简体   繁体   English

在Android上重复使用不同屏幕尺寸和密度的可绘制图像

[英]Reuse drawable images for different screen sizes and densities on Android

Context: I'm developing an Android application for tablets (landscape) with image resources which has a resolution of 1920x1200. 背景:我正在为平板电脑(风景)开发一个Android应用程序,其图像资源的分辨率为1920x1200。 That resolution fits on the following screen sizes and densities: 该分辨率适合以下屏幕尺寸和密度:

drawable-xlarge-hdpi
drawable-large-xhdpi

Problem: If I include all my image resources duplicated on this two folders the final size of the APK will be unnecessarily heavy 问题:如果我在这两个文件夹中包含所有重复的图像资源,则APK的最终大小将不必要地大

My unsuccessful approach: I tried to use Alias for this drawables as defined here: http://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources 我的方法不成功:我尝试使用Alias来实现此处定义的drawable: http//developer.android.com/guide/topics/resources/providing-resources.html#AliasResources

I have my image resource in: 我有我的图像资源:

res/drawable-nodpi/image_cmn.png

and the two alias inside corresponding screen sizes and densities folders: 和相应屏幕大小和密度文件夹中的两个别名:

res/drawable-xlarge-hdpi/image.xml
res/drawable-large-xhdpi/image.xml

image.xml: image.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/image_cmn" />

Of course, when I use my image inside a layout file I reference the alias: 当然,当我在布局文件中使用我的图像时,我引用了别名:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/image" />

But sadly Android is not resizing properly the resource for my testing tablet (mdpi) and the result is that I have bigger images. 但遗憾的是Android没有正确调整我的测试平板电脑(mdpi)的资源,结果是我有更大的图像。

I tried to move the original png's to res/drawable even to res/raw but result is the same than res/drawable-nodpi . 我试图将原始png移动到res / drawable甚至res / raw但结果与res / drawable-nodpi相同

If I move this png's to res/drawable-xlarge-hdpi (same of xml alias) the result is correct but naturally that not solve my problem cause also I'd have to copy them to res/drawable-large-xhdpi and apk size increases. 如果我将此png移动到res / drawable-xlarge-hdpi (与xml别名相同),结果是正确的但自然不能解决我的问题因为我也必须将它们复制到res / drawable-large-xhdpi和apk大小增加。

Does anyone know how to achieve that? 有谁知道如何实现这一目标?

I try to avoid using wrap_content on ImageViews due to different platform versions resizing the images differently, if you put an explicit width and height in dp it doesn't matter as much which image gets selected. 我试图避免在ImageViews上使用wrap_content,因为不同的平台版本会不同地调整图像大小,如果在dp中放置一个明确的宽度和高度,那么选择哪个图像并不重要。

In your case I would have a single image in drawable/xhdpi. 在你的情况下,我会在drawable / xhdpi中有一个图像。 Specify it's width and height in the layout file in dp. 在dp中的布局文件中指定它的宽度和高度。 For a 1920x1200px image that would be layout_width="960dp" layout_height="600dp" 对于1920x1200px的图像,即layout_width =“960dp”layout_height =“600dp”

The image will be the roughly the same physical size on x-large tablets as large tablets. 与大型平板电脑相比,x-large平板电脑上的图像尺寸大致相同。

If you want the image to be bigger on x-large tablets, include a second layout file where the width and height of the images are increased but keep the same images. 如果您希望图像在x-large平板电脑上更大,请包含第二个布局文件,其中图像的宽度和高度会增加但保持相同的图像。

Alternatively you can use dimension resources that are different between large and x-large. 或者,您可以使用大型和x大型之间不同的维度资源。

I can propose two solutions for this problem: 我可以为这个问题提出两个解决方案:

First one: 第一:

  1. Put all your images to assets folder. 将所有图像放到资源文件夹中。 Split them to folders by resolutions. 按分辨率将它们拆分为文件夹。
  2. Write your own logic to determine current device resolution and/or size. 编写自己的逻辑以确定当前的设备分辨率和/或大小。
  3. Pick image from folder with determined resolution. 从确定分辨率的文件夹中选择图像。
  4. Set it to your ImageView in code. 在代码中将其设置为ImageView

Second one (it is ugly, but very quick): 第二个(它很难看,但非常快):

Just compress your png images with tinypng.com . 只需使用tinypng.com压缩您的png图像即可 It can reduece your images up to 70-80% without losing quality (for mobile devices). 它可以将图像重新调整到70-80%而不会降低质量(适用于移动设备)。

how about this: 这个怎么样:

  1. put all your image resource in res/drawable-nodpi 将所有图像资源放在res/drawable-nodpi
  2. put different layout files in res/layout-xlarge , res/layout-large , res/layout 将不同的布局文件放在res/layout-xlargeres/layout-largeres/layout
  3. when use this image on ImageView , do not use match_parent or wrap_content , use calculated size in dp instead. ImageView上使用此图像时,请勿使用match_parentwrap_content ,而是使用dp中的计算大小。

this should work, but however, the downside is that it does not use pre-scaling , meaning on lower dpi devices, it will consume more memory. 这应该工作,但是,缺点是它不使用预缩放 ,这意味着在较低的dpi设备上,它将消耗更多的内存。

If you want to use same images for all size device then i think you have to use 9-Patch Images. 如果你想为所有尺寸的设备使用相同的图像,那么我认为你必须使用9-Patch Images。

Here Official Article of Android click here. Android官方文章点击此处。

Before: 之前:

在此输入图像描述

After: 后:

在此输入图像描述

Refer this : Android 9 Patch Image Tutorial 请参阅: Android 9 Patch Image Tutorial

Hope it helps. 希望能帮助到你。

If you want to use same image for all size devices then i think you should use 9 patch Image with each layout contain weight tag inside linear layout in android. 如果你想为所有尺寸的设备使用相同的图像,那么我认为你应该使用9补丁图像,每个布局包含在android中的线性布局内的重量标签。 I think it's work 100%. 我认为这是100%的工作。 I am sure. 我确定。 because i am using this methodology for universal application(means Tablet and Device application) 因为我使用这种方法进行通用应用(意味着平板电脑和设备应用)

Okay i got your problem.i have my word related to this problem 好的,我得到了你的问题。我的话与这个问题有关

There are two way and i recommended both. 有两种方式,我推荐两种方式。

First Way (more convenient related to your current problem. ) 第一种方式 (与您当前的问题相关更方便。)

Upload multiple apk if size become bigger. 如果大小变大,则上传多个apk。

follow official guide . 按照官方指南

Second way i more prefer to use this way. 第二种方式我更喜欢用这种方式。

Create multiple layouts. 创建多个布局。 like below 如下

res/ layout /main_activity.xml ---> # For handsets (smaller than 600dp available width) res / layout /main_activity.xml --->#适用于手机 (可用宽度小于600dp)

res/ layout-sw600dp /main_activity.xml --># For 7” tablets (600dp wide and bigger) res / layout-sw600dp /main_activity.xml - >#适用于7“平板电脑 (600dp宽大)

res/ layout-sw720dp /main_activity.xml # For 10” tablets (720dp wide and bigger) res / layout-sw720dp /main_activity.xml#适用于10英寸平板电脑 (720dp宽大)

I strongly recommended to read each word of this official doc by google . 我强烈建议谷歌阅读这篇官方文档的每个单词。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM