开发者社区> ctrip_xzh> 正文

android drawable

简介: <span style="font-family:Arial; font-size:14px; line-height:25px">android.graphics.drawable.</span><span style="font-family:Arial; font-size:14px; line-height:25px; color:rgb(255,0,255)">Drawable<
+关注继续查看
android.graphics.drawable.Drawable是个虚类。
它的直接子类有
BitmapDrawable, ClipDrawable, ColorDrawable, DrawableContainer, GradientDrawable, 
InsetDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RotateDrawable, 
ScaleDrawable, ShapeDrawable
间接子类有
AnimationDrawable, LevelListDrawable, PaintDrawable, StateListDrawable, TransitionDrawable

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.

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.这个用来设置Drawable要画好大
  • The getPadding(Rect) 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.这个接口用于返回padding,这个padding其实表示的是控件的content的padding
  • 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 (viasetCallback(Drawable.Callback)) so that animations will work. A simple way to do this is through the system facilities such assetBackgroundDrawable(Drawable) and ImageView.
Drawable定义了一个可画的对象一些接口。
它是抽象类。我们只能实例化它的实现类。Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象。 
注意1:如果要手动调用draw(Canvas canvas)来画Drawable,
一定要在之前通过
void        setBounds(Rect bounds)
来设置其需要画到的区域。
注意2: 可通过setCallback(Drawable.Callback)来实现动画。
构造函数
Public Constructors
Drawable()
主要函数
Public Methods
void clearColorFilter()
final Rect copyBounds()
Return a copy of the drawable's bounds in a new Rect.
final void copyBounds(Rect bounds)
Return a copy of the drawable's bounds in the specified Rect (allocated by the caller).
static Drawable createFromPath(String pathName)
Create a drawable from file path name.
static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts)
Create a drawable from an inputstream, using the given resources and value to determine density information.
static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName)
Create a drawable from an inputstream, using the given resources and value to determine density information.
static Drawable createFromStream(InputStream is, String srcName)
Create a drawable from an inputstream
static Drawable createFromXml(Resources r, XmlPullParser parser)
Create a drawable from an XML document.
static Drawable createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs)
Create from inside an XML document.
abstract void draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
final Rect getBounds()
Return the drawable's bounds Rect.
Drawable.Callback getCallback()
Return the current Drawable.Callback implementation attached to this Drawable.
int getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
Drawable.ConstantState getConstantState()
Return a Drawable.ConstantState instance that holds the shared state of this Drawable.
Drawable getCurrent()
int getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
int getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
final int getLevel()
Retrieve the current level.
int getMinimumHeight()
Returns the minimum height suggested by this Drawable.
int getMinimumWidth()
Returns the minimum width suggested by this Drawable.
abstract int getOpacity()
Return the opacity/transparency of this Drawable.
boolean getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
int[] getState()
Describes the current state, as a union of primitve states, such as state_focusedstate_selected, etc.
Region getTransparentRegion()
Returns a Region representing the part of the Drawable that is completely transparent.
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
Inflate this Drawable from an XML resource.
void invalidateSelf()
Use the current Drawable.Callback implementation to have this Drawable redrawn.
boolean isStateful()
Indicates whether this view will change its appearance based on state.
final boolean isVisible()
void jumpToCurrentState()
If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.
Drawable mutate()
Make this drawable mutable.
static int resolveOpacity(int op1, int op2)
Return the appropriate opacity value for two source opacities.
void scheduleSelf(Runnable what, long when)
Use the current Drawable.Callback implementation to have this Drawable scheduled.
abstract void setAlpha(int alpha)
Specify an alpha value for the drawable.
void setBounds(int left, int top, int right, int bottom)
Specify a bounding rectangle for the Drawable.
void setBounds(Rect bounds)
Specify a bounding rectangle for the Drawable.
final void setCallback(Drawable.Callback cb)
Bind a Drawable.Callback object to this Drawable.
void setChangingConfigurations(int configs)
Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
abstract void setColorFilter(ColorFilter cf)
Specify an optional colorFilter for the drawable.
void setColorFilter(int color, PorterDuff.Mode mode)
Specify a color and porterduff mode to be the colorfilter for this drawable.
void setDither(boolean dither)
Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.
void setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmap when scaled or rotated (for drawables that use bitmaps).
final boolean setLevel(int level)
Specify the level for the drawable.
boolean setState(int[] stateSet)
Specify a set of states for the drawable.
boolean setVisible(boolean visible, boolean restart)
Set whether this Drawable is visible.
void unscheduleSelf(Runnable what)
Use the current Drawable.Callback implementation to have this Drawable unscheduled.
Protected Methods
void onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you recognize the specified state.
boolean onLevelChange(int level)
Override this in your subclass to change appearance if you vary based on level.
boolean onStateChange(int[] state)
Override this in your subclass to change appearance if you recognize the specified state.
直接子类
BitmapDrawable     A Drawable that wraps a bitmap. You can create a BitmapDrawable from a file path, 
                                an input stream, through XML inflation, or from a Bitmap object. 
                            它的本质是个bitmap,它提供了针对bitmap的实现。
