您的位置:首页

Drawable资源的初步使用

2017-07-30 14:04 232 查看
刚開始接触到Android的时候,看到类似以下的一个Button:



当时感觉这种button有点像Material Design风格。真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁剪好的图片,仅仅是用xml来定义我们想要的图片,如今想想真的是挺搞笑,哈哈。实现这种图片我们能够自己定义view,当然啦。最简单的还是用xml文件来定义!

如今看来,实现上面的效果。真的是太简单啦:

观察上面的图片,是一个近似长方形,可是四个角又有点有点圆滑。

既然是Drawable资源,当然是在/res/drawable/下定义的啦,给出.xml文件:

shaper.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners  android:radius="5dp"/>
<size android:width="50dp" android:height="20dp" />
<solid android:color="@color/blue" />
</shape>
selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/shaper"/>
<item android:state_pressed="false"  android:drawable="@drawable/shaper"/>
</selector>
然后Button控件下使用 android:background="@drawable/seletor_button",布局文件非常easy,这里就不写出来了。

由selector.xml知,当松开button时。button的背景色也是蓝色,我们当然能够换成其它的颜色,那就要又一次定义一个shaper2.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners  android:radius="5dp"/>
<size android:width="50dp" android:height="20dp" />
<solid android:color="@color/red" />
</shape>
然后在selector.xml中:

<item android:state_pressed="false"  android:drawable="@drawable/shaper2"/>
效果:



或许你会问,上面的xml文件究竟表示的是什么呢。以下简单的说明Drawable资源。

一.图片资源

这个大家肯定不陌生。获得/res/drawable/下的图片(如:png,gif,jpg)。能够直接R.drawable.picture或者:

Resources resources = getResources();
Drawable drawable = resources.getDrawable(R.drawable.picture);
注意在命名这些图片时,不能以数字开头。否则会报错。图片资源使用比較简单。不具体阐述。

二.StateListDrawable资源

用于组织多个Drawable对象,当使用StateListDrawable作为目标组件的背景,前景图片时。StateListDrawable对象所显示的Drawable对象会随着目标组件状态的改变而自己主动切换。

类的继承层次关系:

java.lang.Object
↳ 	android.graphics.drawable.Drawable
↳ 	android.graphics.drawable.DrawableContainer
↳ 	android.graphics.drawable.StateListDrawable
怎样使用该类资源呢?官方文档上已经给出:

It can be defined in an XML file with the
<selector>
element. Each state Drawable is defined in a nested
<item>
element. For more information, see the guide toDrawable
Resources.

文档指出定义StateListDrawable对象的xml文件的根元素为<selector ../>,该元素能够包括多个<item />元素。

既然用XML文件定义。那么有哪些属性呢?(详细查阅API,这里就不进行大量Ctrl C/V操作了)



简单解释下上面的属性:

android:constantSize:

值为“true”表示随着状态变化,Drawable的大小保持不变(全部状态中最大的size);值为“false”表示大小会变化。默认是false。

android:color或者android:drawable:指定颜色或者Drawable对象

<item />支持的state_xxx状态如以下几种:

android:state_active:代表是否处于激活状态

android:state_checkable:代表是否处于可勾选状态

android:state_checked:代表是否处于已勾选状态

android:state_enabled:代表是否处于可用状态

android:state_first:代表是否处于開始状态

android:state_focused:代表是否处于已得到焦点状态

android:state_last:代表是否处于结束状态

android:state_middle:代表是否处于中间状态

android:state_pressed:代表是否处于已被按下的状态

android:state_selected:代表上是否处于被选中的状态

android:state_window_focused:代表窗体是否处于已得到焦点状态

详细用法还得在工作实践中总结,文章开篇已经简单的使用StateListDrawable资源文件。这里就不再举例。

三.ShapeDrawable资源

ShapeDrawable资源

ShapeDrawable用于定义一个主要的图形(如矩形,线条,圆形,椭圆等等),定义的XML文件的根元素是<shape .../>元素。该元素能够指定例如以下属性:

