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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android Drawable绘图学习笔记

發(fā)布時(shí)間:2023/12/19 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Drawable绘图学习笔记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如何獲取 res 中的資源
數(shù)據(jù)包package:android.content.res

主要類:Resources
Android SDK中的簡(jiǎn)介:Class for accessing an application’s resources.Class for accessing an application’s resources. This sits on top of the asset manager of the application (accessible through getAssets()) and provides a higher-level API for getting typed data from the assets.
其主要接口按照功能,劃分為以下三部分:

getXXXX()

例如:

int getColor(int id)

Drawable getDrawable(int id)

String getString(int id)? 直接獲取res中存放的資源

InputStream openRawResource(int id)? 獲取資源的數(shù)據(jù)流,讀取資源數(shù)據(jù)

void parseBundleExtras(XmlResourceParser parser, Bundle outBundle)? 從XML文件中獲取數(shù)據(jù)

Resource為每種資源提供了相應(yīng)的接口來(lái)獲取這種資源,除了可以直接獲取資源外,還額外提供了以數(shù)據(jù)流的方式獲取資源,這在以后的應(yīng)用程序開(kāi)發(fā)中會(huì)經(jīng)常使用,那么如何獲取Resources了,如下:Resources r = this.getContext().getResources();

如何獲取資源中的畫圖對(duì)象

數(shù)據(jù)包package:android.graphics.drawable
主要類:Drawable
Android SDK中的簡(jiǎn)介: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.
看了以上簡(jiǎn)介,發(fā)現(xiàn)Drawable是個(gè)virtual class,具體如何畫圖,需要具體分析Drawable的子類,例如:BitmapDrawable
Android SDK中的簡(jiǎn)介:A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object. It can be defined in an XML file with the?<bitmap>?element.
其主要接口如下:

BitmapDrawable()

BitmapDrawable(Bitmap bitmap)

BitmapDrawable(String filepath)

BitmapDrawable(InputStream is)

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 Bitmap getBitmap()

final Paint getPaint()

Drawable是個(gè)抽象類,在BitmapDrawable中我們就看到位圖的具體操作,在仔細(xì)看下BitmapDrawable的構(gòu)造函數(shù),我們就會(huì)發(fā)現(xiàn)與Resource中的openRawResource()接口是相對(duì)應(yīng)的,就可以通過(guò)以下方法來(lái)獲取位圖:
Resources r = this.getContext().getResources();
Inputstream is = r.openRawResource(R.drawable.my_background_image);
BitmapDrawable? bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();

Paint

數(shù)據(jù)包package:android.graphics

Android SDK中的簡(jiǎn)介:The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 主要就是定義:畫刷的樣式,畫筆的大小/顏色等。

Typeface
數(shù)據(jù)包 package:android.graphics
Android SDK中的簡(jiǎn)介:The Typeface class specifies the typeface and intrinsic style of a font. 主要就是定義:字體。

核心類顯示資源

數(shù)據(jù)包package:android.graphics
主要類:Canvas
Android SDK中的簡(jiǎn)介:The Canvas class holds the “draw” calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
按照結(jié)構(gòu)的功能,將主要接口分為以下3部分:

boolean clipXXXX() Region區(qū)域操作:DIFFERENCE INTERSECT REPLACE REVERSE_DIFFERENCE UNION XOR

void drawXXXX()畫圖函數(shù)

void rotate()? void scale()? void skew() void translate() 畫布操作函數(shù)

Region在這里需要特殊說(shuō)明下:Region就是一個(gè)區(qū)域,也就是畫布(Canvas)中的有效區(qū)域,在無(wú)效區(qū)域上draw,對(duì)畫布沒(méi)有任何改變。

Drawable類

Drawable是一個(gè)通用的抽象類,它的目的是告訴你什么東西是可以畫的。你會(huì)發(fā)現(xiàn)基于Drawable類擴(kuò)展出各種繪圖的類,見(jiàn)下面的表格,當(dāng)然你可以繼承它來(lái)創(chuàng)建你自己的繪圖類.

