Drawable uses detailed

Highly recommended article: Welcome Favorite
Android Dry Share

Reading five minutes, ten o'clock daily, lifelong learning with you, here is the Android programmer

DrawableIt is a Androidcommon method of image display.
Concept: Drawablerefers to draw graphics on the screen, it has passed getDrawable(int)the like or an application API to retrieve having android:drawableand android:iconother attributes such as XMLgraphics resources.

This article describes the Androidpart of the development of knowledge by reading this article, you will reap the following:

  1. Drawable classification
  2. Bitmap bitmap BitmapDrawable
  3. FIG stretchable (* .9.png) NinePatchDrawable.
  4. Layers LayerDrawable
  5. FIG different states (selector) StateListDrawable
  6. Level list LevelListDrawable
  7. Convert images TransitionDrawable
  8. Insert drawable
  9. Crop to drawing objects ClipDrawable
  10. Scaling can draw objects ScaleDrawable
  11. Shape drawable ShapeDrawable
  12. Drawable Summary and Bitmap common methods

1.Drawable Profile

Inheritance as follows:

[java.lang.Object]
   ↳
     android.graphics.drawable.Drawable

Drawable classified as follows:

  1. Bitmap Bitmap BitmapDrawable
  2. Stretchable FIG.(*.9.png) NinePatchDrawable。
  3. LayersLayerDrawable
  4. FIG different states (selector) StateListDrawable
  5. Level ListLevelListDrawable
  6. Image conversion TransitionDrawable
  7. Insert drawable
  8. Crop to drawing objects ClipDrawable
  9. Zoom drawable ScaleDrawable
  10. Shape drawable ShapeDrawable

Resource references:

In the Javamiddle:
R.drawable.filename

In the XMLmiddle:
@[package:]drawable/filename

1. Bitmap Bitmap BitmapDrawable

Bitmap image. AndroidIt supports three formats of the bitmap file: .png(preferred), .jpg(acceptable), .gif(not recommended). Save these files to res/drawable/the directory

During the construction process, by aapt, the image is automatically optimized lossless compression tool bitmap file. For example, no more than 256the color of the true color PNGmay be converted by the palette 8bits PNG. This produces the same image quality, but requires less memory. So please note this directory binary image may change when building. If you plan to interpret the image as a bit stream to convert it to a bitmap, please change the image in res/raw/the folder, where they will not be optimized

Use as follows:

1. Conventional Bitmap

  • XML layout use
        <ImageView
            android:id="@+id/img_round"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/gril" />
  • Java code to use
getResources().getDrawable(R.drawable.xml_bitmap)

2.XML Bitmap

  • Create a bitmap resource files in XML

Note the property to use:

  1. antialias
    Activate, deactivate anti-aliasing

  2. dither
    When the bitmap to the screen, while a pixel configuration (e.g.: RGB 8888bitmaps and RGB 565the screen), to enable or disable bitmap dither.

3. `filter `
       启用或停用位图过滤。当位图收缩或拉伸以使其外观平滑时使用过滤。
  1. mipmap
    Enable or disable the mipmapprompt

The tileMode
definition of a tiled pattern. When the tile mode is enabled, the bitmap is repeated. Gravity mode is enabled when the tile is ignored
xml_bitmapbitmap achieve

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:filter="false"
    android:gravity="center_vertical|clip_vertical"
    android:mipMap="true"
    android:src="@drawable/gril"
    android:tileMode="repeat" >

    <!--
    antialias   
           启用、停用抗锯齿  
    dither 
           当位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕),启用或停用位图抖动。
    filter 
           启用或停用位图过滤。当位图收缩或拉伸以使其外观平滑时使用过滤。
    mipmap 
          启用或停用 mipmap 提示
    tileMode 
        定义平铺模式。当平铺模式启用时,位图会重复。重力在平铺模式启用时将被忽略

    -->

</bitmap>
  • XML reference method bitmap resources
        <ImageView
            android:id="@+id/img_drawable_bitmap"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@drawable/xml_bitmap" />
  • java code implementation
    java code using a bitmap method

2. FIG stretchable (* .9.png) NinePatchDrawable.

NinePatchIs an PNGimage in which the content may be defined as when the view outside the normal image boundary Androidscaling stretchable region. Such images are often specified as at least one dimension to "wrap_content"background view, and the view when expanded to fit the content, also squared image to match the size of the extended view. AndroidStandard Buttonbackground used for the widget is a typical squared image, which must be stretched to fit the text (or image) within the button.

    1. The regular use of reference with other pictures
    1. XML .9.png Picture of use with Bitmap XML

