上例说过如果需要构造一些较复杂的类的实例,通常的方法是使用@Provides 方法。这个方法必须定义在模块中(Module),而且必须使用@Provides 标注,在个方法的返回类型则绑定到这个方法返回的对象实例。
如果这个方法带有binding Annotation
或是@Named(“xxx”),Guice则将@Provides方法返回的对象绑定到这个annotated 类型。
本例使用@Provides创建三个圆,然后再屏幕上显示出来,图形库的使用可以参见Android简明开发教程十二:引路蜂二维图形库简介及颜色示例 。其实创建圆并不复杂,这里只是用来说明@Provides 方法的用法。
在Graphics2DModule 在添加三个@Provides方法:
@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);
}
分别绑定到IShape带有标注@Named(“Circle1″),@Named(“Circle2″),@Named(“Circle3″).
创建ProvidesMethodsDemo,有如下代码
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);
}
}
@Provides方法通常用来创建将复杂的类对象,可以带参数,参数也可以通过注入传入比如:
@Provides @Named("Circle1")
IShape provideCircle1(@Named("width") int width){
return new Ellipse(30,60,width,width);
}
本例下载
分享到:
相关推荐
Android Apps for Absolute Beginners: Covering Android 7 by Wallace Jackson English | 6 Apr. 2017 | ISBN: 1484222679 | 484 Pages | PDF | 26.76 MB Get your first Android apps up and running with the ...
SBT 目前只兼容 2.11.x 为了libraryDependencies ++ = Seq ( " com.beachape " %% " provide " % " 0.0.1-SNAPSHOT ") 该库使用宏注释,因此您还需要在构建中。用法 import ...
card.io provides fast, easy credit card scanning in mobile apps. Stay up to date Please be sure to keep your app up to date with the latest version of the SDK. All releases follow semantic versioning....
SafetyNet provides services for analyzing the configuration of a particular device to verify that it passes the Android compatibility test. This repository consists of one client and two server ...
Chapter 7: Singular Value Decomposition Section III: Nonlinear Techniques Chapter 8: Nonlinear Systems Chapter 9: Unconstrained Optimization Chapter 10: Constrained Optimization Chapter 11: Iterative...
For ZIP and GZIP formats, 7-Zip provides a compression ratio that is 2-10 % better than the ratio provided by PKZip and WinZip Strong AES-256 encryption in 7z and ZIP formats Self-extracting ...
And plus, it provides more understandable DDMS logs for developers. Usage Traditional android.util.Log usage: public static final String TAG = "MyApp or MyClass name"; void myFunc(){ android.util
The fourth edition of Numerical Methods Using MATLAB® provides a clear and rigorous introduction to a wide range of numerical methods that have practical applications. The authors’ approach is to ...
Welly - clyang edition Introduction / 简介 Welly是一套运行在Mac上功能丰富的BBS软体, 我所维护的版本是... This edition provides new features and bug fixes reported by users. Special Thanks to: @ElvisChian
This is a guide to Android application developers on how to use Android Studio 6 to develop their apps. The first part is a guide on how to use the Code Editor in Android 6. The Code Editor provides ...
Android Espresso made more fluent :coffee: Check out my blog :squirrel: or say hi on Twitter. Overview Cortado provides a layer of abstraction above Espresso, so it's a bit easier to use. ...
Android Cookbook: Problems and Solutions for Android Developers by Ian F. Darwin English | 10 May 2017 | ASIN: B071VLZ9ZK | 838 Pages | AZW3 | 12 MB Jump in and build working Android apps with the ...
This book provides the first coherent presentation of this rapidly growing field, covering a vast range of methods and their applications, from basic theory to advanced methods. This fascinating text...
This book provides an introduction to the foundations of three-dimensional computer vision and describes recent contributions to the field. Geometric methods include linear and bundle adjustment based...
Dagger2是此项目中使用的依赖项注入框架,下面的代码显示了稍后用于注入依赖项的Dagger组件的初始化过程: @Module public class NetworkModule { @Provides @Singleton Retrofit provideRetrofit () { ...
A must-have collection of ready-to-use Android recipes! The popularity of Google Android devices is seemingly unstoppable and the Android 4 release offers, for the first time, a single OS solution for...
Android Studio 2.3 Development Essentials - Android 7 Edition by Neil Smyth English | 12 Mar. 2017 | ASIN: B06XKKYK66 | 1225 Pages | MOBI | 14.8 MB Fully updated for Android Studio 2.3 and Android 7,...
The First Practical, Hands-On Guide to Embedded System Programming for Android Today, embedded systems programming is a more valuable discipline than ever, driven by fast-growing, new fields such ...
WCDB 中文版本请参看这里 WCDB is an efficient, complete, easy-to-use mobile database framework used in the WeChat application....ORM (Object Relational Mapping): WCDB provides a fl