Interfaces

AnimatableInterface that drawables suporting animations should implement.
Drawable.CallbackImplement this interface if you want to create an animated drawable that extends?Drawable.

Classes

AnimationDrawableAn object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
BitmapDrawableA Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
ClipDrawableA Drawable that clips another Drawable based on this Drawable's current level value.
ColorDrawableA specialized Drawable that fills the Canvas with a specified color, with respect to the clip region.
DrawableA 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.
Drawable.ConstantState?
DrawableContainer?
DrawableContainer.DrawableContainerState?
GradientDrawableA Drawable with a color gradient for buttons, backgrounds, etc.
InsetDrawableA Drawable that insets another Drawable by a specified distance.
LayerDrawableA Drawable that manages an array of other Drawables.
LevelListDrawableA resource that manages a number of alternate Drawables, each assigned a maximum numerical value.
NinePatchDrawableA resizeable bitmap, with stretchable areas that you define.
PaintDrawableDrawable that draws its bounds in the given paint, with optional rounded corners.
PictureDrawableDrawable 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.

ScaleDrawableA Drawable that changes the size of another Drawable based on its current level value.
ShapeDrawableA Drawable object that draws primitive shapes.
ShapeDrawable.ShaderFactoryBase class defines a factory object that is called each time the drawable is resized (has a new width or height).
StateListDrawableLets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
TransitionDrawableAn extension of LayerDrawables that is intended to cross-fade between the first and second layer.

有三種方法可以定義和實(shí)例化一個(gè)Drawable:保存一個(gè)圖片到你工程資源中,使用XML文件來(lái)描述Drawable屬性或者用一個(gè)正常的類去構(gòu)造。下面我們將討論兩種技術(shù)(對(duì)一個(gè)有開(kāi)發(fā)經(jīng)驗(yàn)的開(kāi)發(fā)者來(lái)說(shuō)構(gòu)造并不是最新的技術(shù))。

從資源圖像文件中創(chuàng)建

一個(gè)比較簡(jiǎn)單的方法是添加一個(gè)圖片到你的程序中,然后通過(guò)資源文件引用這個(gè)文件,支持的文件類型有PNG(首選的) JPG(可接受的)GIF(不建議),顯然這種對(duì)于顯示應(yīng)用程序的圖標(biāo)跟來(lái)說(shuō)是首選的方法,也可以用來(lái)顯示LOGO,其余的圖片可以用在例如游戲中。

把一個(gè)圖片資源,添加你的文件到你工程中res/drawable/目錄中去,從這里,你就可以引用它到你的代碼或你的XML布局中,也就是說(shuō),引用它也可以用資源編號(hào),比如你選擇一個(gè)文件只要去掉后綴就可以了(例如:my_image.png 引用它是就是my_image)。

注意:SDK指出,為了縮小圖片的存儲(chǔ)空間,在Build的時(shí)候又可能對(duì)圖片進(jìn)行壓縮,如果不想被壓縮,可以將圖片放在res/raw/目錄中。

SDK給出的例子:

LinearLayout mLinearLayout;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Create a LinearLayout in which to add the ImageViewmLinearLayout = new LinearLayout(this);// Instantiate an ImageView and define its propertiesImageView i = new ImageView(this);i.setImageResource(R.drawable.my_image);i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensionsi.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// Add the ImageView to the layout and set the layout as the content viewmLinearLayout.addView(i);setContentView(mLinearLayout); }

獲取Drawable對(duì)象:

Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable.my_image);

?