```



##3. 图层 LayerDrawable

`LayerDrawable` 是管理其他可绘制对象阵列的可绘制对象。列表中的每个可绘制对象按照列表的顺序绘制,列表中的最后一个可绘制对象绘于顶部。每个可绘制对象由单一 `<layer-list> `元素内的 `<item> `元素表示。

<item
    android:id="@+id/bird001"
    android:drawable="@drawable/bird0001_risk">
</item>
<item
    android:id="@+id/bird002"
    android:drawable="@drawable/bird0002_risk"
    android:left="50dp"
    android:top="50dp">
</item>
<item
    android:id="@+id/bird003"
    android:drawable="@drawable/bird0003_risk"
    android:left="100dp"
    android:top="100dp">
</item>


##4. 不同状态图(选择器) StateListDrawable

`StateListDrawable `是在 `XML `中定义的可绘制对象,它根据对象的状态,使用多个不同的图像来表示同一个图形。例如,`Button` 小部件可以是多种不同状态(按下、聚焦或这两种状态都不是)中的其中一种,而且可以利用状态列表可绘制对象为每种状态提供不同的背景图片。

您可以在 `XML` 文件中描述状态列表。每个图形由单一 `<selector> `元素内的 `<item> `元素表示。每个` <item> `均使用各种属性来描述应用作可绘制对象的图形的状态。

在每个状态变更期间,将从上到下遍历状态列表,并使用第一个与当前状态匹配的项目 —此选择并非基于“最佳匹配”,而是选择符合状态最低条件的第一个项目。
此方法非常常用,比如状态选择器

- 1. 常规使用方法同其他图片引用方式

- 2. 选择器 XML的使用方式

![状态选择器](//upload-images.jianshu.io/upload_images/5851256-7f4ea06bd1824161.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


##5. 级别列表 LevelListDrawable

管理大量备选可绘制对象的可绘制对象,每个可绘制对象都分配有最大的备选数量。使用` setLevel() `设置可绘制对象的级别值会加载级别列表中` android:maxLevel` 值大于或等于传递到方法的值的可绘制对象资源。
资源引用:
在 Java 中:
`R.drawable.filename`
在 XML 中:
`@[package:]drawable/filename`

<item
    android:drawable="@drawable/gril"
    android:maxLevel="100"
    android:minLevel="10"/>

可通过 `setLevel() `或 `setImageLevel()` 更改级别。

##6. 转换图像 TransitionDrawable

`TransitionDrawable` 是可在两种可绘制对象资源之间交错淡出的可绘制对象。

每个可绘制对象由单一` <transition> `元素内的 `<item>` 元素表示。不支持超过两个项目。要向前转换,请调用 `startTransition()`。要向后转换,则调用 `reverseTransition()`。

- xml 布局声明





- java 代码中使用

![java代码中TransitionDrawable使用方法 ](//upload-images.jianshu.io/upload_images/5851256-915167c8a4ef7ccb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

##7. 插入可绘制对象

在 `XML `文件中定义的以指定距离插入其他可绘制对象的可绘制对象。当视图需要小于视图实际边界的背景时,此类可绘制对象很有用。



##8. 剪裁可绘制对象 ClipDrawable

在 `XML `文件中定义的对其他可绘制对象进行裁剪(根据其当前级别)的可绘制对象。您可以根据级别以及用于控制其在整个容器中位置的重力,来控制子可绘制对象的裁剪宽度和高度。通常用于实现进度栏之类的项目。
- xml 初始化剪裁样式


- java 代码中使用

![ClipDrawable java 代码中使用方法 ](//upload-images.jianshu.io/upload_images/5851256-0aff2ba5d31cf91b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

##9. 缩放可绘制对象 ScaleDrawable

在 `XML `文件中定义的更改其他可绘制对象大小




##10. 形状可绘制对象 ShapeDrawable

在 `XML` 中定义的一般形状。


1. 绘制直线


"




2.绘制圆角矩形

<!-- 圆角-->
<corners android:radius="5dp" />
<!--描边-->
<stroke
    android:width="1dp"
    android:color="@android:color/holo_blue_light" />


```

12. Drawable Bitmap common and Methods

As more involved in the content, we have another article about the play.

Drawable Summary and Bitmap common methods

So far herein, this has ended, if the wrong place, welcome your suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Micro-channel public concern number: Programmer Android, receive welfare

Guess you like

Origin www.cnblogs.com/wangjie1990/p/11310681.html