Android RoboGuice使用指南(7)@ Provides Methods

來源:互聯網
上載者:User

上例說過如果需要構造一些較複雜的類的執行個體,通常的方法是使用@Provides 方法。這個方法必須定義在模組中(Module),而且必須使用@Provides 標註,在 個方法的傳回型別則綁定到這個方法返回的對象執行個體。

如果這個方法帶有 binding Annotation或是@Named(“xxx”),Guice則將@Provides方法返回的對象 綁定到這個annotated 類型。

本例使用@Provides建立三個圓,然後再屏 幕上顯示出來,圖形庫的使用可以參見Android簡明開發教程十二:引路蜂二維圖 形庫簡介及顏色樣本 。其實建立圓並不複雜,這裡只是用來說明@Provides 方法 的用法。

在Graphics2DModule 在添加三個@Provides方法:

@Provides @Named("Circle1")     IShape provideCircle1(){      return new Ellipse(30,60,80,80);     }          @Provides @Named("Circle2")     IShape provideCircle2(){      return new Ellipse(60,30,80,80);     }               @Provides @Named("Circle3")     IShape provideCircle3(){      return new Ellipse(90,60,80,80);     }

分別綁定到IShape帶有標註@Named(“Circle1″),@Named(“Circle2 ″),@Named(“Circle3″).

建立ProvidesMethodsDemo,有如下代碼

public class ProvidesMethodsDemo extends Graphics2DActivity{                @Inject @Named("Circle1") IShape circle1;      @Inject @Named("Circle2") IShape circle2;      @Inject @Named("Circle3") IShape circle3;                protected void drawImage(){                // The solid (full opaque) red color in the ARGB space      Color redColor    = new Color(0xffff0000);                // The semi-opaque green color in the ARGB space (alpha is 0x78)      Color greenColor = new Color(0x7800ff00,true);               // The semi-opaque blue color in the ARGB space (alpha is 0x78)      Color blueColor = new Color(0x780000ff,true);               // The semi-opaque yellow color in the ARGB space ( alpha is 0x78)      Color yellowColor = new Color(0x78ffff00,true);               // The dash array      int dashArray[] = { 20 ,8 };      graphics2D.clear(Color.WHITE);      graphics2D.Reset();      SolidBrush brush=new SolidBrush(redColor);      graphics2D.fill(brush,circle1);      brush=new SolidBrush(greenColor);      graphics2D.fill(brush,circle2);      Pen pen=new Pen(yellowColor,10,Pen.CAP_BUTT,Pen.JOIN_MITER,dashArray,0);      brush=new SolidBrush(blueColor); graphics2D.setPenAndBrush(pen,brush); graphics2D.fill(null,circle3); graphics2D.draw(null,circle3); }               }

@Provides方法通常用來建立將複雜的類對象,可以帶參數,參數也可 以通過注入傳入比如:

@Provides @Named("Circle1")     IShape provideCircle1(@Named("width") int width){      return new Ellipse(30,60,width,width);     }

本例下載: http://www.imobilebbs.com/download/android/roboguice/ProvidesMethodsDem o.zip

查看全套文章:http://www.bianceng.cn/OS/extra/201301/34950.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.