首页 > 代码库 > Android Drawable总结
Android Drawable总结
Drawable有几种
1:Bitmap
支持格式:.png (preferred), .jpg (acceptable), .gif (discouraged).
将Bitmap文件放在drawable文件夹下会被AAPT自动优化
2:XML Bitmap
An XML bitmap is a resource defined in XML that points to a bitmap file.
The effect is an alias for a raw bitmap file
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="http://www.mamicode.com/@[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"] />
Defines the tile mode. When the tile mode is enabled, the bitmap is repeated.
Gravity is ignored when the tile mode is enabled.
3:Nine-Patch
You typically assign this type of image as the background of a View
that has at least one dimension set to "wrap_content",
and when the View grows to accomodate the content,
the Nine-Patch image is also scaled to match the size of the View
4:XML Nine-Patch
An XML Nine-Patch is a resource defined in XML that points to a Nine-Patch file.
The XML can specify dithering for the image
<?xml version="1.0" encoding="utf-8"?>
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="http://www.mamicode.com/@[package:]drawable/drawable_resource"
android:dither=["true" | "false"] /> 图片与屏幕像素不一样的时候,会拉伸?
5:Layer List
A LayerDrawable is a drawable object that manages an array of other drawables.
<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" ></item>
</layer-list>
<item></item>标签里可以利用<bitmap/>标签装载图片,此时图片将不会被拉伸。
<item>
<bitmap android:src="http://www.mamicode.com/@drawable/image"
android:gravity="center" />
</item>
6:State List
不同状态显示不同的图片 常用于背景
During each state change, the state list is traversed top to bottom and the first item
that matches the current state is used—the selection is not based on the "best match,"
but simply the first item that meets the minimum criteria of the state.
<?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>
7:Level List 有什么用
A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value.
Setting the level value of the drawable with setLevel() loads the drawable resource in the level list
that has a android:maxLevel value greater than or equal to the value passed to the method.
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/status_off"
android:maxLevel="0" />
<item
android:drawable="@drawable/status_on"
android:maxLevel="1" />
</level-list>
8:Transition Drawable 可以让两张图片同时淡入淡出(也只能包含两张图片)
<?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>
9:Inset Drawable
A drawable defined in XML that insets another drawable by a specified distance.
This is useful when a View needs a background that is smaller than the View‘s actual bounds.
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/background"
android:insetTop="10dp"
android:insetLeft="10dp" />
10:Clip Drawable 把原图的长,宽,厚度都分成10000level,以此切割
<?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 imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);
Increasing the level reduces the amount of clipping and slowly reveals the image.
11: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/logo"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
12:Shape Drawable
<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" 必须是45的倍数,0:从左到右,90:从下到上
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer" android:type="radial"时有效, 放射状
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>
Android Drawable总结