现在的位置: 首页 > 综合 > 正文

android.graphics.drawable.Drawable

2018年04月19日 ⁄ 综合 ⁄ 共 2866字 ⁄ 字号 评论关闭
文章目录

Class Overview

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 是对 "something that can be drawn." 的一般抽象。通常你处理的 drawable 是被检索到的能画一些东西在屏幕上的资源。Drawable 类为处理一个底层的可以采用多种形式的可视化资源提供了一套一般的 api。不像 View,Drawable 没有任何的设备来接受事件或者其他的用户交互。

In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn:

  • The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how
    large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the getIntrinsicHeight() and getIntrinsicWidth() methods.
  • The getPadding method
    can return from some Drawables information about how to frame content that is placed inside of them. For example, a Drawable that is intended to be the frame for a button widget would need to return padding that correctly places the label inside of itself.
  • The setState(int[]) method allows the client to tell the Drawable in which state it is to be drawn, such as "focused",
    "selected", etc. Some drawables may modify their imagery based on the selected state.
  • The setLevel(int) method allows the client to supply a single continuous controller that can modify the Drawable is displayed,
    such as a battery level or progress level. Some drawables may modify their imagery based on the current level.
  • A Drawable can perform animations by calling back to its client through the Drawable.Callback interface. All clients should
    support this interface (via setCallback(Drawable.Callback)) so that animations
    will work. A simple way to do this is through the system facilities such as setBackgroundDrawable(Drawable) and ImageView.
除了简单的绘制,Drawable 还为他的客户端提供了大量的通用机制来实现和被绘制的东西交互:
setBounds(Rect) 必须被调用来告诉
Drawble 对象应该绘制在哪儿,绘制多大。所有的 Drawable 对象通常通过简单的尺寸缩放来实现这个请求的大小。getIntrinsicHeight() 和 getIntrinsicWidth() 可以获取到执行后的大小。

Though usually not visible to the application, Drawables may take a variety of forms:

  • Bitmap: the simplest Drawable, a PNG or JPEG image.
  • Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
  • Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
  • Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
  • States: a compound drawable that selects one of a set of drawables based on its state.
  • Levels: a compound drawable that selects one of a set of drawables based on its level.
  • Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.

Developer Guides

For more information about how to use drawables, read the Canvas and Drawables developer guide. For information and examples of creating drawable resources
(XML or bitmap files that can be loaded in code), read the Drawable Resources document.

抱歉!评论已关闭.