注意:保持每個(gè)資源類型的一至,可以保證你項(xiàng)目狀態(tài)的一致性,就不用擔(dān)心有許多不同類型的對(duì)象來(lái)實(shí)例化它。例如:如果使用相同的圖像資源來(lái)實(shí)例化兩個(gè)Drawable對(duì)象。然后修改一個(gè)Drawables的屬性(例如alpha),然后不幸得是這個(gè)效果也會(huì)出現(xiàn)在另一個(gè)對(duì)象上去。所以當(dāng)處理同一個(gè)資源的多個(gè)實(shí)例對(duì)象時(shí),不是直接轉(zhuǎn)換為Drawable,而是應(yīng)該執(zhí)行tween animation

如何添加資源到ImageView:
<ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:tint="#55ff0000"android:src="@drawable/my_image"/> ?
從XML文件中創(chuàng)建

到如今,你應(yīng)該比較熟悉按Android的原則去開(kāi)發(fā)一個(gè)用戶接口,因此,你也應(yīng)該理解了定義一個(gè)XML文件對(duì)于對(duì)象的作用與靈活的重要性。這個(gè)理念無(wú)數(shù)次用于Drawables.

如果你想創(chuàng)建一個(gè)Drawable對(duì)象,而這個(gè)對(duì)象并不依賴于變量或用戶的交換,把它定義到XML中去應(yīng)該是一個(gè)不錯(cuò)的方法。即使你期望在你的應(yīng)用程序中改變其屬性來(lái)增加用戶體驗(yàn)。你應(yīng)該考慮把對(duì)象放入XML中,因?yàn)槟?strong>可以隨時(shí)修改其屬性。

當(dāng)你在你的XML中定義了一個(gè)Drawable,保存這個(gè)XML文件到你工程目錄下res/drawable目錄中,然后通過(guò)調(diào)用Resource.getDrawable()來(lái)檢索并實(shí)例化,傳遞給它XML文件中的資源ID號(hào)。任何Drawable的子類都支持inflate這個(gè)方法,這個(gè)方法會(huì)通過(guò)XML來(lái)實(shí)例化你的程序。任何Drawable都支持XML的擴(kuò)展來(lái)利用特殊的XML屬性來(lái)幫助定義對(duì)象的屬性,可以查看任何Drawable子類文檔來(lái)看如何定義XML文件。

如下定義了一個(gè)TransitionDrawable:An extension of LayerDrawables that is intended to cross-fade between the first and second layer. It can be defined in an XML file with the?<transition>?element. Each Drawable in the transition is defined in a nested?<item>. 有關(guān)TransitionDrawable的詳細(xì)信息查看http://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html。

將其定義在res/drawable/expand_collapse.xml:

<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/pic1"/><item android:drawable="@drawable/pic2"/> </transition>

下面實(shí)例化并處理:

public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Resources res=getResources();TransitionDrawable trans=(TransitionDrawable )res.getDrawable(R.drawable.expand_collapse);ImageView image = (ImageView)findViewById(R.id.ImageView01);image.setImageDrawable(trans);trans.startTransition(3000);} }
ShapeDrawable

當(dāng)你想去畫一些動(dòng)態(tài)的二維圖片,一個(gè)ShapeDrawable對(duì)象可能會(huì)對(duì)你有很大的幫助。通過(guò)ShapeDrawable,你可以通過(guò)編程畫出任何你想到的圖像與樣式。

ShapeDrawable繼承了Drawable, 所以你可以調(diào)用Drawable里有的函數(shù),比如視圖的背景,通過(guò)setBackgroundDrawable()設(shè)置。當(dāng)然,你可以在自定義的視圖布局中畫你的圖形,因?yàn)镾hapeDrawable有自己的draw()方法。你可以在View.OnDraw()方法期間創(chuàng)建一個(gè)視圖的子類去畫ShapeDrawable。

ShapeDrawable類(像很多其他Drawable類型在android.graphics.drawable包)允許你定義drawable公共方法的各種屬性。有些屬性你可以需要調(diào)整,包括透明度,顏色過(guò)濾,不透明度,顏色。

例子:

