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="@[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="@[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="@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>

时间: 2024-08-29 11:12:32

Android Drawable总结的相关文章

Android Drawable 关于selector中state_pressed=&quot;true&quot;的位置顺序

界面中有一个按钮使用这样的样式: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <corners android:radius="10dp"/> <solid a

Android Drawable Mipmap Vector使用及Vector兼容

原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/article/details/51472458 一.谷歌在app中图标的适配的历史 在安卓的发展历程中,由于设备碎片化的原故,谷歌在app中图标的适配上做出一步又一步的改进,大体有这么几个阶段: 首先有了drawable-(m|h|xh|xxh|xxxh)dpi 自android studio后,又有了mi

Android Drawable的9种子类 介绍

原文: Android Drawable的9种子类 介绍 Drawable 在android里面 就是代表着图像,注意是图像 而不是图片. 图片是图像的子集.图像除了可以包含图片以外 还可以包含颜色. 换句话说Drawble就是canvas 可以绘制的 一种概念. android 系统自带了很多种drawable.我们最好对自带的drawable 有一种比较熟悉的了解, 这样对我们apk开发很有好处,因为drawable使用 非常简单 基本上就是xml编写 即可.比你自己自定义view的成本要低

Android drawable selector 设置无效

今天写代码时,需要设置一个TextView 的点击效果,press 状态和normal 状态两个icon,同时,点击区域背景在press状态下也不同.实现时将TextView 放在RelativeLayout 中 ,RelativeLayout 作点击区域,设置点击的背景效果,TextView 在点击时显示不同的icon图片. 布局文件如下 <RelativeLayout android:id="@+id/dele_layout" android:layout_width=&qu

Android Drawable文件夹对应像素密度

Android是自适应屏幕大小及密度的.Android为了保证在不同屏幕下的应用界面效果,提供了以下文件夹来储存图片资源.不同的文件夹对应像素密度不同的图片资源 drawable-ldpi:120dpi左右的屏幕(低密度) drawable-mdpi:160dpi左右的屏幕(中等密度) drawable-tvdpi:213dpi左右的屏幕(中高密度)这个主要在api13中为了优化面向电视的应用程序而引入的. drawable-hdpi:240dpi左右的屏幕(高密度) drawable-xdpi

Android -- Drawable与Bitmap

Drawable                                                                                 以下这个是测试加载1000个Drawable对象的代码: public class Main extends Activity { int number = 1000; Drawable[] array; @Override public void onCreate(Bundle savedInstanceState)

Android Drawable 与 LayerList综合汇总

先看需求.要求这样的效果 上代码 <?xml version="1.0" encoding="utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="oval" > <solid android:color=

Android drawable 玩转自定义图片以及bug的解决

很久没有空更新博客了,以至于挺多东西都用过之后就忘记了,没有很好的记录下来,之前在工作的时候也是这样,用完就忘记,所以觉得还是很有必要把自己用过的一些东西,解决的一些问题记录下来的,所以以后尽量坚持一周写一次博客,记录一下自己解决的问题,也与大学共享一下,建议大家也写一下博客或笔记什么的,因为在工作中,自己接触的东西并不可能只是自己刚开始的东西,比如说Android,其实在开发一个app或平时在公司工作的时候,还需要用到很多的东西,而且还有可能有一段时间去使用别的语言去开发,如果自己不记录一下,

Android drawable微技巧,你所不知道的drawable的那些细节

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/50727753 好像有挺久时间没更新博客了,最近我为了准备下一个系列的博客,也是花了很长的时间研读源码.很遗憾的是,下一个系列的博客我可能还要再过一段时间才能写出来,那么为了不至于让大家等太久,今天就给大家更新一篇单篇的文章,讲一讲Android drawable方面的微技巧. 话说微技巧这个词也是我自己发明的,因为drawable这个东西相信大家天天都在使用,每个人都再熟悉不过了

Android UI集锦——1.Android Drawable分类汇总(1/3)

Android UI集锦--1.Android Drawable分类汇总(1/3) -转载请注明出处coder-pig 本节引言: 小猪好像写了好几个专题,都没坚持写完,又忍不住开个新的专题了,因为最近打算 开始研究Android图形与图形图像处理,动画以及自定义View等,所以就顺道记录下, 最近事有点多,感觉情绪很低迷,心理压抑又找不到倾述的对象,这个时候程序猿肯定会说: "没对象,你自己new一个啊",好有道理,我竟无言以对...好吧!还是自己的那句座右铭: 没什么可以一蹴而就,