Most visited

Recently visited

Added in API level 1

ClipDrawable

public class ClipDrawable
extends DrawableWrapper

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.DrawableWrapper
       ↳ android.graphics.drawable.ClipDrawable


Drawable基于此Drawable的当前级别值剪切另一个Drawable。 您可以控制Drawable子项在宽度和高度上根据等级裁剪的程度,以及控制放置在整个容器中的位置的重力。 通常用于实现诸如进度条之类的东西,方法是使用setLevel()增加drawable的级别。

注意:当drawable被完全剪裁并且在level为0时不可见,并且在level为10,000时完全透露。

它可以使用<clip>元素在XML文件中定义。 有关更多信息,请参阅Drawable Resources的指南。

Summary

XML attributes

android:clipOrientation The orientation for the clip. 
android:drawable Reference to a drawable resource to draw with the specified scale. 
android:gravity Specifies where to clip within the drawable. 

Constants

int HORIZONTAL

int VERTICAL

Public constructors

ClipDrawable(Drawable drawable, int gravity, int orientation)

用指定的重力和方向创建一个可绘制的新剪辑。

Public methods

void applyTheme(Resources.Theme t)

将指定的主题应用于此Drawable及其子项。

void draw(Canvas canvas)

在其边界(通过setBounds设置)中绘制,考虑可选效果,如alpha(通过setAlpha设置)和color filter(通过setColorFilter设置)。

int getOpacity()

返回此Drawable的不透明度/透明度。

void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

从可选的由主题设计的XML资源中扩展此Drawable。

Protected methods

boolean onLevelChange(int level)

如果您根据级别而变化,请在您的子类中覆盖此内容以更改外观。

Inherited methods

From class android.graphics.drawable.DrawableWrapper
From class android.graphics.drawable.Drawable
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback

XML attributes

android:clipOrientation

剪辑的方向。

必须是以下常量值中的一个或多个(用'|'分隔)。

Constant Value 描述
horizontal 1 Clip the drawable horizontally.
vertical 2 Clip the drawable vertically.

这对应于全局属性资源符号 clipOrientation

android:drawable

引用可绘制资源以指定的比例绘制。

必须是另一个资源的引用,其形式为“ @[+][package:]type:name ”,或者其形式为“一个主题属性 ?[package:][type:]name ”。

这对应于全局属性资源符号 drawable

android:gravity

指定绘图内的剪辑位置。 缺省值为左侧。

必须是以下常量值中的一个或多个(用'|'分隔)。

Constant Value 描述
top 0x30 Push object to the top of its container, not changing its size.
bottom 0x50 Push object to the bottom of its container, not changing its size.
left 0x03 Push object to the left of its container, not changing its size.
right 0x05 Push object to the right of its container, not changing its size.
center_vertical 0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical 0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal 0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal 0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center 0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill 0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical 0x80 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 will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal 0x08 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 will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
start 0x00800003 Push object to the beginning of its container, not changing its size.
end 0x00800005 Push object to the end of its container, not changing its size.

这对应于全局属性资源符号 gravity

Constants

HORIZONTAL

Added in API level 1
int HORIZONTAL

常数值:1(0x00000001)

VERTICAL

Added in API level 1
int VERTICAL

常量值:2(0x00000002)

Public constructors

ClipDrawable

Added in API level 1
ClipDrawable (Drawable drawable, 
                int gravity, 
                int orientation)

用指定的重力和方向创建一个可绘制的新剪辑。

Parameters
drawable Drawable: the drawable to clip
gravity int: gravity constant (see Gravity used to position the clipped drawable within the parent container
orientation int: bitwise-or of HORIZONTAL and/or VERTICAL

Public methods

applyTheme

Added in API level 21
void applyTheme (Resources.Theme t)

将指定的主题应用于此Drawable及其子项。

Parameters
t Resources.Theme: the theme to apply

draw

Added in API level 1
void draw (Canvas canvas)

在其边界(通过setBounds设置)中绘制,考虑可选效果,如alpha(通过setAlpha设置)和color filter(通过setColorFilter设置)。

Parameters
canvas Canvas: The canvas to draw into

getOpacity

Added in API level 1
int getOpacity ()

返回此Drawable的不透明度/透明度。 返回的值是在抽象格式常数之一PixelFormatUNKNOWNTRANSLUCENTTRANSPARENT ,或OPAQUE

OPAQUE drawable是一个绘制所有内容的范围,完全覆盖drawable后面的所有内容。 一个TRANSPARENT drawable是一个在其范围内没有绘制任何东西的图形,允许它后面的所有东西都显示出来。 TRANSLUCENT drawable是任何其他状态中的drawable,drawable将绘制其边界内的一些内容,但不是全部内容,并且至少drawable后面的某些内容将可见。 如果无法确定可绘制内容的可见性,则最安全/最佳返回值为TRANSLUCENT。

一般而言,Drawable应该尽可能保守并且返回值。 例如,如果它包含多个子drawable,并且一次只显示其中一个,如果只有一个子项是TRANSLUCENT,而其他子项是OPAQUE,则应返回TRANSLUCENT。 您可以使用方法resolveOpacity(int, int)将两个不透明度标准减少到相应的单个输出。

请注意,返回的值不一定会考虑客户端通过setAlpha(int)setColorFilter(ColorFilter)方法应用的自定义alpha或颜色过滤器。 一些子类,比如BitmapDrawableColorDrawable ,并GradientDrawable ,对价值做账setAlpha(int) ,但总的行为是依赖于子类实现的。

Returns
int int The opacity class of the Drawable.

inflate

Added in API level 21
void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

从可选的由主题设计的XML资源中扩展此Drawable。 对于每个Drawable,这不能被调用一次以上。 请注意,框架可能已经调用过这一次从XML资源创建Drawable实例。

Parameters
r Resources: Resources used to resolve attribute values
parser XmlPullParser: XML parser from which to inflate this Drawable
attrs AttributeSet: Base set of attribute values
theme Resources.Theme: Theme to apply, may be null
Throws
XmlPullParserException
IOException

Protected methods

onLevelChange

Added in API level 1
boolean onLevelChange (int level)

如果您根据级别而变化,请在您的子类中覆盖此内容以更改外观。

Parameters
level int
Returns
boolean Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

Hooray!