android寻找最适合资源过程

简介: When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration.

When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:

drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/

And assume the following is the device configuration:

Locale = en-GB 
Screen orientation = port 
Screen pixel density = hdpi 
Touchscreen type = notouch 
Primary text input method = 12key

By comparing the device configuration to the available alternative resources, Android selects drawables from drawable-en-port.

The system arrives at its decision for which resources to use with the following logic:

Figure 2. Flowchart of how Android finds the best-matching resource.

  1. Eliminate resource files that contradict the device configuration.

    The drawable-fr-rCA/ directory is eliminated, because it contradicts the en-GB locale.

    drawable/
    drawable-en/
    
        
         drawable-fr-rCA/
        
    drawable-en-port/
    drawable-en-notouch-12key/
    drawable-port-ldpi/
    drawable-port-notouch-12key/
    

    Exception: Screen pixel density is the one qualifier that is not eliminated due to a contradiction. Even though the screen density of the device is hdpi, drawable-port-ldpi/ is not eliminated because every screen density is considered to be a match at this point. More information is available in the Supporting Multiple Screens document.

  2. Pick the (next) highest-precedence qualifier in the list (table 2). (Start with MCC, then move down.)
  3. Do any of the resource directories include this qualifier?
  • If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)
  • If Yes, continue to step 4.
  • Eliminate resource directories that do not include this qualifier. In the example, the system eliminates all the directories that do not include a language qualifier:
  •   
       drawable/
      
    drawable-en/
    drawable-en-port/
    drawable-en-notouch-12key/
    
      
       drawable-port-ldpi/
      
    
      
       drawable-port-notouch-12key/
      
    

    Exception: If the qualifier in question is screen pixel density, Android selects the option that most closely matches the device screen density. In general, Android prefers scaling down a larger original image to scaling up a smaller original image. See Supporting Multiple Screens.

  • Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen orientation is the next qualifier for which there are any matches. So, resources that do not specify a screen orientation are eliminated:
       
        drawable-en/
       
    drawable-en-port/
    
       
        drawable-en-notouch-12key/
       
    

    The remaining directory is drawable-en-port.

  • Though this procedure is executed for each resource requested, the system further optimizes some aspects. One such optimization is that once the device configuration is known, it might eliminate alternative resources that can never match. For example, if the configuration language is English ("en"), then any resource directory that has a language qualifier set to something other than English is never included in the pool of resources checked (though a resource directory without the language qualifier is still included).

    When selecting resources based on the screen size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources are larger than the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen).

    Note: The precedence of the qualifier (in table 2) is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen type, and input method), while drawable-en has only one parameter that matches (language). However, language has a higher precedence than these other qualifiers, so drawable-port-notouch-12key is out.

    To learn more about how to use resources in your application, continue to Accessing Resources.

    相关文章
    |
    4月前
    |
    监控 Java 开发工具
    ### 绝招揭秘!Android平台GB28181设备接入端如何实现资源占用和性能消耗的极限瘦身?
    【8月更文挑战第14天】本文介绍在Android平台优化GB28181标准下设备接入的性能方法,涵盖环境搭建、SDK集成与初始化。重点讲解内存管理技巧如软引用、按需加载资源,以及通过硬件加速解码视频数据和图像缩放来减轻CPU与GPU负担。同时采用线程池异步处理视频流,确保UI流畅性。这些策略有助于提高应用效率和用户体验。
    51 0
    |
    6月前
    |
    XML Java API
    54. 【Android教程】图片资源:Drawable
    54. 【Android教程】图片资源:Drawable
    93 0
    |
    7月前
    |
    Android开发
    Android源代码定制:Overlay目录定制|调试Overlay资源是否生效
    Android源代码定制:Overlay目录定制|调试Overlay资源是否生效
    322 0
    |
    Java Android开发
    Android 保存资源图片到相册最新写法适用于Android10.0及以上
    Android 保存资源图片到相册最新写法适用于Android10.0及以上
    854 0
    |
    编解码 监控 前端开发
    Android平台GB28181设备接入端如何降低资源占用和性能消耗
    Android平台GB28181设备接入端如何降低资源占用和性能消耗?
    |
    Java Android开发
    Android 中使用数组资源文件定义数组
    Android 中使用数组资源文件定义数组
    164 0
    |
    存储 Android开发 开发者
    关于安卓媒体资源变动监听(ContentResolver)应用
    关于安卓媒体资源变动监听(ContentResolver)应用
    302 0
    |
    XML 编解码 C#
    .NET MAUI 安卓 UI 资源设置
    本文主要介绍使用 MAUI 开发安卓应用时,如何更换和处理 UI 资源:应用名称,图标,主题配色,状态栏,闪屏。
    626 0
    .NET MAUI 安卓 UI 资源设置
    |
    XML Java Android开发
    【Android】正确使用资源res文件
    首先有的UI改颜色,没用,发现无法更改按钮背景颜色。 我的AS下载的是最新版本,Button按钮的背景颜色一直都是亮紫色,无法更改。 为什么呢? 首先在你的清单文件中看你应用的是哪个主题。
    385 0