1
1
package com .liuh .learn .rxjava2 ;
2
2
3
3
import android .content .Intent ;
4
+ import android .graphics .Bitmap ;
5
+ import android .graphics .drawable .BitmapDrawable ;
6
+ import android .graphics .drawable .Drawable ;
4
7
import android .support .v7 .app .AppCompatActivity ;
5
8
import android .os .Bundle ;
9
+ import android .util .Log ;
6
10
import android .view .View ;
7
11
8
12
import com .liuh .learn .rxjava2 .usecase .RxCaseConcatActivity ;
16
20
17
21
import butterknife .ButterKnife ;
18
22
import butterknife .OnClick ;
23
+ import io .reactivex .Observable ;
24
+ import io .reactivex .ObservableEmitter ;
25
+ import io .reactivex .ObservableOnSubscribe ;
26
+ import io .reactivex .Observer ;
27
+ import io .reactivex .android .schedulers .AndroidSchedulers ;
28
+ import io .reactivex .disposables .Disposable ;
29
+ import io .reactivex .functions .Consumer ;
30
+ import io .reactivex .schedulers .Schedulers ;
31
+
32
+ import static java .lang .Thread .sleep ;
19
33
20
34
public class MainActivity extends AppCompatActivity {
21
35
@@ -28,7 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {
28
42
29
43
@ OnClick ({R .id .btn_rx_operators , R .id .btn_a_simple_http_request , R .id .btn_http_request_concat ,
30
44
R .id .btn_http_request_flatmap , R .id .btn_http_request_zip , R .id .btn_http_request_interval ,
31
- R .id .btn_http_request_debounce , R .id .btn_thread_scheduler })
45
+ R .id .btn_http_request_debounce , R .id .btn_thread_scheduler , R .id .btn_novel_reader ,
46
+ R .id .btn_complex_use })
32
47
void onViewClicked (View view ) {
33
48
switch (view .getId ()) {
34
49
case R .id .btn_rx_operators :
@@ -55,7 +70,104 @@ void onViewClicked(View view) {
55
70
case R .id .btn_thread_scheduler :
56
71
startActivity (new Intent (this , RxCaseThreadSchedulerActivity .class ));
57
72
break ;
58
-
73
+ case R .id .btn_novel_reader :
74
+ novelAndReader ();
75
+ break ;
76
+ case R .id .btn_complex_use :
77
+ oneComplexUse ();
78
+ break ;
59
79
}
60
80
}
81
+
82
+ /**
83
+ * 观察者模式(连载小说和读者)
84
+ */
85
+ private void novelAndReader () {
86
+ Observable .create (new ObservableOnSubscribe <String >() {
87
+ @ Override
88
+ public void subscribe (ObservableEmitter <String > emitter ) throws Exception {
89
+ emitter .onNext ("连载1" );
90
+ sleep (3000 );
91
+ emitter .onNext ("连载2" );
92
+ emitter .onNext ("连载3" );
93
+
94
+ }
95
+ }).subscribeOn (Schedulers .io ())
96
+ .observeOn (AndroidSchedulers .mainThread ())
97
+ .subscribe (new Observer <String >() {
98
+ @ Override
99
+ public void onSubscribe (Disposable d ) {
100
+
101
+ }
102
+
103
+ @ Override
104
+ public void onNext (String s ) {
105
+ Log .e ("-----" , "Observer : onNext : " + s );
106
+ }
107
+
108
+ @ Override
109
+ public void onError (Throwable e ) {
110
+ Log .e ("-----" , "Observer : onError" );
111
+ }
112
+
113
+ @ Override
114
+ public void onComplete () {
115
+ Log .e ("-----" , "Observer : onComplete" );
116
+ }
117
+ });
118
+ }
119
+
120
+
121
+ private int [] drawables = {R .drawable .icon_1 , R .drawable .icon_2 , R .drawable .icon_3 , R .drawable .icon_4 , R .drawable .icon_5 ,
122
+ R .drawable .icon_6 , R .drawable .icon_7 , R .drawable .icon_8 , R .drawable .icon_9 , R .drawable .icon_10 };
123
+
124
+ /**
125
+ * 一个复杂一点的应用场景
126
+ */
127
+ private void oneComplexUse () {
128
+
129
+ Observable .create (new ObservableOnSubscribe <Drawable >() {
130
+ @ Override
131
+ public void subscribe (ObservableEmitter <Drawable > emitter ) throws Exception {
132
+ for (int i = 0 ; i < drawables .length ; i ++) {
133
+ Drawable drawable = getResources ().getDrawable (drawables [i ]);
134
+
135
+ // 第六张图片延时3秒后加载
136
+ if (5 == i ) {
137
+ sleep (3000 );
138
+ }
139
+
140
+ // 复制第七张图片到sd卡
141
+ if (6 == i ) {
142
+ Bitmap bitmap = ((BitmapDrawable ) drawable ).getBitmap ();
143
+ saveBitmap (bitmap , "test.png" , Bitmap .CompressFormat .PNG );
144
+ }
145
+
146
+ // 上传到网络
147
+ if (7 == i ) {
148
+ uploadIcon (drawable );
149
+ }
150
+ emitter .onNext (drawable );
151
+ }
152
+ emitter .onComplete ();
153
+ }
154
+ }).subscribeOn (Schedulers .io ())
155
+ .subscribeOn (AndroidSchedulers .mainThread ())
156
+ .subscribe (new Consumer <Drawable >() {
157
+ @ Override
158
+ public void accept (Drawable drawable ) throws Exception {
159
+ // 回调后在UI界面上展示出来
160
+
161
+ }
162
+ });
163
+
164
+ }
165
+
166
+ private void saveBitmap (Bitmap bitmap , String s , Bitmap .CompressFormat png ) {
167
+
168
+ }
169
+
170
+ private void uploadIcon (Drawable drawable ) {
171
+
172
+ }
61
173
}
0 commit comments