---
title: "Drawable资源之ScaleDrawable资源"
author: "jguangyou"
date: "2014-02-17T14:25:02Z"
tags: []
categories: ["移动开发"]
slug: "drawable-scaledrawable"
draft: false
lastmod: "2014-02-17T14:25:02Z"
id: 2018181
url: "https://deep-fish.iteye.com/blog/2018181"
---

A drawable defined in XML that changes the size of another drawable based on its current level.

file location:

`res/drawable/filename.xml`  
The filename is used as the resource ID.

compiled resource datatype:

Resource pointer to a `ScaleDrawable`.

resource reference:

In Java: `R.drawable.filename`  
In XML: `@[package:]drawable/filename`

syntax:

```
<?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" />
```

 

elements:

`<scale>`

Defines the scale drawable. This must be the root element.

attributes:

`xmlns:android`

*String*. **Required.** Defines the XML namespace, which must be`"http://schemas.android.com/apk/res/android"`.

`android:drawable`

*Drawable resource*. **Required**. Reference to a drawable resource.

`android:scaleGravity`

*Keyword*. Specifies the gravity position after scaling.

Must be one or more (separated by '|') of the following constant values:

Value Description `top` Put the object at the top of its container, not changing its size. `bottom` Put the object at the bottom of its container, not changing its size. `left` Put the object at the left edge of its container, not changing its size. This is the default. `right` Put the object at the right edge of its container, not changing its size. `center_vertical` Place object in the vertical center of its container, not changing its size. `fill_vertical` Grow the vertical size of the object if needed so it completely fills its container. `center_horizontal` Place object in the horizontal center of its container, not changing its size. `fill_horizontal` Grow the horizontal size of the object if needed so it completely fills its container. `center` Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. `fill` Grow the horizontal and vertical size of the object if needed so it completely fills its container. `clip_vertical` Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. `clip_horizontal` Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges.

`android:scaleHeight`

*Percentage*. The scale height, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc.

`android:scaleWidth`

*Percentage*. The scale width, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc.

example:

```
<?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%"
    android:scaleWidth="80%" />
```