Android RoboGuice使用指南(8):Provider Bindings
如果@Provides方法很复杂的话,可以将这些代码移动到单独的类中。这个类需要实现Guice的Provider 接口,该接口定义如下
[java] public inte易做图ce Provider<T> {
T get();
}
public inte易做图ce 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);
}
}
本例下载:/2012/0504/20120504100129894.zip
摘自 引路蜂移动软件
补充:移动开发 , Android ,