最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android中圖形圖像處理之drawable用法分析

 更新時(shí)間:2015年10月16日 12:10:12   作者:一葉飄舟  
這篇文章主要介紹了android中圖形圖像處理之drawable用法,較為詳細(xì)的分析了Android中繪圖所涉及的各種常用技巧與方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了android中圖形圖像處理之drawable用法。分享給大家供大家參考。具體如下:

一、如何獲取 res 中的資源

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

主要類:Resources

其主要接口按照功能,劃分為以下三部分:

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)的接口來獲取這種資源,除了可以直接獲取資源外,還額外提供了以數(shù)據(jù)流的方式獲取資源,這在以后的應(yīng)用程序開發(fā)中會經(jīng)常使用,那么如何獲取Resources了,如下:Resources r = this.getContext().getResources();
 
二、如何獲取資源中的畫圖對象

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

主要類:Drawable

Drawable是個(gè)virtual class,具體如何畫圖,需要具體分析Drawable的子類,例如:BitmapDrawable

其主要接口如下:

BitmapDrawable()
BitmapDrawable(Bitmap bitmap)
BitmapDrawable(String filepath)
BitmapDrawable(InputStream is)
void draw(Canvas canvas)
final Bitmap getBitmap()
final Paint getPaint()

Drawable是個(gè)抽象類,在BitmapDrawable中我們就看到位圖的具體操作,在仔細(xì)看下BitmapDrawable的構(gòu)造函數(shù),我們就會發(fā)現(xiàn)與Resource中的openRawResource()接口是相對應(yīng)的,就可以通過以下方法來獲取位圖:

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中的簡介: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中的簡介:The Typeface class specifies the typeface and intrinsic style of a font. 主要就是定義:字體。

核心類顯示資源

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

主要類:Canvas

Android SDK中的簡介: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在這里需要特殊說明下:Region就是一個(gè)區(qū)域,也就是畫布(Canvas)中的有效區(qū)域,在無效區(qū)域上draw,對畫布沒有任何改變。

Drawable類

Drawable是一個(gè)通用的抽象類,它的目的是告訴你什么東西是可以畫的。你會發(fā)現(xiàn)基于Drawable類擴(kuò)展出各種繪圖的類,見下面的表格,當(dāng)然你可以繼承它來創(chuàng)建你自己的繪圖類.
 
有三種方法可以定義和實(shí)例化一個(gè)Drawable:保存一個(gè)圖片到你工程資源中,使用XML文件來描述Drawable屬性或者用一個(gè)正常的類去構(gòu)造。下面我們將討論兩種技術(shù)(對一個(gè)有開發(fā)經(jīng)驗(yàn)的開發(fā)者來說構(gòu)造并不是最新的技術(shù))。

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

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

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

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

SDK給出的例子:

LinearLayout mLinearLayout;
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // Create a LinearLayout in which to add the ImageView
  mLinearLayout = new LinearLayout(this);
  // Instantiate an ImageView and define its properties
  ImageView i = new ImageView(this);
  i.setImageResource(R.drawable.my_image);
  i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
  i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  // Add the ImageView to the layout and set the layout as the content view
  mLinearLayout.addView(i);
  setContentView(mLinearLayout);
}獲取Drawable對象:
Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image); 

注意:保持每個(gè)資源類型的一至,可以保證你項(xiàng)目狀態(tài)的一致性,就不用擔(dān)心有許多不同類型的對象來實(shí)例化它。例如:如果使用相同的圖像資源來實(shí)例化兩個(gè)Drawable對象。然后修改一個(gè)Drawables的屬性(例如alpha),然后不幸得是這個(gè)效果也會出現(xiàn)在另一個(gè)對象上去。所以當(dāng)處理同一個(gè)資源的多個(gè)實(shí)例對象時(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的原則去開發(fā)一個(gè)用戶接口,因此,你也應(yīng)該理解了定義一個(gè)XML文件對于對象的作用與靈活的重要性。這個(gè)理念無數(shù)次用于Drawables.

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

當(dāng)你在你的XML中定義了一個(gè)Drawable,保存這個(gè)XML文件到你工程目錄下res/drawable目錄中,然后通過調(diào)用Resource.getDrawable()來檢索并實(shí)例化,傳遞給它XML文件中的資源ID號。任何Drawable的子類都支持inflate這個(gè)方法,這個(gè)方法會通過XML來實(shí)例化你的程序。任何Drawable都支持XML的擴(kuò)展來利用特殊的XML屬性來幫助定義對象的屬性,可以查看任何Drawable子類文檔來看如何定義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. */
  @Override
  public 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)你想去畫一些動態(tài)的二維圖片,一個(gè)ShapeDrawable對象可能會對你有很大的幫助。通過ShapeDrawable,你可以通過編程畫出任何你想到的圖像與樣式。

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

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

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

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

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

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

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

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

凭祥市| 莱西市| 桃江县| 苏尼特左旗| 台南县| 策勒县| 新郑市| 平利县| 定兴县| 巴里| 开封市| 广州市| 松潘县| 台北市| 贵定县| 城口县| 渭源县| 鄯善县| 深泽县| 眉山市| 永德县| 西宁市| 石城县| 确山县| 嘉义市| 商城县| 菏泽市| 唐海县| 海口市| 星座| 江油市| 龙泉市| 城步| 舞阳县| 卓资县| 东乌| 东宁县| 武穴市| 和龙市| 保康县| 广灵县|