publicclassCustomDrawableViewextendsView{
???
privateShapeDrawable mDrawable;

???
publicCustomDrawableView(Context context){
???????
super(context);

???????
int x =10;
???????
int y =10;
???????
int width =300;
???????
int height =50;

??????? mDrawable
=newShapeDrawable(newOvalShape());
??????? mDrawable
.getPaint().setColor(0xff74AC23);
??????? mDrawable
.setBounds(x, y, x + width, y + height);
???
}

???
protectedvoid onDraw(Canvas canvas){
??????? mDrawable
.draw(canvas);
???
}

顯示:

CustomDrawableView mCustomDrawableView;

protectedvoid onCreate(Bundle savedInstanceState){
???
super.onCreate(savedInstanceState);
??? mCustomDrawableView
=newCustomDrawableView(this);
???
??? setContentView
(mCustomDrawableView);
}

在XML中定義方法:如果你想用XML文件配置來(lái)取代原有布局來(lái)畫自定義的drawable,于是你自定義的Drawable類必須重載view (Context, AttributeSet) 構(gòu)造函數(shù)。

<com.example.shapedrawable.CustomDrawableView
???
android:layout_width="fill_parent"
???
android:layout_height="wrap_content"
???
/>
NinePatchDrawable


NinePatchDrawable 繪畫的是一個(gè)可以伸縮的位圖圖像,Android會(huì)自動(dòng)調(diào)整大小來(lái)容納顯示的內(nèi)容。一個(gè)例子就是NinePatch為背景,使用標(biāo)準(zhǔn)的Android按鈕,按鈕必須伸縮來(lái)容納長(zhǎng)度變化的字符

NinePatchDrawable是一個(gè)標(biāo)準(zhǔn)的PNG圖像,它包括額外的1個(gè)像素的邊界,你必須保存它后綴為.9.png,并且保持到工程的res/drawable目錄中。

這個(gè)邊界是用來(lái)確定圖像的可伸縮和靜態(tài)區(qū)域。你可以在左邊和上邊的線上畫一個(gè)或多個(gè)黑色的1個(gè)像素指出可伸縮的部分(你可以需要很多可伸縮部分),它的相對(duì)位置在可伸縮部分相同,所以大的部分總是很大的。

你還有可以在圖像的右邊和下邊畫一條可選的drawable區(qū)域(有效的,內(nèi)邊距線)。如果你的視圖對(duì)象設(shè)置NinePath為背景然后指定特殊的視圖字體,它將自行伸縮使所有的文本來(lái)適應(yīng)根據(jù)右線與底部線設(shè)計(jì)好的區(qū)域(如果有的話),當(dāng)然內(nèi)邊距線不包括其中,Android可以使用左邊的線與上面的線來(lái)定義一個(gè)drawable區(qū)域。

我們來(lái)澄清一下這兩條不同的線,左邊跟頂部的線來(lái)定義哪些圖像的像素允許在伸縮時(shí)被復(fù)制。底部與右邊的線用來(lái)定義一個(gè)相對(duì)位置內(nèi)的圖像,視圖的內(nèi)容就放入其中。

使用方法:

<Buttonid="@+id/tiny"
???????
android:layout_width="wrap_content"
???????
android:layout_height="wrap_content"
???????
android:layout_alignParentTop="true"
???????
android:layout_centerInParent="true"
???????
android:text="Tiny"
???????
android:textSize="8sp"
???????
android:background="@drawable/my_button_background"/>

<Buttonid="@+id/big"
???????
android:layout_width="wrap_content"
???????
android:layout_height="wrap_content"
???????
android:layout_alignParentBottom="true"
???????
android:layout_centerInParent="true"
???????
android:text="Biiiiiiig text!"
???????
android:textSize="30sp"
???????
android:background="@drawable/my_button_background"/>

總結(jié)

以上是生活随笔為你收集整理的Android Drawable绘图学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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