forked from androidquery/androidquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wiki.txt
278 lines (163 loc) · 6.38 KB
/
Wiki.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
public void asyncHtml(){
//fetch Google's homepage in html
String url = "http://www.google.com";
aq.ajax(url, String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String html, AjaxStatus status) {
}
});
}
public void asyncJson(){
//perform a Google search in just a few lines of code
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
//successful ajax call, show status code and json content
Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
});
}
public void asyncBytes(){
//fetch a remote resource in raw bytes
String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";
aq.ajax(url, byte[].class, new AjaxCallback<byte[]>() {
@Override
public void callback(String url, byte[] object, AjaxStatus status) {
Toast.makeText(aq.getContext(), "bytes array:" + object.length, Toast.LENGTH_LONG).show();
}
});
}
public void asyncBitmap(){
//fetch a image over the network
String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";
aq.ajax(url, Bitmap.class, new AjaxCallback<Bitmap>() {
@Override
public void callback(String url, Bitmap object, AjaxStatus status) {
Toast.makeText(aq.getContext(), object.toString(), Toast.LENGTH_LONG).show();
}
});
}
public void asyncJson(){
//perform a Google search in just a few lines of code
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, this, "jsonCallback");
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status){
if(json != null){
//successful ajax call, show status code and json content
Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
public class MainApplication extends Application{
@Override
public void onCreate() {
//set the max number of concurrent network connections, default is 4
AjaxCallback.setNetworkLimit(8);
//set the max number of icons (image width <= 50) to be cached in memory, default is 20
BitmapAjaxCallback.setIconCacheLimit(20);
//set the max number of images (image width > 50) to be cached in memory, default is 20
BitmapAjaxCallback.setCacheLimit(40);
super.onCreate();
}
}
public void onDestroy(){
//stop all aync calls when current activity is exiting
AjaxCallback.cancel();
}
protected void onDestroy(){
super.onDestroy();
//clean the file cache when root activity exit
//the resulting total cache size will be less than 3M
AQUtility.cleanCacheAsync(this);
}
protected void onDestroy(){
super.onDestroy();
//clean the file cache with advance option
long triggerSize = 3000000; //starts cleaning when cache size is larger than 3M
long targetSize = 2000000; //remove the least recently used files until cache size is less than 2M
AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
}
public class MainApplication extends Application{
@Override
public void onLowMemory(){
//clear all memory cache when system is in low memory
//note that you can configure the max image cache count, see CONFIGURATION
BitmapAjaxCallback.clearCache();
}
}
String imageUrl = "http://www.vikispot.com/z/images/vikispot/android-w.png";
final int tint = 0x77AA0000;
aq.id(R.id.image1).image(imageUrl, true, true, new BitmapAjaxCallback(){
@Override
public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
iv.setImageBitmap(bm);
//do something to the bitmap
iv.setColorFilter(tint, PorterDuff.Mode.SRC_ATOP);
}
});
File file = new File(path);
aq.id(R.id.avatar).image(file, false, 300, new BitmapAjaxCallback(){
@Override
public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
iv.setImageBitmap(bm);
if(bm != null){
byte[] upload = toBytes(bm);
writeAvatar(upload);
newAvatar = upload;
}
}
});
public void async_post(){
//do a twiiter search with a http post
String url = "http://search.twitter.com/search.json";
Map<String, Object> params = new HashMap<String, Object>();
params.put("q", "androidquery");
aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject json, AjaxStatus status) {
showResult(json);
}
});
}
ArrayAdapter<JSONObject> aa = new ArrayAdapter<JSONObject>(this, R.layout.content_item_s, items){
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null){
view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.content_item_s, null);
}
JSONObject jo = getItem(position);
AQuery aq = new AQuery(view);
aq.id(R.id.name).text(jo.optString("titleNoFormatting", "No Title"));
aq.id(R.id.meta).text(jo.optString("publisher", ""));
JSONObject image = jo.optJSONObject("image");
String tb = null;
if(image != null){
tb = image.optString("tbUrl");
aq.id(R.id.tb).visible().image(tb);
}else{
aq.id(R.id.tb).gone();
}
return view;
}
};
aq.id(R.id.list).adapter(aa);
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable draw = getDrawable();
int vwidth = MeasureSpec.getSize(widthMeasureSpec);
int vheight = MeasureSpec.getSize(heightMeasureSpec);
if(draw != null){
double r = getRatio(draw);
vheight = (int) Math.ceil(vwidth * r);
}
setMeasuredDimension(vwidth, vheight);
}