当前位置:天才代写 > tutorial > 安卓教程 > Android学习之Drawable(一)

Android学习之Drawable(一)

2021-03-01 10:16 星期一 所属: 安卓教程 浏览:460

Drawable有很多种多样,他们表明一种图象定义,但他们 不都是照片。Drawable是什么呢?下边是Google Android API中的界定:

A Drawable is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user. 
它大致的意思是:Drawable一种图象定义。一般 ,你能把它当做一种可以在显示屏上表明的资源来解决,Drawable类出示了一个通用性的API来解决不一样方式的图象資源。与View不一样,Drawable不可以接纳事情,也不可以和客户互动。

下边详细介绍几类Drawable

 

BitmapDrawable

BitmapDrawable基本上是非常简单的了,它表明一张图片。一般 在开发设计中大家就直接引用照片就可以,例如: R.drawable.image(drawable文件目录下有一个image.jpg或是image.png的图片种子),可是大家还可以用xml来叙述Drawable。xml文件以下:

 <?xml version="1.0" encoding="utf-8"?>
 <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
         android:src="@mipmap/ic_launcher"
         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:mipMap="true|false"
         android:tileMode="disabled|clamp|repeat|mirror"/>

ShapeDrawable

它是一种很普遍的Drawable,一般 是根据撰写xml文件来建立的,因而有一些繁杂。ShapeDrawable一般 是根据色调来搭建图型的,既能够是单色,还可以具备渐变色实际效果。应用大概以下所显示:

 <?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:bottomLeftRadius="integer"
         android:bottomRightRadius="integer"
         android:radius="integer"
         android:topLeftRadius="integer"
         android:topRightRadius="integer" />
     <gradient
         android:angle="integer"
         android:centerColor="integer"
         android:centerX="integer"
         android:centerY="integer"
         android:endColor="color"
         android:gradientRadius="integer"
         android:startColor="color"
         android:type="linear|radial|sweep"
         android:useLevel="true|false" />
     <padding
         android:bottom="integer"
         android:left="integer"
         android:right="integer"
         android:top="integer" />
     <size
         android:width="integer"
         android:height="integer" />
     <solid android:color="color" />
     <stroke
         android:width="integer"
         android:color="color"
         android:dashGap="integer"
         android:dashWidth="integer" />
 </shape>
    • android:shape 
        表明图型样子:rectangle(矩形框)、oval(椭圆形)、line(水平线)、ring(圆形),默认设置矩形框,line和ring务必有 < stroke>标识来特定总宽和色调,不然达不上预期目标。
    • < gradient>  渐变色实际效果,与< solid>标识相互独立

    • < solid>  单色添充 根据android:color就可以特定shape的色调

LayerDrawable

它是一种层次化的Drawable,在< layer-list>< /layer-list>节点下有好几个< item>< /item>在其中后边的< item>< /item>累加在前面的< item>< /item>上边,想爱你面是一巴掌文字文本框的事例:

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item>
         <shape android:shape="rectangle">
             <solid android:color="#0ac39e" />
         </shape>
     </item>
     <item android:bottom="6dp">
         <shape android:shape="rectangle">
             <solid android:color="#ffffff" />
         </shape>
     </item>
     <item
         android:bottom="1dp"
         android:left="1dp"
         android:right="1dp">
         <shape android:shape="rectangle">
             <solid android:color="#ffffff" />
         </shape>
     </item>
 </layer-list>

StateListDrawable

StateListDrawable相匹配< selector>标识,这一大伙儿应当较为了解。大家常常会给Button设定一个selector。StateListDrawable表明Drawable的结合,结合中的每一个Drawable都相匹配着View的一种情况,系统软件会依据View的情况来给View设置相对的Drawable,下边是一个selector的建立示例:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@android:color/black"/>    <!-- 表明默认设置情况-->
     <item android:state_focused="true"
           android:drawable="@android:color/holo_orange_dark"/><!-- 表明获得聚焦点情况-->
     <item android:state_pressed="true"
           android:drawable="@android:color/holo_red_dark"/><!-- 表明被点一下情况-->
 </selector>
 

    关键字:

天才代写-代写联系方式