Drawable是对可绘制资源的一种抽象,他和view不同,它不具有可交互性。在我们的项目结构中,通常在res下面有一个名为drawable的文件夹,里面的资源我们是可以通过以下两种方式获取:
(1)R.drawable.xxx
(2)getResources().getDrawable(R.drawable.xxx)
接下来我就会对常用的Drawable(本身是一个抽象类)子类的使用做一个简单的介绍,今天主要介绍一下InsetDrawable,在看之前也可以看一下我的上两篇关于BitmapDrawable基本使用,LayerDrawable基本使用,ClipDrawable基本使用,LevelListDrawable基本使用,TransitionDrawable基本使用的博客。
1,InsetDrawable
下面直接看官方解释,上面也有介绍使用场景,官方解释如下:
【
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.
It can be defined in an XML file with the <inset>
element.
一个Drawable以一定的间距内置进另一个Drawable中,当一个View需要一个比其实际边界小的背景的时候使用该种Drawable
它可以通过在xml文件中使用<inset>属性来定义
】
其实可以看出来InsetDrawable的介绍还是很简单的
构造方法:
InsetDrawable(Drawable drawable, int inset) |
InsetDrawable(Drawable drawable, int insetLeft, int insetTop, int insetRight, int insetBottom) |
成员方法:
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). |
int |
getChangingConfigurations() Return a mask of the configuration parameters for which this drawable mau change, requiring that it be re-created. |
Drawable.ConstantState |
getConstantState() |
int |
getIntrinsicHeight() Return the intrinsic height of the underlying drawable object. |
int |
getIntrinsicWidth() Return the intrinsic width of the underlying drawable object. |
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. |
void |
inflate(Resources r, XmlPullParser parser, AttributeSet attrs) |
void |
invalidateDrawable(Drawable who) Called when the drawable needs to be redrawn. |
boolean |
isStateful() Indicates whether this view will change its appearance based on state. |
Drawable |
mutate() Make this drawable mutable. |
void |
scheduleDrawable(Drawable who, Runnable what, long when) A Drawable can call this to schedule the next frame of its animation. |
void |
setAlpha(int alpha) Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque. |
void |
setColorFilter(ColorFilter cf) Specify an optional colorFilter for the drawable. |
boolean |
setVisible(boolean visible, boolean restart) Set whether this Drawable is visible. |
void |
unscheduleDrawable(Drawable who, Runnable what) A Drawable can call this to unschedule an action previously scheduled with Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long) . |
2,InsetDrawable基本使用
2.1 定义一个InsetDtawable对应的xml文件
inset_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#00ffff"
android:insetBottom="100dp"
android:insetLeft="50dp"
android:insetRight="50dp"
android:insetTop="100dp">
</inset>
如果到四个边距一样,也可以这样设置一个属性参数即可:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#00ffff"
android:inset="50dp">
</inset>
2.2 测试代码和结果
InsetDtawable的基本使用比较简单,这里就不多展开介绍了。其实我们也可以看出来其构造方法对应于我们前面介绍的两种边距设置方式。其他的Drawable子类也是一样的道理,我们通过XML文件创建的Drawable,在设置属性值的时候就相当于new一个Drawable的过程类似。