`
jguangyou
  • 浏览: 375683 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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"?>
<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:

Value Description
horizontal Clip the drawable horizontally.
vertical Clip 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:

Value Description
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.

  • 大小: 2.4 KB
分享到:
评论

相关推荐

    Android中Drawable分类汇总

    - ClipDrawable:用于裁剪其他Drawable资源的Drawable。 - ScaleDrawable:用于缩放其他Drawable资源的Drawable。 - StateListDrawable:可以包含多种状态(如选中、按下等),根据不同的状态显示不同的Drawable。 -...

    android安卓APP之13种Drawable位图片资源.zip_android客户端是什么

    本资源包“android安卓APP之13种Drawable位图片资源.zip”涵盖了Android开发中常见的13种Drawable类型,这些类型极大地丰富了Android应用的视觉表现和动态效果。下面将详细介绍每种Drawable的特性和用法。 1. **...

    drawable(图片).zip

    本篇文章将深入探讨Android中的Drawable资源及其在压缩包"drawable(图片).zip"中的应用。 首先,Drawable是Android系统中用于表示图形对象的接口,它可以是位图(Bitmap)、形状(Shape)、动画(Animation)等。在...

    Android ClipDrawable 实现进度条效果

    在Android开发中,为了创建各种视觉效果,开发者经常会利用到Drawable资源。`ClipDrawable`是Android SDK提供的一种Drawable类型,它允许我们根据指定的级别值来裁剪其他Drawable,从而实现如进度条、滑块等效果。在...

    android drawable下的xml详解

    **ColorDrawable**是最简单的`drawable`类型之一,它用于表示一个单一颜色的绘制区域。在XML布局文件中,可以通过`&lt;color&gt;`标签来定义一个颜色资源,并通过`android:color`属性指定具体的颜色值。例如,以下XML代码...

    clipdrawable 的使用

    在XML资源文件中,你可以使用`&lt;clip&gt;`标签来创建一个`ClipDrawable`。例如: ```xml &lt;bitmap android:src="@drawable/your_image" /&gt; |bottom" /&gt; ``` 其中,`android:src`指定要剪裁的图片,`clip-...

    Android中不同类型的Drawable使用

    BitmapDrawable是最基础的Drawable类型,用于展示位图资源,比如从本地文件、网络或资源ID加载的图片。可以通过设置BitmapDrawable的BitmapOptions来调整缩放、裁剪和过滤等参数。 2. **Shape Drawable** Shape ...

    ClipDrawable图片徐徐展开

    其中,`@drawable/clip_drawable`应是一个XML资源,定义了`ClipDrawable`,如下所示: ```xml &lt;bitmap android:src="@drawable/your_image" /&gt; &lt;clip-android:drawablePadding="5dp" /&gt; ``` 在这里,`...

    玩转Android之Drawable的使用

    【玩转Android之Drawable的使用】 Drawable在Android开发中扮演着至关重要的角色,它是图形和图像的基础元素,广泛用于UI设计和自定义视图。在Android中,Drawable不仅仅局限于简单的图片,还包括各种复杂的图形和...

    ResourceTypeDrawable.rar

    在Android开发中,Drawable资源是不可或缺的一部分,它们用于定义UI元素的外观和行为。"ResourceTypeDrawable.rar" 文件很可能是包含一系列示例,演示了如何在Android应用中使用和操作Drawable资源。在这个压缩包中...

    Android图像介绍-Drawable

    在Android开发中,Drawable是图像资源的一个核心概念,它不仅限于普通的图片,还包括各种图形、状态列表、层叠图像等。本篇文章将深入探讨Android中的Drawable及其在图像处理中的应用。 首先,Drawable是一个接口,...

    通过ClipDrawable 实现茶杯注满效果

    首先,`ClipDrawable`是`Drawable`的一个子类,它可以根据指定的方向来“剪切”或显示其内容的一部分。默认情况下,`ClipDrawable`的方向是顶部到底部,这正好符合茶杯注满的场景。你可以通过设置`level`属性来控制...

    使用ClipDrawable做的电池充电效果的Demo

    然后,通过`ClipDrawable`的构造函数将这个图像资源转换为可裁剪的Drawable对象。构造函数接受三个参数:资源ID、裁剪方向(可以是上、下、左、右四个方向)和裁剪级别(0-100,0代表完全隐藏,100代表完全显示)。 ...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    6.4、使用Drawable资源:图片资源; StateListDrawable资源; LayerDrawable资源; ShapeDrawable资源; ClipDrawable资源; AnimationDrawable资源; 6.5、使用原始XML资源: 6.6、使用Layout资源: 6.7、使用菜单...

    WorkingWithDrawables

    attr/selectableItemBackground"`属性为View提供触摸反馈效果,无需额外的Drawable资源。 10. **Drawable的兼容性问题** 考虑到Android版本和设备差异,应使用`AppCompat`库中的`AppCompatDrawableManager`和`...

    Android:Drawbale

    6. ClipDrawable:剪切图,可以按照指定的方向裁剪`Drawable`。 7. ScaleDrawable:缩放图,可以动态地改变`Drawable`的大小。 8. AnimationDrawable:动画图,可以播放一系列的`Drawable`来创建帧动画效果。 二...

    Android实例代码

    6.4、使用Drawable资源:图片资源; StateListDrawable资源; LayerDrawable资源; ShapeDrawable资源; ClipDrawable资源; AnimationDrawable资源; 6.5、使用原始XML资源: 6.6、使用Layout资源: 6.7、使用菜单...

    疯狂Android讲义源码

     6.4.5 ClipDrawable资源 231  6.4.6 AnimationDrawable资源 233  6.5 使用原始XML资源 236  6.5.1 定义原始XML资源 236  6.5.2 使用原始XML文件 237  6.6 使用布局(Layout)资源 239  6.7 使用菜单(Menu)...

Global site tag (gtag.js) - Google Analytics