`
h416756139
  • 浏览: 359182 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Drawable资源

阅读更多
Drawable资源是对图像的一个抽象,你可以通过getDrawable(int)得到并绘制到屏幕上。这里有几种不同类型的Drawable: 

Bitmap File 
基本的Bitmap图像。Android支持几种不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。
注意:Bitmap文件可能会被aapt工具进行无损图像压缩优化。例如,一个真彩色的PNG(不超过256色)可能会被转换成一个带有颜色板的8位PNG。这样做能保证图片质量一样,但减少内存占用。因此,需要了解的是放在这个文件夹下的二进制图像在编译时可能会发生变更。如果你打算以位流方式读取图像来转化成Bitmap的话,可以把它们放到res/raw中,在这里,它们不会被优化。
 
File Location:
    res/drawable/filename.png (.png, .jpg, 或.gif)
    文件名会被当作资源ID使用。
Complied Resource Datatype:
    指向BitmapDrawable的资源指针。
Resource Reference:
    R.drawable.filename(Java)
    @[package:]drawable/filename(XML)
Example:
    在res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/myimage" />
Shape 
定义在XML中的几何形状。 
File Location:
    res/drawable/filename.xml
    文件名将被当作资源ID使用。
Complied Resource Datatype:
    指向ShapeDrawable的资源指针。
Resource Reference:
    R.drawable.filename(Java)
    @[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape=["rectangle" | "oval" | "line" | "ring"] > 
    <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:usesLevel=["true" | "false"] /> 
    <solid 
        android:color="color" /> 
    <stroke 
        android:width="integer" 
        android:color="color" 
        android:dashWidth="integer" 
        android:dashGap="integer" /> 
    <padding 
        android:left="integer" 
        android:top="integer" 
        android:right="integer" 
        android:bottom="integer" /> 
    <corners 
        android:radius="integer" 
        android:topLeftRadius="integer" 
        android:topRightRadius="integer" 
        android:bottomLeftRadius="integer" 
        android:bottomRightRadius="integer" /> 
</shape>
Elements:
    <shape>
              必须。必须是根元素。
    Attributes:
              android:shape
                  Keyword。定义Shape的类型。有效的值包括:
Value
Desciption
"rectangle"
矩形。默认形状。
"oval"
椭圆。
"line"
水平直线。需要<stroke>元素定义线的宽度。
"ring"
环形。
 
接下来的特性只能在android:shape=”ring”时使用:
              android:innerRadius
                  Dimension。内环的半径。
              android:innerRadiusRatio
         Float。以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值可以被android:innerRadius覆盖。默认值是9。
              android:thickness
                  Dimension。环的厚度。
              android:thicknessRatio
          Float。以环的宽度比率来表示环的厚度。例如,如果android:thicknessRatio=”2”,厚度就等于环的宽度除以2。这个值可以被android:thickness覆盖。默认值是3。
              android:useLevel
                  Boolean。“true”表示可以当作LevelListDrawable使用。一般都为“false”。
   
<gradient>
              为Shape指定渐变色。
Attributes:
              android:angle
Integer。渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。
              android:centerX
                  Float。渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。
              android:centerY
                  Float。渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。
              android:centerColor
                  Color。可选的颜色,出现在start和end颜色之间。
              android:endColor
                  Color。end颜色。
              android:gradientRadius
                  Float。渐变色的半径。当android:type=”radial”时有效。
              android:startColor
                  Color。start颜色。
android:type
                  Keyword。渐变色的样式。有效值为:
Value
Description
"linear"
线性渐变,默认值。
"radial"
环形渐变。start颜色是处于中间的颜色。
"sweep"
sweep渐变
              android:useLevel
                  Boolean。“true”表示可以当作LevelListDrawable使用。   
<solid>
              填充shape的单一色。
Attributes:
    android:color
                  Color。这个颜色会应用到shape上。   
<stroke>
              shape的线形。
    Attributes:
              android:width
                  Dimension。线的厚度。
              android:color
                  Color。线的颜色。
              android:dashGap
                  Dimension。间断线间的距离。仅在android:dashWidth设定时有效。
              android:dashWidth
                  Dimension。间断线的大小。仅在android:dashGap设定时有效。 
    <padding>
              内部View元素的边距。
    Attributes:
              android:left
                  Dimension。左内边距。
              android:top
                  Dimension。上内边距。
              android:right
                  Dimension。右内边距。
              android:bottom
                  Dimension。下内边距。 
    <corners>
              为shape创建圆角。当shape是一个矩形时有效。
    Attributes:
              android:radius
                  Dimension。圆角的半径。会被下面的特性覆盖。
              android:topLeftRadius
                  Dimension。左上圆角半径。
              android:topRightRadius
                  Dimension。右上圆角半径。
              android:bottomLeftRadius
                  Dimension。左下圆角半径。
              android:bottomRightRadius
                  Dimension。右下圆角半径。
 
Examples:
    XML文件保存在res/drawable/gradient_box.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient  
        android:startColor="#FFFF0000"  
        android:endColor="#80FF00FF" 
        android:angle="45"/> 
    <padding android:left="7dp"  
        android:top="7dp" 
        android:right="7dp"  
        android:bottom="7dp" /> 
    <corners android:radius="8dp" /> 
</shape>
    Layout XML将被当作ShapeDrawable应用到一个View上:
<TextView 
    android:background="@drawable/gradient_box" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" />

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics