系统的Drawable(一)

系统的Drawable(一)

学习自

《Android 开发艺术探索》
《官方文档》
http://www.javashuo.com/article/p-ofyhrlzz-go.html
http://www.runoob.com/w3cnote/android-tutorial-drawable1.htmlhtml

Drawable漫谈

首先学习一个东西,先找找最权威的解释,固然是官方文档了,我先先来看看官方文档是如何描述Drawable的.android

Provides classes to manage a variety of visual elements that are intended for display only, such as bitmaps and gradients. These elements are often used by widgets as background images or simply as indicators (for example, a volume level indicator).
提供一些类来管理一系列的仅用来显示的可视化元素,好比bitmap和渐变. 这些元素常常被控件用做背景或者简单地做为指示器(Indicator)(好比说,音量指示器)。
You can create most of these drawables using XML, as described in Drawable Resources.
你能够经过xml在Drawable目录下声明大多数的Drawableide

如今咱们对Drawable有了一个大概的认识,即Drawable时一种可视化资源,好比说简单的颜色,图片,Shape等,同时Drawable也是一个类,可是这个类的实例能够经过Xml文件的形式来建立。Drawable类是一个抽象类,位于 android.graphics.drawable 包下,全部的Drawable都继承于它。经过合理的使用Drawable咱们能够减小对图片的依赖,减少APK的体积。学习

Drawable能够经过代码定义,也能够经过Xml文件定义,可是代码定义比较繁琐也不利于复用,因此咱们通常习惯经过XML文件来定义Drawable。ui

Google 提供了多种Drawable,接下来咱们一一学习它们。google

Android系统的Drawable概览

ColorDrawable

ColorDrawable仅仅表明的是一种颜色,因此也比较简单。设计

<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FFF" />

经过代码定义3d

var colorDrawable = ColorDrawable(0xFFF)

GradientDrawable

GradientDrawable,表明的是一个渐变色,会与 Shape 结合着用,Shape定义形状 GradientDrawable 定义渐变颜色.XML标签是 gradient .code

渐变类型的图例cdn

图片.png | left | 401x142
angle的图例

图片.png | left | 340x282

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:centerColor="#5F3f"
        android:centerX="0.2"
        android:centerY="0.2"
        android:endColor="#000"
        android:startColor="#FFF"
        android:type="linear" />
</shape>

BitmapDrawable

BitmapDrawable就是一个Bitmap,使用的时候直接引用Bitmap便可,可是经过使用XML来描述Bitmap能够设置更多的效果.BitmapDrawable的属性以下:

属性 描述
scr 图片资源,该属性是必须设置的
antialias 是否开启抗锯齿,开启后会让图片变的平滑,建议开启
dither 抖动效果,当高质量的图片显示在低质量的屏幕上的时候,不会过于失真,建议开启
filter 过滤效果,当图片被压缩/拉伸的时候保存较好的显式效果
gravity 当图片小于容器的大小的时候,经过此属性能够为图片定位
tileMode 平铺模式,设置了此值的话gravity会失效分为 [disable] [clamp] [repeat] [mirro]
mipMap 纹理映射,不经常使用,默认值为false,我也没搞懂
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:src="@drawable/dog" />

Nine-pacth Drawable

能够用来描述一个 .9图 ,我的感受比较鸡肋,由于AS的.9图设计器很是好使,这里就很少说了,我想我几乎是不会使用XML来制做.9图的。你们若是感兴趣,能够去学习一下。

ShapeDrawable

我想这个Drawable是最经常使用的Drawable了,用来定义各类形状。下面是ShapeDrawable的属性:

上面的思惟导图中列出的,ShapeDrawable的属性,咱们来看一看这些属性的详细信息.

Shape

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring">

shape 属性定义的ShapeDrawable的形状,其中有4个值 oval(椭圆形) line(线形) rectangle(正方形) ring(环形)

solid

单色填充形状,须要注意的是,此属性和 gradient 属性是冲突的,只能生效一个。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFF" />
</shape>

gradient

这一属性在上面已经介绍过,这里就不在赘述。

corners

设置圆角,能够四个角统一设置,也能够分别设置。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="45dp" />
</shape>

padding

padding 内边距,这个属性比较奇怪,虽然它定义在Shape中,可是它表明的是View的内容与View的边距l。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#000" />
    <padding
        android:left="10dp"
        android:top="20dp" />
</shape>

效果图以下:

图片.png | left | 369x134

size

size 表明的是Shape的__固有大小__,可是通常来讲,经过size设置的大小并非shape的最终的显式大小,在这里咱们须要了解一下固有大小的概念,Drawable类有两个方法 getIntrinsicWidthgetIntrinsicHeight 来获取Drawable的固有宽高,对于BitmapDrawable来讲,固有宽高表明的是Bitmap的宽和高。而Shape是默认没有固有宽高的(默认值为-1),可是若是经过了size属性为Shape设置了宽和高,那么Shape就有了固有宽高,可是这仅仅表明的是ShapeDrawable的固有宽高,可是若是Shape做为背景的话是不受固有宽高影响的依然会根据View的大小拉伸或压缩。

stroke

stroke表明的是Shape的边框.

<stroke
    android:width="1dp"
    android:color="#FFF"
    android:dashGap="1dp"
    android:dashWidth="3dp" />

属性解释:

  • width 设置边框的宽度
  • color 设置边框的颜色
  • dashGap若是设置了此值,那么边框的样式就会成为虚线,该值表明的是虚线与是虚线之间的间隔的大小
  • dashWidth 表明的是,每一条虚线的宽度

总结

这一片文章,咱们主要学习了5个Drawable,其中Shape是最重要的。接线来的文章中,咱们还会继续学习其余的Drawable直到学完系统的13种Drawable.

相关文章
相关标签/搜索