Android学习之Drawable(一)


Drawable有很多种,它们表示一种图像概念,但它们不全是图片。Drawable是什么呢?下面是Google Android API中的定义:

A Drawable is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
它大致的意思是:Drawable一种图像概念。通常,你会把它当成一种能够在屏幕上显示的资源类型来处理,Drawable类提供了一个通用的API来处理不同形式的图像资源。与View不同,Drawable不能接受事件,也不能和用户交互。

下面介绍几种Drawable


BitmapDrawable

BitmapDrawable几乎是最简单的了,它表示一张图片。通常在开发中我们就直接引用图片即可,比如: R.drawable.image(drawable目录下有一个image.jpg或者image.png的图片资源),但是我们也可以用xml来描述Drawable。xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@mipmap/ic_launcher"
        android:antialias="true|false"
        android:dither="true|false"
        android:filter="true|false"         android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
        android:mipMap="true|false"
        android:tileMode="disabled|clamp|repeat|mirror"/>

ShapeDrawable

这是一种很常见的Drawable,通常是通过编写xml文件来创建的,因此有些复杂。ShapeDrawable通常是通过颜色来构建图形的,既可以是纯色,也可以具有渐变效果。使用大致如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle|oval|line|ring">
    <corners
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer"
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerColor="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type="linear|radial|sweep"
        android:useLevel="true|false" />
    <padding
        android:bottom="integer"
        android:left="integer"
        android:right="integer"
        android:top="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashGap="integer"
        android:dashWidth="integer" />

</shape>
  • android:shape
      表示图形形状:rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环),默认矩形,line和ring必须有 < stroke>标签来指定宽度和颜色,否则达不到预期效果。
  • < gradient>  渐变效果,与< solid>标签互斥

  • < solid>  纯色填充 通过android:color即可指定shape的颜色


LayerDrawable

它是一种层次化的Drawable,在< layer-list>< /layer-list>结点下有多个< item>< /item>其中后面的< item>< /item>叠加在前面的< item>< /item>上面,想爱你面是一耳光文本输入框的例子:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e" />
        </shape>
    </item>
    <item android:bottom="6dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>

</layer-list>

StateListDrawable

StateListDrawable对应< selector>标签,这个大家应该比较熟悉。我们经常会给Button设置一个selector。StateListDrawable表示Drawable的集合,集合中的每个Drawable都对应着View的一种状态,系统会根据View的状态来给View设定相应的Drawable,下面是一个selector的创建样例

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/black"/>    <!-- 表示默认状态-->
    <item android:state_focused="true"
          android:drawable="@android:color/holo_orange_dark"/><!-- 表示获取焦点状态-->
    <item android:state_pressed="true"
          android:drawable="@android:color/holo_red_dark"/><!-- 表示被点击状态-->
</selector>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值