Android RoboGuice使用指南(8):Provider Bindings

來源:互聯網
上載者:User

如果@Provides方法很複雜的話,可以將這些代碼移動到單獨的類中。這個類需要實現Guice的Provider 介面,該介面定義如下

[java] public interface Provider<T> { 
 T get(); 

public interface Provider<T> {
 T get();
}

 

為一個generic 介面。

本例我們定義一個PathProvider,用於返回一個Path對象:

[java] public class PathProvider implements Provider<Path>{ 
  
 private String pathdata 
 = "M 60 20 Q -40 70 60 120 Q 160 70 60 20 z"; 
 @Override 
 public Path get() { 
 return Path.fromString(pathdata); 
 } 
  

public class PathProvider implements Provider<Path>{
 
 private String pathdata
 = "M 60 20 Q -40 70 60 120 Q 160 70 60 20 z";
 @Override
 public Path get() {
 return Path.fromString(pathdata);
 }
 
}

然後在Module中定義從Path類到Provider的綁定:

[java] bind(Path.class).toProvider(PathProvider.class); 
bind(Path.class).toProvider(PathProvider.class);

然後使用繪製這個Path:

[java] public class ProviderBindingsDemo extends Graphics2DActivity{ 
  
 @Inject Path path; 
  
 protected void drawImage(){ 
  
 AffineTransform mat1; 
  
 // Colors  
 Color redColor = new Color(0x96ff0000, true); 
 Color greenColor = new Color(0xff00ff00); 
 Color blueColor = new Color(0x750000ff, true); 
  
 mat1 = new AffineTransform(); 
 mat1.translate(30, 40); 
 mat1.rotate(-30 * Math.PI / 180.0); 
  
 // Clear the canvas with white color.  
 graphics2D.clear(Color.WHITE); 
  
 graphics2D.setAffineTransform(new AffineTransform()); 
 SolidBrush brush = new SolidBrush(greenColor); 
 graphics2D.fill(brush, path); 
 graphics2D.setAffineTransform(mat1); 
  
 brush = new SolidBrush(blueColor); 
 com.mapdigit.drawing.Pen pen 
 = new com.mapdigit.drawing.Pen(redColor, 5); 
 graphics2D.setPenAndBrush(pen, brush); 
 graphics2D.draw(null, path); 
 graphics2D.fill(null, path); 
  
 } 
  

 

本例下載:http://www.bkjia.com/uploadfile/2012/0504/20120504100129894.zip
 

 


摘自  引路蜂移動軟體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.