One of the most commonly used bindings in Roboguice is linked bindings, which maps a type to its implementation. Here we use the class in the two-dimensional graphics library of the guide bee as an example, the use of the two-dimensional graphics library of the guide bee can be seen in the Android Concise development Tutorial Eight: The Guide bee two-dimensional graphic drawing instance function definition.
Use the following classes IShape, Rectangle, MyRectangle, MySQUARE, whose inheritance relationship is shown in the following illustration:
The following code maps IShape to MyRectangle
Public
class Graphics2dmodule extends abstractandroidmodule{
@Override
protected void Configure () {
Bind (Ishape.class). to (Myrectangle.class);
}
}
At this point, if you use Injector.getinstance (ishape.class) or injector you encounter a dependency on IShape place, it will use MyRectangle. You can map a type to any of its subclasses or to all classes that implement that type of interface. You can also map a real class (a non-interface) to its subclasses, such as
Bind (Myrectangle.class). to (Mysquare.class);
The following example uses the @Inject application IShape.
public class Linkedbindingsdemo extends
graphics2dactivity{
@Inject ishape shape;
protected void DrawImage () {
/**
* semi-opaque blue color in
* The ARGB spaces (Alpha is 0x78)/
Co Lor Bluecolor = new Color (0x780000ff,true);
/**
* The semi-opaque yellow color in the
* ARGB spaces (alpha is 0x78)
/
Color yellowcolor = new Col or (0x78ffff00,true);
/**
* The dash array
*
/int dasharray[] = {8};
Graphics2d.clear (color.white);
Graphics2d.reset ();
Pen pen=new Pen (Yellowcolor,10,pen.cap_butt,
pen.join_miter,dasharray,0);
SolidBrush brush=new SolidBrush (bluecolor);
Graphics2d.setpenandbrush (Pen,brush);
Graphics2d.fill (null,shape);
Graphics2d.draw (Null,shape);
}