Drawable 详解

public abstract class Drawable 
extends Object 

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

   

 <com.myapp.MyCustomDrawable xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="#ffff0000" />


    用drawable作为XML元素名并且从类的属性指定完整路径名。这个方法可以用在public 高层级类和public static 内部类。

 <drawable xmlns:android="http://schemas.android.com/apk/res/android"
 class="com.myapp.MyTopLevelClass$InnerCustomDrawable"
 android:color="#ffff0000" />


总结

Nested classes(嵌套的类

接口Drawable.Callback

如果想要创建一个动画继承 Drawable 的drawable,就实现这个接口

Drawable.ConstantState

这个抽象类被Drawables用来存储Drawables之间的共享状态和数据


Public constructors (公共构造函数

Drawable()


Public methods

voidapplyTheme(Resources.Theme t)

Applies the specified theme to this Drawable and its children.

应用指定的theme(主题)在Drawable和它的孩子

booleancanApplyTheme()
voidclearColorFilter()

Removes the color filter for this drawable.

从drawable中移除颜色过滤器

final RectcopyBounds()

Return a copy of the drawable's bounds in a new Rect.

在新的矩形(Rect)返回一个drawable的约束副本

final voidcopyBounds(Rect bounds)

Return a copy of the drawable's bounds in the specified Rect (allocated by the caller).

在指定的矩形(Rect)返回一个drawable的约束副本

static DrawablecreateFromPath(String pathName)

Create a drawable from file path name.

从路径名(pathName)创建一个drawable

static DrawablecreateFromResourceStream(Resources res, TypedValue value, InputStream is,String srcName, BitmapFactory.Options opts)

This method was deprecated in API level P. Prefer the version without an Options object.

这个方法在API P 中已经废弃了,请使用下面的方法

static DrawablecreateFromResourceStream(Resources res, TypedValue value, InputStream is,String srcName)

Create a drawable from an inputstream, using the given resources and value to determine density information.

从输入流创建一个drawable,使用给定的资源和值去决定密度信息。

static DrawablecreateFromStream(InputStream is, String srcName)

Create a drawable from an inputstream

从输入流创建一个drawable

static DrawablecreateFromXml(Resources r, XmlPullParser parser)

Create a drawable from an XML document.

从一个XML文件中创建一个drawable

static DrawablecreateFromXml(Resources r, XmlPullParser parser, Resources.Theme theme)

Create a drawable from an XML document using an optional Resources.Theme.

从一个XML文档用Resources.Theme创建一个drawable

static DrawablecreateFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs,Resources.Theme theme)

Create a drawable from inside an XML document using an optional Resources.Theme.

从一个XML文档使用一个可选的Resources.Theme创建一个drawable

static DrawablecreateFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs)

Create from inside an XML document.

从一个XML文档内创建

abstract voiddraw(Canvas canvas)

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

在它的约束内绘制,并遵从可选的效果,例如透明度alpha 和颜色过滤器

intgetAlpha()

Gets the current alpha value for the drawable.

获取当前透明度的值

final RectgetBounds()

Return the drawable's bounds Rect.

返回drawable的约束矩形

Drawable.CallbackgetCallback()

Return the current Drawable.Callback implementation attached to this Drawable.

返回当前这个Drawable实现的Drawable.Callback

intgetChangingConfigurations()

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

返回drawable可能会改变的配置参数,这需要drawable重建

ColorFiltergetColorFilter()

Returns the current color filter, or null if none set.

返回当前的颜色过滤器,返回null如果没设置的话

Drawable.ConstantStategetConstantState()

Return a Drawable.ConstantState instance that holds the shared state of this Drawable.

返回存有这个Drawable的共享状态的 Drawable.ConstantState 实例。

DrawablegetCurrent()
RectgetDirtyBounds()

Return the drawable's dirty bounds Rect.

返回drawable的脏约束矩形

voidgetHotspotBounds(Rect outRect)

Populates outRect with the hotspot bounds.

使用热点约束填充外矩形

intgetIntrinsicHeight()

Returns the drawable's intrinsic height.

返回drawable的本身高度

intgetIntrinsicWidth()

Returns the drawable's intrinsic width.

返回drawable的本身宽度

intgetLayoutDirection()

Returns the resolved layout direction for this Drawable.

返回Drawable的布局方向

final intgetLevel()

Retrieve the current level.

查询当前的level

intgetMinimumHeight()

Returns the minimum height suggested by this Drawable.

返回Drawable的建议最小高度

intgetMinimumWidth()

Returns the minimum width suggested by this Drawable.

返回Drawable的建议最小宽度

abstract intgetOpacity()

Return the opacity/transparency of this Drawable.

返回Drawable的不透明度或者透明度

voidgetOutline(Outline outline)

Called to get the drawable to populate the Outline that defines its drawing area.

调用此方法去获取drawable去填充定义在绘画区域的轮廓

