Android Drawable资源

android中的drawable资源

Drawable简介

一.StateListDrawable (选择器)

语法

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

子节点要关心只有 selector里面的属性和item里面的状态。
每一个item表示一个Drawable,item里面放什么怎么主要关系不大,我们关注的只有item的状态。

  • android:constantSize
    StateListDrawable的大小是否随着View的状态的改变而改变。true固定大小,不随之改变,false为随着改变拉伸自身大小。
    默认为false。
  • android:dither
    是否开启抖动,默认为true
  • android:variablePadding
    默认false,建议false
    是否随着View的状态的改变而改变padding,如果为true,padding的状态会随着改变,如果为false,那么就采用item内部的Drawable自身设定的padding的值。
  • 主要关心item里面的Drawable的状态判定
状态含义
android:state_pressed按下的状态,(按下但是还没松开)
android:state_focused当前View获取了焦点
android:state_selected用户选择了当前View
android:state_checked用户选中了View,一般用于CheckBox这种非黑即白的选项
android:state_enabled当前View处于可用的状态
android:state_hovered光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_checkable组件是否能被check。如:RadioButton是可以被check的。
android:state_activated是否被激活
android:state_window_focused应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

特点

  1. item可以用多个,item里面放的是Drawable
  2. 系统查找顺序是顺着item从上到下知道找到就停止往下寻找。

二.LayerDrawable(layer-list)

  • LayerDrawable对应的xml标签是,它表示一种层次化的Drawable集合,通过将Drawable放置在不同的层上面从而达到一种叠加后的效果。

  • 使用layer-list可以将多个drawable按照顺序层叠在一起显示,默认情况下,所有的item中的drawable都会自动根据它附上view的大小而进行缩放,

  • layer-list中的item是按照顺序从下往上叠加的,即先定义的item在下面,后面的依次往上面叠放

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

    <!-- 定义seekbar的背景颜色 ,id应为系统自带的 android:id="@android:id/background"-->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/lbs_popup_female"/>

    <!-- 定义seekbar滑动后的背景颜色 ,id应为系统自带的id, android:id="@android:id/progress"-->

    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/lbs_popup_female_selected"/>

</layer-list>

一个layer-list可以包含多个item,每个item表示一个Drawable

android:top=“50dp”;表示该item上边以ImageView上边界往里面缩了50dp
android:bottom="50dp"表示该item下边以ImageView下边界往里面缩了50dp
android:left=“50dp”;表示该item左边以ImageView左边界往里面缩了50dp
android:right=“50dp”;表示该item右边以ImageView右边界往里面缩了50dp

实现带阴影的按钮效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <!-- 灰色阴影 -->
        <layer-list> 
            <item
        android:left="2dp"
        android:top="4dp">
        <shape>
            <solid android:color="@android:color/darker_gray" />
            <corners android:radius="4dp" />
        </shape>
    </item>
    
         <!-- 红色前景 -->
        <item
        android:bottom="4dp"
        android:right="2dp">
        <shape>
            <solid android:color="#FF0000" />
            <corners android:radius="4dp" />
        </shape>
    </item>
        </layer-list>
    </item>
    
    <item>
        
        <!-- 灰色阴影 -->
        <layer-list> 
            <item
        android:left="2dp"
        android:top="4dp">
        <shape>
            <solid android:color="@android:color/darker_gray" />
            <corners android:radius="4dp" />
        </shape>
    </item>
    
         <!-- 白色前景 -->
        <item
        android:bottom="4dp"
        android:right="2dp">
        <shape>
            <solid android:color="#FFFFFF" />
            <corners android:radius="4dp" />
        </shape>
    </item>
        </layer-list>
        
    </item>
</selector>

三.Android中shape属性详解

三.ShapeDrawable

基本属性(corners、gradient、padding、size、solid、stroke)

1、Corners(定义圆角)

<corners    //定义圆角  
    android:radius="dimension"      //全部的圆角半径  
    android:topLeftRadius="dimension"   //左上角的圆角半径  
    android:topRightRadius="dimension"  //右上角的圆角半径  
    android:bottomLeftRadius="dimension"    //左下角的圆角半径  
    android:bottomRightRadius="dimension" />    //右下角的圆角半径  

Corners标签是用来定义圆角的,其中radius与其它四个并不能共同使用。

android:radius:定义四个角的的圆角半径。

其它四个是逐个字义每个角的圆角半径。

使用:

控件布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="50dip"
        android:text="@string/hello_world" 
        android:background="@drawable/shape_radius"/>
 </RelativeLayout>

shape定义:

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

2、solid(内部填充色)

solid用以指定内部填充色

只有一个属性:

<solid  android:color="color" />  

在上面的例子中,我们就将填充色指定为#ffff00了,如果我们不加圆角,只使用填充色,即将shape变成这样子:

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

3、gradient(定义渐变色)

gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:

<gradient 
    android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变  
    android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下  
    android:centerX="float"     //渐变中心X的相当位置,范围为0~1  
    android:centerY="float"     //渐变中心Y的相当位置,范围为0~1  
    android:startColor="color"   //渐变开始点的颜色  
    android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间  
    android:endColor="color"    //渐变结束点的颜色  
    android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用  
    android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果  

首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变)

  • 这几个属性的使用方法:
android:type=["linear" | "radial" | "sweep"]
android:startColor="color"   //渐变开始点的颜色  
android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间  
android:endColor="color"    //渐变结束点的颜色  
android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用  

下面我们使用三色渐变来看看这三种渐变方式都是怎么显示的:(如果不使用centerColor属性就是双色渐变,这个属性是可选的)

需要注意的一点是,在构造放射性渐变时,要加上android:gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用,下面列出这三个渐变方式的shape代码

线性渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

放射性渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="radial"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:gradientRadius="100"/>
</shape>

