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

如果@Provides方法很复杂的话,可以将这些代码移动到单独的类中。这个类需要实现Guice的Provider 接口,该接口定义如下

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


为一个generic 接口。

本例我们定义一个PathProvider,用于返回一个Path对象:

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的绑定:

bind(Path.class).toProvider(PathProvider.class);


然后使用绘制这个Path:

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);
 
 }
 
}



本例下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值