booleangetPadding(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_focused,state_selected, etc.

描述当前状态,作为一个联合的基本状态,例如state_focused,state_selected, 等等

RegiongetTransparentRegion()

Returns a Region representing the part of the Drawable that is completely transparent.

返回完全透明的范围

voidinflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

从某个主题样式的XML资源填充这个drawable

voidinflate(Resources r, XmlPullParser parser, AttributeSet attrs)

Inflate this Drawable from an XML resource.

从XML 资源填充Drawable

voidinvalidateSelf()

Use the current Drawable.Callback implementation to have this Drawable redrawn.

使用当前 Drawable.Callback实现让这个Drawable重新绘制。

booleanisAutoMirrored()

Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left.

是否自动生成镜像

booleanisFilterBitmap()
booleanisStateful()

Indicates whether this drawable will change its appearance based on state.

标识是否根据状态来改变它的显示

final booleanisVisible()
voidjumpToCurrentState()

If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.

如果这个Drawable从状态之间执行转换动画,询问他立即调到当前状态并且跳过所有的动画。

Drawablemutate()

Make this drawable mutable.

让这个Drawable易变的。

booleanonLayoutDirectionChanged(int layoutDirection)

Called when the drawable's resolved layout direction changes.

在drawable的布局方向改变时调用。

static intresolveOpacity(int op1, int op2)

Return the appropriate opacity value for two source opacities.

在两种不透明源中返回恰当的不透明度值

voidscheduleSelf(Runnable what, long when)

Use the current Drawable.Callback implementation to have this Drawable scheduled.

abstract voidsetAlpha(int alpha)

Specify an alpha value for the drawable.

定义drawable的透明度

voidsetAutoMirrored(boolean mirrored)

Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left).

设置自动镜像

voidsetBounds(int left, int top, int right, int bottom)

Specify a bounding rectangle for the Drawable.

定义Drawable的约束矩形

voidsetBounds(Rect bounds)

Specify a bounding rectangle for the Drawable.

定义Drawable的约束矩形

final voidsetCallback(Drawable.Callback cb)

Bind a Drawable.Callback object to this Drawable.

给Drawable绑定Drawable.Callback对象 

voidsetChangingConfigurations(int configs)

Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.


voidsetColorFilter(int color, PorterDuff.Mode mode)

Specify a color and Porter-Duff mode to be the color filter for this drawable.

abstract voidsetColorFilter(ColorFilter colorFilter)

Specify an optional color filter for the drawable.

voidsetDither(boolean dither)

This method was deprecated in API level 23. This property is ignored.

voidsetFilterBitmap(boolean filter)

Set to true to have the drawable filter its bitmaps with bilinear sampling when they are scaled or rotated.

voidsetHotspot(float x, float y)

Specifies the hotspot's location within the drawable.

voidsetHotspotBounds(int left, int top, int right, int bottom)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

final booleansetLayoutDirection(int layoutDirection)

Set the layout direction for this drawable.

final booleansetLevel(int level)

Specify the level for the drawable.

booleansetState(int[] stateSet)

Specify a set of states for the drawable.

voidsetTint(int tintColor)

Specifies tint color for this drawable.

voidsetTintList(ColorStateList tint)

Specifies tint color for this drawable as a color state list.

voidsetTintMode(PorterDuff.Mode tintMode)

Specifies a tint blending mode for this drawable.

booleansetVisible(boolean visible, boolean restart)

Set whether this Drawable is visible.

voidunscheduleSelf(Runnable what)

Use the current Drawable.Callback implementation to have this Drawable unscheduled.


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CoordinatorLayout是Android Support Library中新增的一个布局容器,它继承自ViewGroup类。CoordinatorLayout的主要作用是协调子View之间的交互行为,可以让子View之间相互协调,实现一些比较复杂的交互效果。 1. 特点 - CoordinatorLayout可以作为根布局,内部可以包含多个子View。 - CoordinatorLayout可以通过设置各个子View之间的依赖关系,来实现各种复杂的交互效果。 - CoordinatorLayout默认会启用一个Behavior机制,通过设置Behavior可以对子View的布局和交互行为进行控制。 - CoordinatorLayout可以对子View进行移动、缩放、旋转等操作,实现各种炫酷的效果。 2. 使用 在使用CoordinatorLayout时,需要注意以下几点: - CoordinatorLayout必须作为根布局。 - 子View必须设置layout_behavior属性,用于指定Behavior。 - 子View之间可以通过设置app:layout_anchor属性和app:layout_anchorGravity属性来指定依赖关系。 - 子View的Behavior必须继承自CoordinatorLayout.Behavior类。 以下是一个简单的使用CoordinatorLayout的例子: ``` <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/image" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="200dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/image" android:scaleType="centerCrop" app:layout_collapseMode="parallax"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_add" app:layout_anchor="@id/appbar" app:layout_anchorGravity="bottom|end"/> </android.support.design.widget.CoordinatorLayout> ``` 在这个例子中,CoordinatorLayout作为根布局,内部包含了一个ImageView、一个AppBarLayout和一个FloatingActionButton。通过设置各个View之间的依赖关系和Behavior,可以实现以下效果: - ImageView和AppBarLayout可以滚动。 - 当AppBarLayout滚动到顶部时,其中的Toolbar会固定在顶部。 - FloatingActionButton会跟随AppBarLayout一起滚动,并且会停留在AppBarLayout的底部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值