扫描式渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

可见放射性渐变,只是比其它两个多了个android:gradientRadius属性

  • android:angle属性(仅对线性渐变有效)
android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下  

我们在上面的三种渐变上都加上angle属性

能过跟上一个图对比可以发现,angle属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静,下面是此时的线性渐变shape代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="45"/>
</shape>
  • android:centerX与android:centerY

centerX、centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效,类型为分数或小数,不接受Dimension。默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。centerX、centerY的取值其实是宽和高的百分比;不难理解,下面看代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:centerX="0.2"
        android:centerY="0.8"/>
</shape>

取宽度的20%和高度的80%的位置,

  • android:useLevel

useLevel属性通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。

4、stroke(描边属性)

这是描边属性,可以定义描边的宽度,颜色,虚实线等

<stroke       
    android:width="dimension"   //描边的宽度  
    android:color="color"   //描边的颜色  
    // 以下两个属性设置虚线  
    android:dashWidth="dimension"   //虚线的宽度,值为0时是实线  
    android:dashGap="dimension" />      //虚线的间隔 

上面各个属性的意义如下:

我们使用绿色虚线描边,虚线高度是20dp,虚线宽度为10dp虚线间距为1dp:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke 
        android:width="20dp" 
        android:color="#00ff00"
        android:dashWidth="10dp"
        android:dashGap="1dp" />
</shape>

5、size和padding

size:是用来定义图形的大小的。

<size  
    android:width="dimension"  
    android:height="dimension" />

padding:用来定义内部边距

<padding   
    android:left="dimension"  
    android:top="dimension"  
    android:right="dimension"  
    android:bottom="dimension" />

Shape的属性(rectangle、oval、line、ring)

上面我们讲了Shape的子标签的的作用,但Shape本身还没讲,Shape自已是可以定义当前Shape的形状的,比如上面的矩形,还有椭圆形,线形和环形;这些都是通过Shape标签的 shape属性来定义的

  1. android:shape=[“rectangle” | “oval” | “line” | “ring”]
  2. shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
  3. 下面的属性只有在android:shape="ring时可用:
  4. android:innerRadius 尺寸,内环的半径。
  5. android:innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径,
  6. android:thickness 尺寸,环的厚度
  7. android:thicknessRatio 浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio=“2”,
  8. android:useLevel boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.

可见,只有第一个shape是可用的,其它五个都是shape等于ring时所特有的。

注意,无论这里shape取什么形状,他的子标签都是可用的,但有时并不会有效果,比如在shape为椭圆时,那corners标签就不会有效果

1、rectangle (矩形)

这就是上一节我们使用的形状,当我们不指定shape属性时,默认就是矩形的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    
    <TextView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:background="@drawable/try_shape_3"/>
 </LinearLayout>

shape代码:

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

2、oval(椭圆)

控件代码不变,下面是shape代码:

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

3、line(线形)

4、ring(环形)

android:innerRadius         尺寸,内环的半径。  
android:thickness           尺寸,环的厚度  
android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,  
      例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.  
android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",  
      那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.  
android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.

定义环形的内环尺寸和环的宽度。

控件定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    
    <TextView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:background="@drawable/try_shape_2"/>
 </LinearLayout>

shape定义:(这里一定要要加上useLevel属性并定义为false,不然没有效果)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" 
    android:innerRadius="20dp" 
    android:thickness="50dp"  
    android:useLevel="false">
    <solid android:color="#ff00ff"/>
</shape>

四.BitmapDrawable

Bitmap,代表一个位图图像,Android支持三种格式的位图图像:.png (preferred),.jpg (acceptable), .gif (discouraged)。

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

节点介绍:

  • 定义位图的来源和属性

属性:

  • xmlns:android

    类型:String。定义了XML的命名空间,必须是"http://schemas.android.com/apk/res/android"。如果<bitmap>是根元素,那么他是必须的,如果是嵌套在<itme>里面,那么就不是必须的。

  • android:src

    类型:Drawable resource。必需。 引用一个drawableresource.

  • android:antialias

    类型:Boolean。是否开启抗锯齿。

  • android:dither

    类型:Boolean。如果位图与屏幕的像素配置不同时,是否允许抖动.(例如:一个位图的像素设置是 ARGB 8888,但屏幕的设置是RGB 565)

  • android:filter

    类型:Boolean。是否允许对位图进行滤波。对位图进行收缩或者延展使用滤波可以获得平滑的外观效果。

  • android:gravity

    当图片小于容器的尺寸时,设置此选项对位图进行定位,不同选项可通过"|"来组合使用

  • tileMode:

    指定图片平铺填充容器的模式,设置这个的话,gravity属性会被忽略,有以下可选值: disabled(整个图案拉伸平铺),clamp(原图大小), repeat(平铺),mirror(镜像平铺)

功能:显示缩略图,大小为40*40

//通过openRawResource获取一个inputStream对象  
   InputStream inputStream = getResources().openRawResource(R.drawable.test);  
   //通过一个InputStream创建一个BitmapDrawable对象  
   BitmapDrawable drawable = new BitmapDrawable(inputStream);  
   //通过BitmapDrawable对象获得Bitmap对象  
   Bitmap bitmap = drawable.getBitmap();  
   //利用Bitmap对象创建缩略图  
   bitmap = ThumbnailUtils.extractThumbnail(bitmap, 40, 40);  
   //imageView 显示缩略图的ImageView  
   imageView.setImageBitmap(bitmap);

功能:image2从image1中截取120*120大小后显示,截取起始坐标为(x,y)

	BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
   //获取第一个图片显示框中的位图
   Bitmap  bitmap = bitmapDrawable.getBitmap();
   //显示图片的指定区域
   image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
   image2.setAlpha(alpha);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值