Android RoboGuice User Guide (7): @ Provides Methods

Source: Internet
Author: User

For example, if you need to construct instances of complex classes, the common method is to use the @ Provides method. This method must be defined in the Module and marked with @ Provides. The return type of each method is bound to the object instance returned by this method.

If this method has binding Annotation or @ Named ("xxx"), Guice binds the object returned by the @ Provides method to this annotated type.

In this example, use @ Provides to create three circles and display them on the screen. For details about how to use the graphics library, see tutorial 12 of simplified Android development: Introduction to the two-dimensional graphics library of the Web tracking bee and color samples. In fact, it is not complex to create a circle. Here it is only used to describe the use of the @ Provides method.

Add three @ Provides methods in Graphics2DModule:

[Java] @ 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 );
}
@ 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 with annotation @ Named ("Circle1"), @ Named ("Circle2"), @ Named ("Circle3") respectively ″).

Create ProvidesMethodsDemo with the following code:

[Java] 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 space (alpha is 0x78)
Color greenColor = new Color (0x7800ff00, true );

// The semi-opaque blue color in the ARGB space (alpha is 0x78)
Color blueColor = new Color (0x780000ff, true );

// The semi-opaque yellow color in the ARGB space (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 );

}

}
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 space (alpha is 0x78)
Color greenColor = new Color (0x7800ff00, true );
 
// The semi-opaque blue color in the ARGB space (alpha is 0x78)
Color blueColor = new Color (0x780000ff, true );
 
// The semi-opaque yellow color in the ARGB space (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 );
 
}
 
}
The @ Provides method is usually used to create complex class objects, which can contain parameters or input parameters through injection. For example:


[Java] @ Provides @ Named ("Circle1 ")
IShape provideCircle1 (@ Named ("width") int width ){
Return new Ellipse (30,60, width, width );
}






Download this example: http://www.bkjia.com/uploadfile/2012/0504/20120504095832813.zip





Excerpted from the mobile app



 

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.