加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:00:23 所属栏目:百科 来源:网络整理
导读:此篇主要介绍Transition Drawable,Inset Drawable和Clip Drawable。 前言 这已经是【ADNROID XML图像资源文件详细讲解】第4篇了,安卓提供了很多个图像处理的对象,如果运用的好,真的是可以事半功倍。 比如下面的效果大家想想怎么做: 不知道怎么做的,可

此篇主要介绍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>
效果如下:




总结

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读