java.lang.Object |
? |
android.graphics.drawable.Drawable |
|
? |
android.graphics.drawable.ClipDrawable |
Class Overview
A Drawable that clips another Drawable based on this Drawable's current level value. 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, by increasing the drawable's level with setLevel()
.
Note: The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.
It can be defined in an XML file with the <clip>
element. For more information, see the guide to Drawable Resources.
ClipDrawable是Drawable的子类,我通过它可以设置Drawable的clip.
构造函数
:
Public Constructors |
|
ClipDrawable( Drawable drawable, int gravity, int orientation) 参数 orientation Bitwise-or of HORIZONTAL and/or VERTICAL |
主要函数有:
public final boolean setLevel (int level)
Since: API Level 1
Specify the level for the drawable. This allows a drawable to vary its imagery based on a continuous controller,
for example to show progress or volume level.
If the new level you are supplying causes the appearance of the Drawable to change, then it is responsible for calling invalidateSelf() in order to have itself redrawn, and true will be returned from this function.
Parameters
level The new level, from 0 (minimum) to 10000 (maximum).
Returns
* Returns true if this change in level has caused the appearance of the Drawable to change (hence requiring an invalidate), otherwise returns false.
注意 :必须使用setLevel来设置cliping,注意The new level, from 0 (minimum) to 10000 (maximum).
该方法继承于Drawable。
ClipDrawable的使用
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"?>
<
clip
xmlns:android
="http://schemas.android.com/apk/res/android"
android:drawable
="@drawable/drawable_resource"
android:clipOrientation
=["horizontal" | "vertical"]
android:gravity
=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"] />
示例1 :
attributes:
xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
名字空间是固定的"
http://schemas.android.com/apk/res/android"
android:drawable
Drawable resource. Required. Reference to a drawable resource to be clipped.
clipped所操作的drawable资源
android:clipOrientation
Keyword. The orientation for the clip.
Must be one of the following constant values:
Value Description
horizontal Clip the drawable horizontally.
vertical Clip the drawable vertically.
clip区域可变化的方向horizontally或vertically
android:gravity
Keyword. Specifies where to clip within the drawable.
Must be one or more (separated by '|') of the following constant values:
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.
Drawable采用上对齐的方式。Drawable的大小不变。如果clipOrientation为"vertical"的话,则clipping只能向下
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.
Drawable采用下对齐的方式。Drawable的大小不变。如果clipOrientation为"vertical"的话,则clipping只能向上
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.
Drawable采用左对齐的方式。Drawable的大小不变。如果clipOrientation为"horizontal"的话,则clipping只能向右。 它是默认的对齐方式。
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.
Drawable采用右对齐的方式。Drawable的大小不变。如果clipOrientation为"horizontal"的话,则clipping只能向左。
center_vertical Place object in the vertical center of its container, not changing its size.
Clipping behaves the same as when gravity is "center".
Drawable的vertical方向居中对齐。Drawable的大小不变。
Clipping行为和"center"在clipOrientation时"vertical"一样。
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).
Drawable的vertical方向居中对齐。Drawable的大小会变化,
它在vertical方向要stretch,以便在vertical方向填充它所在的容器。
vertical方向没Clipping行为除非the drawable level is 0,这时它不可见。
center_horizontal Place object in the horizontal center of its container, not changing its size.
Clipping behaves the same as when gravity is "center".
Drawable的horizontal方向居中对齐。Drawable的大小不变。
Clipping行为和"center"在clipOrientation时"horizontal"一样。
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).
Drawable的horizontal方向居中对齐。Drawable的大小会变化,
它在horizontal方向要stretch,以便在vertical方向填充它所在的容器。
horizontal方向没Clipping行为除非the drawable level is 0,这时它不可见。
center Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
When clipOrientation is "horizontal", clipping occurs on the left and right.
When clipOrientation is "vertical", clipping occurs on the top and bottom.
Drawable在horizontal和vertical方向都居中对齐 。
当clipOrientation是"horizontal"时,clipping发生在左右方向。
当clipOrientation是"vertica"时,clipping发生在上下方向。
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).
Drawable在horizontal和vertical方向都就进行stretch以便填充它所在的容器。它没有clipping行为。
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.
附加选项
注意1 :在程序中使用fill_vertical和center_vertical,发现他们居然没差别,无论手动的画,还通过控件画。
fill_horizontal和center_horizontal同理。
注意2 :上面关于ClipDrawable的大小的讨论(来自文档)在实际使用发现
毫无意义 ,
在实际的程序中,系统是先把Drawable发到它的Bounds或容器大小时,才来讨论的clipping问题
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">
</
clip
>
在文档中居然是用的"shape",而非"clip".经验证"clip"才对,“shape”是错误的。
文档居然也有种低级错误。
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:
示例2:
loading.xml文件
<?xml version="1.0" encoding="utf-8"?>
<
clip
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/load"
android:clipOrientation="horizontal"
android:gravity="fill_vertical" >
</
clip
>
代码1:
ClipDrawable loadDrawable;
int loadDrawableLevel=0;
final static int levelMax=10000;
代码2:
l
oadDrawable=(
ClipDrawable
)context.getResources().getDrawable(R.drawable.loading);
代码3:
Rect bounds=null;
if
(loadDrawableLevel<levelMax)
{
loadDrawableLevel+=10;;
loadDrawable.setLevel(loadDrawableLevel);
bounds =new Rect(0,0,Game.width,Game.height);
loadDrawable.setBounds(bounds);
}
else
loadDrawableLevel=0;
loadDrawable.draw(canvas);
参考资料:http://developer.android.com/guide/topics/resources/drawable-resource.html