ADNROID XML图像资源文件详细讲解四

此篇主要介绍Transition Drawable,Inset Drawable和Clip Drawable。

前言

这已经是【ADNROID XML图像资源文件详细讲解】第4篇了,安卓提供了很多个图像处理的对象,如果运用的好,真的是可以事半功倍。
比如下面的效果大家想想怎么做:



不知道怎么做的,可以接着往下看。

一、Transition Drawable

此对象是2种图片或者颜色之间的交叉显示。即2张图片同时进行fade in和fade out交叉显示的过程。需要注意的是这个效果是在同一个对象中(比如同一个ImageView对象)完成。
效果如下:



语法格式

<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition>

例子


res/drawable/transition.xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/orange"></item>
<item android:drawable="@drawable/green"></item>
</transition>
说明:
<transition>中的<item>只有前2个有效,多余的都是无效的。所以只需要准备2个item就行。

Activity中的代码如下:
 ImageView imageView = (ImageView) findViewById(R.id.image01);
        TransitionDrawable transitionDrawable = (TransitionDrawable) imageView.getDrawable();
        transitionDrawable.startTransition(3000);//3000毫秒,表示效果持续3000毫秒
image01为:
<ImageView
        android:id="@+id/image01"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/transition"/>


二、Inset Drawable

此对象使用很简单。实际开发中的使用情况也仅仅是在需要改变控件背景图大小时使用。

语法格式

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

需要注意的是此处是android:insetTop,不是android:top。
使用时直接赋值给background属性值就OK。

例子

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/background"
    android:insetTop="10dp"
    android:insetLeft="10dp" />


三、Clip Drawable

此对象就是我们开头前言中的效果实现者。此对象在实际开发中大多都用在跟Progress Bar类似的控件和功能中。比如SeekBar,ProgressBar等,也可以用在ImageView等对象中。废话不多说,看看怎么使用。

语法格式

<?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"] />

语法解析

android:clipOrientation 移动的方向。可以垂直和水平方向移送。
android:gravity Drawable对象所在容器的位置。多个取值可以用“|”隔开。

例子

R.layout.activity_main
 <ImageView
        android:id="@+id/image01"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/clip_drawable"/>
    <SeekBar
        android:id="@+id/progressbar01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="10000"/>
activity的代码
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SeekBar seekBar = (SeekBar) findViewById(R.id.progressbar01);
        ImageView imageView = (ImageView) findViewById(R.id.image01);
        final ClipDrawable clipdrawable = (ClipDrawable) imageView.getDrawable();//需要转为ClipDrawable对象
        
        seekBar.setonSeekBarchangelistener(new SeekBar.OnSeekBarchangelistener() {
            @Override
            public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
                clipdrawable.setLevel(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        



res/drawable/clip.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/qq123"
    android:clipOrientation="horizontal">
</clip>
效果如下:




总结

以上代码都很简单,使用过程也很简单。但是能帮助解决大问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