注意:Android supports bitmap files in a three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).
也是说Android把.png,.jpg,.gif都是看为BitmapDrawable,Android应该是把这三种图片转为bitmap来处理。
其实任何图片绘画时最后都是转为bitmap来处理

        BitmapDrawable bitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);
        Bitmap bitmap=bitmapDrawable.getBitmap();

可以通过上面的形式把BitmapDrawable转化为Bitmap
ClipDrawable     A Drawable that clips another Drawable based on this Drawable's current level value. 
具体参照《ClipDrawable
ColorDrawable     A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region. 
DrawableContainer
    它应该是专门用来存放Drawable的。
    如何直接使用它还不知道。它不能在XML文件中定义。
    它的子类AnimationDrawable, LevelListDrawable, StateListDrawable        
GradientDrawable     A Drawable with a color gradient for buttons, backgrounds, etc.
    It can be defined in an XML file with the <shape> element. For more information。 
InsetDrawable     A Drawable that insets another Drawable by a specified distance. 
LayerDrawable     A Drawable that manages an array of other Drawables. 
NinePatchDrawable     A resizeable bitmap, with stretchable areas that you define. 
它对应的是.9.png文件。关于此请参考《个性缩放图片NinePatchDrawable
PictureDrawable     Drawable subclass that wraps a Picture, allowing the picture to be used whereever a Drawable is supported. 
RotateDrawable     
A Drawable that can rotate another Drawable based on the current level value. 
ScaleDrawable     A Drawable that changes the size of another Drawable based on its current level value. 
ShapeDrawable     A Drawable object that draws primitive shapes. 关于此请参考《ShapeDrawable
ColorDrawable
    A specialized Drawable that fills the Canvas with a specified color, 
    with respect to the clip region. Note that a ColorDrawable ignores the ColorFilter. 
    It also ignores the Bounds, meaning it will draw everywhere in the current clip, 
    even if setBounds(...) was called with a smaller area.
    It can be defined in an XML file with the <color> element.
注意1:它会忽略掉ColorFilter和Bounds,它会把整个clip区域都填充成指定的颜色。
在XML中定义和引用ColorDrawable
A color resource can also be used as a drawable in XML. For example,
when creating a state list drawable, you can reference a color resource for the android:drawable attribute 
(android:drawable="@color/green
在values下的Colors.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<
color name="red">#FFFF0000</color>
</resources>

像引用Drawable一样引用它
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:background="
@color/red"
    />

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云安全组设置详细图文教程(收藏起来) 阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程。阿里云会要求客户设置安全组,如果不设置,阿里云会指定默认的安全组。那么,这个安全组是什么呢?顾名思义,就是为了服务器安全设置的。安全组其实就是一个虚拟的防火墙,可以让用户从端口、IP的维度来筛选对应服务器的访问者,从而形成一个云上的安全域。
19823 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
29209 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
22543 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
16483 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
20725 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
14905 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
23589 0
+关注
443
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载