Drawable资源之ClipDrawable资源

本文介绍如何利用Android中的ClipDrawable资源实现动态显示效果,如进度条等。通过设置不同属性,可以控制子Drawable被裁剪的程度及位置。

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

A drawable defined in XML that clips another drawable based on this Drawable's current level. You can control how much the child drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars.

file location:
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a   ClipDrawable.
resource reference:
In Java:   R.drawable.filename
In XML:   @[package:]drawable/filename
syntax:
<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:insetTop="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension" />
  elements:
<clip>
Defines the clip 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 to be clipped.
android:clipOrientation
Keyword. The orientation for the clip.

Must be one of the following constant values:

ValueDescription
horizontalClip the drawable horizontally.
verticalClip the drawable vertically.
android:gravity
Keyword. Specifies where to clip within the drawable.

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

ValueDescription
top

Put the object at the top of its container, not changing its size.

When clipOrientation is "vertical", clipping occurs at

the bottom of the drawable.

bottom

Put the object at the bottom of its container, not changing its size.

When clipOrientation is "vertical", clipping occurs

at the top of the drawable.

left

Put the object at the left edge of its container, not changing its size.

This is the default. When clipOrientation is "horizontal",

clipping occurs at the right side of the drawable. This is the default.

right

Put the object at the right edge of its container, not changing its size.

When clipOrientation is "horizontal",

clipping occurs at the left side of the drawable.

center_vertical

Place object in the vertical center of its container, not changing its size.

Clipping behaves the same as when gravity is "center".

fill_vertical

Grow the vertical size of the object if needed so it completely fills its

container. When clipOrientation is "vertical",

no clipping occurs because the drawable fills the vertical space

(unless the drawable level is 0, in which case it's not visible).

center_horizontal

Place object in the horizontal center of its container, not changing

its size. Clipping behaves the same as when gravity is "center".

fill_horizontal

Grow the horizontal size of the object if needed so it completely

fills its container. When clipOrientation is "horizontal",

no clipping occurs because the drawable fills the horizontal space

(unless the drawable level is 0, in which case it's not visible).

center

Place the object in the center of its container in both the vertical

and horizontal axis, not changing its size.

When clipOrientationis "horizontal",

clipping occurs on the left and right. WhenclipOrientation is "vertical", clipping occurs on the top and bottom.

fill

Grow the horizontal and vertical size of the object if needed so it

completely fills its container. No clipping occurs because the drawable

fills the horizontal and vertical space (unless the drawable level is 0,

in which case it's not visible).

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.

example:
XML file saved at   res/drawable/clip.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/android"
    android:clipOrientation="horizontal"
    android:gravity="left" />

 

The following layout XML applies the clip drawable to a View:

<ImageView
    android:id="@+id/image"
    android:background="@drawable/clip"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

 

The following code gets the drawable and increases the amount of clipping in order to progressively reveal the image:

ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);

 

Increasing the level reduces the amount of clipping and slowly reveals the image. Here it is at a level of 7000:



 

Note: The default level is 0, which is fully clipped so the image is not visible. When the level is 10,000, the image is not clipped and completely visible.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值