shape=["rectangle"|"oval"|"line"|"ring"]  指定图形形状
corners   定义几何图形的四个角的弧度
gradient  定义使用渐变色填充
padding   定义几何形状的内边距(能够分别指定top,bottom,right,left)
size      定义几何形状的大小
solid     定义使用单种颜色填充
stroke    定义为几何形状绘制边框
height    定义图片高度
width     定义图片的宽度
用法相对简单。不再举例说明。

四.ClipDrawable资源

ClipDrawable资源定义在一个XML中,表示从其它资源裁剪一个"图片片段"。在XML定义时使用<clip .../>作为根元素。

xml中的属性:

drawable:指定截取Drawable对象
clipOrientation:指定截取方向,能够设置垂直截取或者水平截取
gravity:指定截取时的对齐方式
当中gravity的取值有非常多。详细能够參考API这里就不贴上来了。

通过demo简单的说明下怎样利用ClipDrawable资源文件缓缓的展开一张图片的,clip.xml:

<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/psb"
android:clipOrientation="horizontal"
android:gravity="center"
/>


上面也不须要解释上面了吧,布局文件也非常easy,就是一个ImageView:

...
...
<ImageView
android:id="@+id/psb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/clip"/>
...
...
MainActivity.java:

public class MainActivity extends Activity {
@SuppressWarnings("unused")
private ClipDrawable clip;
Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case 0:
clip.setLevel(clip.getLevel()+200);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ImageView psg = (ImageView)findViewById(R.id.psb);
clip = (ClipDrawable)psg.getDrawable();
final Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
// TODO 自己主动生成的方法存根
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
if(clip.getLevel()>10000){
time.cancel();
}
}
},0,500);

}
}
看下效果。已经等的不耐烦了:



上面代码也非常easy,利用Timer定时器,每隔500毫秒时间利用Handler发送信息,改动clip的Level,以下说明下Level:

裁剪一个其它资源基于ClipDrawable资源的Level, 默认的Level值是0,表示图片被全然裁剪,因此图片是不可见的。当值达到10000时。图片不被裁剪,所以能够全然显示。

五.RippleDrawable资源
5.0后出现的ripple,使得我们更加方便的定义出波纹效果的控件。以下赶快来定义吧!(*^__^*)
嘻嘻……

首先还是看下基础的知识点,当然了,官方API文档是最好的资料:
既然是用XML文件定义的。那么有什么属性呢?
android:color 受到ripple影响时的颜色
android:radius ripple全然传播时的半径
不会就这两个属性吧!
哈哈,细致阅读文档知道,它的属性继承自LayerDrawable,LayerDrawable还没学习呢,也没关系,打开看看有哪些属性吧!哦呦,属性真多啊,只是都是我们常见的(仅仅截取部分属性。很多其它參考文档)



以下来写demo吧!在XML中自己定义出一个带波纹的button,当然离不了shaper,以下继续,

shaper.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="4dp" />
</shape>
rippler.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/pink">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/blue" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:drawable="@drawable/shaper" />
</ripple>
还有布局文件:

<Button
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:elevation="30dp"
android:background="@drawable/rippler" />

效果:



上面还指定了5.0引入的新属性android:elevation,使得组件“浮”起来的高度,通过该属性能够让该组件呈现3D效果,也就是Material Design风格。哈哈。

当然也能够设置android:translationZ(设置该组件在Z方向上的位移。这个属性多用在动画资源中) 来达到该种效果。还能够通过方法setElevation(float) , setTranslationZ(float)来设置。

上面仅仅是简单的介绍几种Drawable资源。还有LayerDrawable,AnimationDrawable。 LevelListDrawable等没有介绍。写文章的主要目的为了自己复习巩固知识,假设有幸帮助到他人,本人很开心!当然假设出现错误而影响到他人,这是自己不能容忍的错误!文章中若出现错误之处。恳请指正!

不甚感激。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: