日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Drawable 详解

發(fā)布時(shí)間:2025/4/16 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Drawable 详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

public abstract class Drawable?
extends?Object?

java.lang.Object
????android.graphics.drawable.Drawable
Known direct subclasses? ?直接子類AdaptiveIconDrawable,?AnimatedImageDrawable,?AnimatedVectorDrawable,?BitmapDrawable,?ColorDrawable,?DrawableContainer,?DrawableWrapper,?GradientDrawable,?LayerDrawable,?NinePatchDrawable,?PictureDrawable,?ShapeDrawable,?VectorDrawable
Known indirect subclasses? ? ? 非直接子類AnimatedStateListDrawable,?AnimationDrawable,?ClipDrawable,?InsetDrawable,?LevelListDrawable,?PaintDrawable,?RippleDrawable,?RotateDrawable,?ScaleDrawable,?StateListDrawable,?TransitionDrawable

Drawable是 可以被畫的東西的通用抽象類,多數(shù)情況下Drawable一般用作為畫在屏幕上資源;Drawable類提供了一個(gè)通用的API去處理優(yōu)先的各種類型的可視化資源。與View不同,Drawble 沒有接收事件和用戶交互的能力。


除了簡單的繪畫,Drawable提供了大量的通用機(jī)制給客戶端去和正在被繪制的進(jìn)行交互。


  • ?setBounds(Rect)方法必須調(diào)用去告訴Drawable它應(yīng)該在哪里繪制和應(yīng)該繪制多大,所有Drawables應(yīng)該遵從請(qǐng)求的大小,通常只是簡單的縮放它們的畫像。客戶端可以用getIntrinsicHeight()?和getIntrinsicWidth()方法為Drawables找到偏好的大小?。
  • ?getPadding(Rect)?方法可以從一些Drawables 返回一些關(guān)于如何設(shè)計(jì)Drawables里面的內(nèi)容。例如,一個(gè)打算作為按鈕控件的Drawable 需要返回正確放置自身內(nèi)部標(biāo)簽的padding。
  • setState(int[])?方法允許客戶端告訴Drawable用那種狀態(tài)繪制,例如“focused”,“selected”,等。一些drawables會(huì)基于被選中的狀態(tài)來調(diào)整它們的畫像。
  • setLevel(int)?方法允許用戶提供簡單的持續(xù)控制,可以修改被展示的控制,例如,電池量和進(jìn)度條。一些drawables 會(huì)基于當(dāng)前的水平值修改它們的畫像。
  • Drawable可以通過用?Drawable.Callback接口回調(diào)它的客戶端程序來呈現(xiàn)動(dòng)畫,為了使動(dòng)畫工作,所有客戶端程序都應(yīng)該支持這樣的接口(?(通過?setCallback(Drawable.Callback))?)。一個(gè)簡單的實(shí)現(xiàn)方式是通過系統(tǒng)功能例如?setBackground(Drawable)?and?ImageView。


雖然通常在應(yīng)用中不可見,但Drawables 會(huì)以以下幾種形式顯示:

  • Bitmap: 最簡單的Drawable,一個(gè)PNG或者 JPEG圖像。
  • Nine Patch(通常叫9patch或者.9圖):? PNG 格式的擴(kuò)展,允許它指定關(guān)于怎樣拉伸和在里面放置內(nèi)容。
  • Vector(向量圖):? 定義在XML文件中,作為一組點(diǎn),線,和曲線 伴隨著他相關(guān)的顏色信息,這類的drawable可以無損地被縮放。
  • Shape: 包含簡單的繪制命令而非原生bitmap,允許它在一些情況下更好的調(diào)整大小。
  • Layers: 一個(gè)復(fù)合的drawable,在相互之間繪制多重的drawbles.
  • States: 一個(gè)復(fù)合的drawable,基于它的狀態(tài)在一組drawables中選擇一個(gè)。
  • Levels: 一個(gè)復(fù)合的drawable,基于它的水平值(level)在一組drawables中選擇一個(gè)。
  • Scale: 一個(gè)帶有唯一子drawable的復(fù)合drawable, 子drawable的整體大小是基于當(dāng)前的水平值(level)來修改的。

讀者可以自行寫相關(guān)的例子來加深體驗(yàn),在這里由于篇幅問題就不在這里寫例子了,有疑問的同學(xué)可以留言。


自定義drawables

所有版本Android允許Drawable類被擴(kuò)展和用在運(yùn)行時(shí)取代framework提供的drawable。API 24開始,自定義drawables類可以用在XML中。


注意:自定義drawable類只能在你的應(yīng)用包中使用,其他的應(yīng)用是不能取加載它們的。


最低限度,自定義drawable類必須實(shí)現(xiàn)Drawable的abstract方法和重載draw(Canvas)方法去繪制內(nèi)容。



自定義drawbles 類可以在XML中以多種形式使用:



用完整的類名作為XML元素的名稱。對(duì)于這個(gè)方法,自定義drawable類必須是public高層級(jí)的類。

? ?

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


????用drawable作為XML元素名并且從類的屬性指定完整路徑名。這個(gè)方法可以用在public 高層級(jí)類和public static 內(nèi)部類。

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


總結(jié)

Nested classes(嵌套的類

接口Drawable.Callback

如果想要?jiǎng)?chuàng)建一個(gè)動(dòng)畫繼承?Drawable?的drawable,就實(shí)現(xiàn)這個(gè)接口

Drawable.ConstantState

這個(gè)抽象類被Drawables用來存儲(chǔ)Drawables之間的共享狀態(tài)和數(shù)據(jù)


Public constructors (公共構(gòu)造函數(shù)

Drawable()


Public methods

voidapplyTheme(Resources.Theme?t)

Applies the specified theme to this Drawable and its children.

應(yīng)用指定的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)返回一個(gè)drawable的約束副本

final voidcopyBounds(Rect?bounds)

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

在指定的矩形(Rect)返回一個(gè)drawable的約束副本

static?DrawablecreateFromPath(String?pathName)

Create a drawable from file path name.

從路徑名(pathName)創(chuàng)建一個(gè)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.

這個(gè)方法在API P 中已經(jīng)廢棄了,請(qǐng)使用下面的方法

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.

從輸入流創(chuàng)建一個(gè)drawable,使用給定的資源和值去決定密度信息。

static?DrawablecreateFromStream(InputStream?is,?String?srcName)

Create a drawable from an inputstream

從輸入流創(chuàng)建一個(gè)drawable

static?DrawablecreateFromXml(Resources?r,?XmlPullParser?parser)

Create a drawable from an XML document.

從一個(gè)XML文件中創(chuàng)建一個(gè)drawable

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

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

從一個(gè)XML文檔用Resources.Theme創(chuàng)建一個(gè)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.

從一個(gè)XML文檔使用一個(gè)可選的Resources.Theme創(chuàng)建一個(gè)drawable

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

Create from inside an XML document.

從一個(gè)XML文檔內(nèi)創(chuàng)建

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).

在它的約束內(nèi)繪制,并遵從可選的效果,例如透明度alpha 和顏色過濾器

intgetAlpha()

Gets the current alpha value for the drawable.

獲取當(dāng)前透明度的值

final?RectgetBounds()

Return the drawable's bounds Rect.

返回drawable的約束矩形

Drawable.CallbackgetCallback()

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

返回當(dāng)前這個(gè)Drawable實(shí)現(xiàn)的Drawable.Callback

intgetChangingConfigurations()

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

返回drawable可能會(huì)改變的配置參數(shù),這需要drawable重建

ColorFiltergetColorFilter()

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

返回當(dāng)前的顏色過濾器,返回null如果沒設(shè)置的話

Drawable.ConstantStategetConstantState()

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

返回存有這個(gè)Drawable的共享狀態(tài)的?Drawable.ConstantState?實(shí)例。

DrawablegetCurrent()
RectgetDirtyBounds()

Return the drawable's dirty bounds Rect.

返回drawable的臟約束矩形

voidgetHotspotBounds(Rect?outRect)

Populates?outRect?with the hotspot bounds.

使用熱點(diǎn)約束填充外矩形

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.

查詢當(dāng)前的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.

調(diào)用此方法去獲取drawable去填充定義在繪畫區(qū)域的輪廓

booleangetPadding(Rect?padding)

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.

返回內(nèi)容與約束邊框的內(nèi)邊距

int[]getState()

Describes the current state, as a union of primitve states, such as?state_focused,state_selected, etc.

描述當(dāng)前狀態(tài),作為一個(gè)聯(lián)合的基本狀態(tài),例如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.

從某個(gè)主題樣式的XML資源填充這個(gè)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.

使用當(dāng)前?Drawable.Callback實(shí)現(xiàn)讓這個(gè)Drawable重新繪制。

booleanisAutoMirrored()

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

是否自動(dòng)生成鏡像

booleanisFilterBitmap()
booleanisStateful()

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

標(biāo)識(shí)是否根據(jù)狀態(tài)來改變它的顯示

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.

如果這個(gè)Drawable從狀態(tài)之間執(zhí)行轉(zhuǎn)換動(dòng)畫,詢問他立即調(diào)到當(dāng)前狀態(tài)并且跳過所有的動(dòng)畫。

Drawablemutate()

Make this drawable mutable.

讓這個(gè)Drawable易變的。

booleanonLayoutDirectionChanged(int layoutDirection)

Called when the drawable's resolved layout direction changes.

在drawable的布局方向改變時(shí)調(diào)用。

static intresolveOpacity(int op1,?int op2)

Return the appropriate opacity value for two source opacities.

在兩種不透明源中返回恰當(dāng)?shù)牟煌该鞫戎?/p>

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).

設(shè)置自動(dòng)鏡像

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對(duì)象?

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.


《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Drawable 详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。