Drawable学习之-----ScaleDrawable

本文详细介绍了Android中ScaleDrawable的使用方法及属性配置,包括如何通过XML定义一个可缩放的图片资源,并展示了具体的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对另一个drawable资源,基于当前的level,进行尺寸变换的drawable。

文件位置:
res/drawable/filename.xml
文件名即资源名
编译数据类型:
指向  ScaleDrawable的指针。
资源引用:
In Java:  R.drawable.filename
In XML:  @[package:]drawable/filename
语法:
<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:scaleHeight="percentage"
    android:scaleWidth="percentage" />
元素:
<scale>
定义一个ScaleDrawable,必须作为根元素。

属性:

xmlns:android
String类型。 必须的。定义XML文件的命名空间。必须是  "http://schemas.android.com/apk/res/android".
android:drawable
Drawable 资源。必须的。引用一个drawable资源。
android:scaleGravity
关键字。指定缩放后的gravity的位置。

必须是下面的一个或多个值(多个值之间用”|“分隔),下面的值和描述和上一篇的ClipDrawable一样。

描述
topPut the object at the top of its container, not changing its size.
bottomPut the object at the bottom of its container, not changing its size.
leftPut the object at the left edge of its container, not changing its size. This is thedefault.
rightPut the object at the right edge of its container, not changing its size.
center_verticalPlace object in the vertical center of its container, not changing its size.
fill_verticalGrow the vertical size of the object if needed so it completely fills its container.
center_horizontalPlace object in the horizontal center of its container, not changing its size.
fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container.
centerPlace the object in the center of its container in both the vertical and horizontal axis, notchanging its size.
fillGrow the horizontal and vertical size of the object if needed so it completely fills itscontainer.
clip_verticalAdditional option that can be set to have the top and/or bottom edges of the child clipped toits container's bounds. The clip is based on the vertical gravity: a top gravity clips thebottom edge, a bottom gravity clips the top edge, and neither clips both edges.
clip_horizontalAdditional option that can be set to have the left and/or right edges of the child clipped toits container's bounds. The clip is based on the horizontal gravity: a left gravity clipsthe right edge, a right gravity clips the left edge, and neither clips both edges.
android:scaleHeight
Percentage(百分比)缩放的高度,以百分比的方式表示drawable的缩放。形式例如:100%,12.5%。
android:scaleWidth
Percentage(百分比)缩放的宽度,以百分比的方式表示drawable的缩放。形式例如:100%,12.5%。
示例:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%" //表示从高度80% 开始缩放,80%为图片高度最小值。level 0不可见。
    android:scaleWidth="80%"/> //表示从宽度80% 开始缩放,80%为图片宽度最小值。level 0不可见。
参考:


示例:

在scale.xml中:

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:scaleWidth="50%"  
  4.     android:scaleHeight="50%"  
  5.     android:drawable="@drawable/image1"   
  6.     android:scaleGravity="center_vertical|center_horizontal"  
  7.     >  
  8. </scale>  

在layout中使用:

[java]  view plain  copy
  1. <ImageView   
  2.     android:id="@+id/imgView"  
  3.     android:src="@drawable/scale"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"/>  
在 Android 开发中,`drawable-hdpi-1920x1080` 这样的资源目录名称是用于提供更精细的屏幕适配能力的一部分。Android 系统支持多种屏幕尺寸和密度,因此开发者可以为不同设备提供不同的资源文件以获得最佳显示效果。 ### 目录用途 `drawable-hdpi-1920x1080` 目录的用途是为具有 **高密度屏幕(hdpi)** 且 **分辨率为 1920x1080** 的设备提供特定的图像资源。这样的目录命名方式属于 Android 的资源目录限定符机制的一部分,它允许开发者根据设备的屏幕特性(如分辨率、密度、方向等)加载最合适的资源[^2]。 通过这种方式,应用可以在不同分辨率的设备上保持图像的清晰度和 UI 布局的美观性。例如,某些高端手机或平板设备可能具有 1920x1080 的分辨率并且属于 hdpi 密度级别,使用该目录中的资源可以确保图像在这些设备上以最佳方式呈现。 ### 使用指南 要正确使用 `drawable-hdpi-1920x1080` 目录,请遵循以下步骤: 1. **创建目录结构**:在 `res` 目录下创建名为 `drawable-hdpi-1920x1080` 的目录。注意目录名称的格式必须严格符合 Android 资源目录命名规则。 2. **放置资源文件**:将适用于 1920x1080 分辨率和 hdpi 密度的图像资源放入该目录中。这些图像通常是为高分辨率屏幕优化的高清图片。 3. **引用资源**:在布局文件或代码中引用这些资源时,不需要指定具体的目录,只需使用通用的资源引用方式,例如 `@drawable/image_name`。系统会根据当前设备的屏幕特性自动选择最合适的资源文件[^2]。 4. **测试适配性**:使用 Android 模拟器或真实设备测试应用在不同分辨率和密度下的显示效果,确保资源被正确加载并保持良好的 UI 布局[^3]。 ### 示例代码 以下是一个简单的示例,展示如何在 XML 布局文件中引用图像资源: ```xml <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/example_image" /> ``` 在这个例子中,`example_image` 是一个图像资源,它应该存在于多个 `drawable` 目录中,包括 `drawable-hdpi-1920x1080`,以确保在不同设备上都能正确显示。 ### 注意事项 - **目录命名规则**:Android 对资源目录的命名有严格的规则,目录名称中的限定符必须按照特定顺序排列,并且不能包含非法字符。 - **避免重复资源**:尽量避免在多个目录中放置相同的资源文件,以减少维护成本和潜在的冲突。 - **使用支持的限定符**:确保使用 Android 支持的限定符组合,例如 `hdpi`、`xhdpi`、`nodpi` 等,并根据需要添加分辨率限定符,如 `1920x1080`[^2]。 通过合理使用 `drawable-hdpi-1920x1080` 目录,开发者可以更有效地管理应用的图像资源,从而提升应用在不同设备上的显示质量和用户体验。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优雅的心情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值