当前位置:首页 > 开发 > 移动开发 > 正文

Android Drawable总结

发表于: 2013-03-05   作者:892848153   来源:转载   浏览:
摘要: Drawable有几种 1:Bitmap        支持格式:.png (preferred), .jpg (acceptable), .gif (discouraged). 将Bitmap文件放在drawable文件夹下会被AAPT自动优化 2:XML Bitmap        An XML bitm

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>

 

    若要有不清楚的,请查看官方文档:http://developer.android.com/guide/topics/resources/drawable-resource.html

Android Drawable总结

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
LayerDrawable LayerDrawable是一组按照顺序绘制的drawable资源,是由多个drawable资源重叠绘制而组
Android SDK提供了一个强大的类Drawable,Drawable这个抽象类到底代表了什么,如何使用?Drawable是
Android Drawable 标签(空格分隔): Android Android的drawable有很多种类型, Bitmap File Nine-
Android Drawable 标签(空格分隔): Android Android的drawable有很多种类型, Bitmap File Nine-
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简
Android's drawables对编写程序是非常有用的。A Drawable通常是一个与view相关的插件后的画图容器。
Android's drawables对编写程序是非常有用的。A Drawable通常是一个与view相关的插件后的画图容器。
这篇文章帮助你更好了解Android's drawables对象,这篇文章描述现象你可以不知道,但是当你遇到了该
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简
在Android应用中,常常会用到Drawable资源,比如图片资源等,在Android开发中我们是用Drawable类来D
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号