android:clipOrientation有两个属性,默认为horizontal
android:gravity有两个属性,默认为left
android:gravity有两个属性,默认为left
ClipDrawable代表从其它位图上截取一个“图片片段”。在XML文件中使用<clip.../>元素定义ClipDrawable对象,可指定如下三个属性:
- android:drawable:指定截取的源Drawable对象
- android:clipOrientation:指定截取的方向,可设置为水平截取或垂直截取
- android:gravity:指定截取时的对齐方式
android:clipOrientation
和
android:gravity
是相对应的例如:
当我们用
vertical
的时候gravity的属性可以设置为top 或bottom 这样图片就可以从顶部向下展开或者从底部向上展开
同样
horizontal
的时候
gravity的属性可以设置为left或right
这样图片就可以从左向右展开或者从又向左展开
但是值得注意的是
android:clipOrientation
的属性为horizontal的时候 grivaty的属性top和bottom 是失效的不会从上向下展开也不会从下向上展开会默认从中间向两边展开
同样
android:clipOrientation
的属性为vertical的时候 grivaty的属性left和right也是这样的
上代码
自定义drawable 设置ClipDrawable属性 引用一张你想要切割的图片
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
clip
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:clipOrientation
=
"horizontal"
android:drawable
=
"@drawable/clip_pic"
android:gravity
=
"right"
>
</
clip
>
界面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/clip" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/clip" />
</LinearLayout>
代码实现
private
ImageView
view
;
@Override
protected
void
onCreate(Bundle
savedInstanceState
) {
//
TODO
Auto-generated method stub
super
.onCreate(
savedInstanceState
);
setContentView(R.layout.
activity_main
);
view
= (ImageView)findViewById(R.id.
view
);
// 获取图片所显示的ClipDrawable对象
final
ClipDrawable
drawable
= (ClipDrawable)
view
.getDrawable();
final
Handler
handler
=
new
Handle r() {
@Override
public
void
handleMessage(Message
msg
) {
// 如果消息是本程序发送的
if
(
msg
.
what
== 0x123) {
// 修改ClipDrawable的level值
drawable
.setLevel(
drawable
.getLevel() + 200);
}
}
};
final
Timer
timer
=
new
Timer();
timer
.schedule(
new
TimerTask() {
@Override
public
void
run() {
Message
msg
=
new
Message();
msg
.
what
= 0x123;
// 发送消息,通知应用修改ClipDrawable的level的值
handler
.sendMessage(
msg
);
// 取消定时器
if
(
drawable
.getLevel() >= 10000) {
timer
.cancel();
}
}
}, 0, 300);
}
预期效果(从右向左)