Android Roboguice Use Guide (4) linked bindings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.