Android Roboguice Use Guide (7) @ provides Methods

Source: Internet
Author: User

The example above says that if you need to construct instances of more complex classes, the usual approach is to use the @provides method. This method must be defined in a module, and must use the @provides annotation, and the return type of the method is bound to the object instance returned by the method.

If this method has binding annotation or @named ("xxx"), Guice binds the object returned by the @provides method to this annotated type.

This example uses @provides to create three circles and then displays them on the screen, using the graphics library to see the Android Simple development Tutorial 12: Introduction to the two-dimensional graphic library of the Guide Bee and a color sample. In fact, creating a circle is not complicated, but it is used to illustrate the use of @provides methods.

Add three @provides methods in Graphics2dmodule:

@Provides @Named ("Circle1")     
ishape ProvideCircle1 () {return     
 new Ellipse (30,60,80,80);     
}          
@Provides @Named ("Circle2")     
ishape ProvideCircle2 () {return     
 new Ellipse (60,30,80,80);     
}     
          
@Provides @Named ("Circle3")     
ishape ProvideCircle3 () {return     
 new Ellipse (90,60,80,80);     
}

Bind to IShape, respectively, with callout @Named ("Circle1″"), @Named ("Circle2″"), @Named ("Circle3″").

Create Providesmethodsdemo, with the following code

public class Providesmethodsdemo extends graphics2dactivity{@Inject @Named ("Circle1") IShape circle1;     
 @Inject @Named ("Circle2") IShape circle2;     
          
 @Inject @Named ("Circle3") IShape Circle3;    protected void DrawImage () {//The solid (full opaque) red color in the ARGB space color Redcolor     
          
 = new Color (0xffff0000);     
          
The Semi-opaque green color in the ARGB spaces (alpha is 0x78) color Greencolor = new color (0x7800ff00,true); The Semi-opaque blue color in the ARGB spaces (alpha is 0x78) color Bluecolor = new Color (0X780000FF,     
          
true);     
          
The Semi-opaque yellow color in the ARGB spaces (alpha is 0x78) color Yellowcolor = new color (0x78ffff00,true);     
 The dash array int dasharray[] = {20, 8};     
 Graphics2d.clear (Color.White);     
 Graphics2d.reset ();     
 SolidBrush brush=new SolidBrush (Redcolor); Graphics2d.fill (Brush,ciRCLE1);     
 Brush=new SolidBrush (Greencolor);     
 Graphics2d.fill (Brush,circle2);     
 Pen pen=new Pen (yellowcolor,10,pen.cap_butt,pen.join_miter,dasharray,0);
 Brush=new SolidBrush (Bluecolor);
 Graphics2d.setpenandbrush (Pen,brush);
 Graphics2d.fill (NULL,CIRCLE3);

 Graphics2d.draw (NULL,CIRCLE3); }     
          
}

@Provides methods are commonly used to create complex class objects that can take parameters, and parameters can be injected into such as:

@Provides @Named ("Circle1")     
ishape provideCircle1 (@Named ("width") int width) {return     
 new Ellipse (30,60,width , width);     
}

This example downloads: Http://www.imobilebbs.com/download/android/roboguice/ProvidesMethodsDem o.zip

View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm

Related Article

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.