Drawable Resource 使用笔记

Layer List

管理一组drawable,在List中最后一个显示在最上面。

一个 <item> element 代表一个drawable。显示位置由属性指定。

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    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" />
</layer-list>


所有drawable item 默认scale fit to the size of the containing view.为避免scale 填充 ,可以给一个<bitmap>的,gravity 设置成center或其他,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

Bitmap

<bitmap> 可以给BitmapDrawable添加额外的效果

BitmapDrawable :A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.


Nine-Patch

.9.png的图片常用来做背景,可拉伸。

<nine-patch> 可以指定些效果

<?xml version="1.0" encoding="utf-8"?>
<nine-patch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:dither=["true" | "false"] />

nine-patch的拉伸规则

left top,图片会被拉伸的区域

right  bottom, View的内容会被放置在图片的该区域



State List

当状态发生改变,从top到bottom,第一个符合条件的item会被使用。所以默认的要放在最后一个。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

Level List

ImageView setLevel(level),从Level List中选择对应level的图片。要求List中从top到bottom第一满足条件的item,要求item的level大于等于传进来的level

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="0" android:drawable="@drawable/battery_0" />
    <item android:maxLevel="1" android:drawable="@drawable/battery_1" />
    <item android:maxLevel="2" android:drawable="@drawable/battery_2" />
    <item android:maxLevel="3" android:drawable="@drawable/battery_3" />
    <item android:maxLevel="4" android:drawable="@drawable/battery_4" />
</level-list>

Transition Drawable

过渡,渐变。

<transition> 下不超过2个item

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>

<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);

Inset Drawable

当View需要一个比它的边界小的背景时,用inset drawable正好

<?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

对一个Drawable进行裁剪

<?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"] />

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/android"
    android:clipOrientation="horizontal"
    android:gravity="left" />

		
		<ImageView
	    android:id="@+id/image"
	    android:src="@drawable/clip"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content" />

ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);

默认clipdrawable的level是0,完全不显示,当level为10000时,则完全显示。可在0-10000间设置。


Scale Drawable

A drawable defined in XML that changes the size of another drawable based on its current level.

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:scaleHeight="percentage"
    android:scaleWidth="percentage" />

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />

        <ImageView android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/scale_test" />

scale drawable 并没有显示。如下在代码中设置了后才显示。且,当scaleWidth、scaleheight 大于等于100%时,仍无显示,若是80%指缩放了80%,剩下的显示的只有20%

		ImageView imageview = (ImageView) findViewById(R.id.image);
		Drawable d = imageview.getDrawable();
		d.setLevel(1);
		imageview.setImageDrawable(d);

Shape Drawable

形状 Drawable

<?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:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

<corner> 当shape是rectangle时才起作用

<gradient>为shape填充渐变色

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

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="0dp" //当内径为0时,ring则成了一个圆
    android:thickness="100dp"
    android:useLevel="false">
    
    <solid android:color="#ffffff"/>
	<stroke android:color="#0096ff" android:width="3dp"/>
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ffffff"/>
    <size android:width="80dp" android:height="150dp"/> //椭圆,用宽高控制长短直径
	<stroke android:color="#0096ff" android:width="3dp"/>
</shape>



<padding>View的content和View的padding,不是针对shape的

<size> shape的大小

<solid> 为shape填充固定颜色

<stroke> 给shape描边


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值