/
WebImageView.java
executable file
·250 lines (213 loc) · 8.53 KB
/
WebImageView.java
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
/* Copyright (c) 2009 Matthias Kaeppler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wareninja.opensource.droidfu.widgets;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ViewSwitcher;
import android.widget.ImageView.ScaleType;
import com.wareninja.opensource.droidfu.cachefu.ImageCache;
import com.wareninja.opensource.droidfu.imageloader.ImageLoader;
import com.wareninja.opensource.droidfu.imageloader.ImageLoaderHandler;
/**
* An image view that fetches its image off the web using the supplied URL. While the image is being
* downloaded, a progress indicator will be shown.
*
* @author Matthias Kaeppler
*/
public class WebImageView extends ViewSwitcher {
private static final String XMLNS = "http://github.com/droidfu/schema";
private String imageUrl;
private boolean isLoaded;
private ProgressBar loadingSpinner;
private ImageView imageView;
private ScaleType scaleType = ScaleType.CENTER_INSIDE;//ScaleType.CENTER_CROP;
private Drawable progressDrawable;
/**
* @param context
* the view's current context
* @param imageUrl
* the URL of the image to download and show
* @param autoLoad
* Whether the download should start immediately after creating the view. If set to
* false, use {@link #loadImage()} to manually trigger the image download.
*/
public WebImageView(Context context, String imageUrl, boolean autoLoad) {
super(context);
initialize(context, imageUrl, null, autoLoad);
}
/**
* @param context
* the view's current context
* @param imageUrl
* the URL of the image to download and show
* @param progressDrawable
* the drawable to be used for the {@link ProgressBar} which is displayed while the
* image is loading
* @param autoLoad
* Whether the download should start immediately after creating the view. If set to
* false, use {@link #loadImage()} to manually trigger the image download.
*/
public WebImageView(Context context, String imageUrl, Drawable progressDrawable,
boolean autoLoad) {
super(context);
initialize(context, imageUrl, progressDrawable, autoLoad);
}
public WebImageView(Context context, AttributeSet attributes) {
super(context, attributes);
// TypedArray styles = context.obtainStyledAttributes(attributes,
// R.styleable.GalleryItem);
int progressDrawableId = attributes.getAttributeResourceValue(XMLNS,
"progressDrawable", 0);
Drawable progressDrawable = null;
if (progressDrawableId > 0) {
progressDrawable = context.getResources().getDrawable(progressDrawableId);
}
initialize(context, attributes.getAttributeValue(XMLNS, "imageUrl"),
progressDrawable, attributes.getAttributeBooleanValue(XMLNS, "autoLoad",
true));
// styles.recycle();
}
// YG: so that you can pass custom imageCache from your app
ImageCache preInitImageCache;
public ImageCache getPreInitImageCache() {
return preInitImageCache;
}
public void setPreInitImageCache(ImageCache preInitImageCache) {
this.preInitImageCache = preInitImageCache;
ImageLoader.initializeWithPreInit(preInitImageCache);
}
private void initialize(Context context, String imageUrl, Drawable progressDrawable,
boolean autoLoad) {
this.imageUrl = imageUrl;
this.progressDrawable = progressDrawable;
if (preInitImageCache!=null)
ImageLoader.initializeWithPreInit(preInitImageCache);
else
ImageLoader.initialize(context);
// ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
// 125.0f, preferredItemHeight / 2.0f);
// anim.setDuration(500L);
// AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
// anim.setDuration(500L);
// setInAnimation(anim);
addLoadingSpinnerView(context);
addImageView(context);
if (autoLoad && imageUrl != null) {
loadImage();
}
}
private void addLoadingSpinnerView(Context context) {
loadingSpinner = new ProgressBar(context);
loadingSpinner.setIndeterminate(true);
if (this.progressDrawable == null) {
this.progressDrawable = loadingSpinner.getIndeterminateDrawable();
} else {
loadingSpinner.setIndeterminateDrawable(progressDrawable);
if (progressDrawable instanceof AnimationDrawable) {
((AnimationDrawable) progressDrawable).start();
}
}
LayoutParams lp = new LayoutParams(progressDrawable.getIntrinsicWidth(), progressDrawable
.getIntrinsicHeight());
lp.gravity = Gravity.CENTER;
addView(loadingSpinner, 0, lp);
}
private void addImageView(Context context) {
imageView = new ImageView(context);
imageView.setScaleType(scaleType);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
addView(imageView, 1, lp);
}
/**
* Use this method to trigger the image download if you had previously set autoLoad to false.
*/
public void loadImage() {
if (imageUrl == null) {
throw new IllegalStateException(
"image URL is null; did you forget to set it for this view?");
}
ImageLoader.start(imageUrl, new DefaultImageLoaderHandler());
}
//added by YG: so that you can pass in 'custom handler'
public void loadImage(ImageLoaderHandler customHandler) {
if (imageUrl == null) {
throw new IllegalStateException(
"image URL is null; did you forget to set it for this view?");
}
ImageLoader.start(imageUrl, customHandler);
}
public ImageView getImageView(){
return this.imageView;
}
public String getImageUrl() {
return this.imageUrl;
}
public boolean isLoaded() {
return isLoaded;
}
public void setLoaded(boolean isLoaded) {
this.isLoaded = isLoaded;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
/**
* Often you have resources which usually have an image, but some don't. For these cases, use
* this method to supply a placeholder drawable which will be loaded instead of a web image.
*
* @param imageResourceId
* the resource of the placeholder image drawable
*/
public void setNoImageDrawable(int imageResourceId) {
imageView.setImageDrawable(getContext().getResources().getDrawable(imageResourceId));
setDisplayedChild(1);
}
@Override
public void reset() {
super.reset();
this.setDisplayedChild(0);
}
private class DefaultImageLoaderHandler extends ImageLoaderHandler {
public DefaultImageLoaderHandler() {
super(imageView, imageUrl);
}
@Override
protected boolean handleImageLoaded(Bitmap bitmap, Message msg) {
//bitmap = WareNinjaUtils.getRoundedCornerBitmap(bitmap);
//bitmap = WareNinjaUtils.getBitmapWithReflection(bitmap);
boolean wasUpdated = super.handleImageLoaded(bitmap, msg);
if (wasUpdated) {
isLoaded = true;
setDisplayedChild(1);
}
return wasUpdated;
}
}
@Override
public String toString() {
return "WebImageView [imageUrl=" + imageUrl + ", imageView="
+ imageView + ", isLoaded=" + isLoaded + ", loadingSpinner="
+ loadingSpinner + ", progressDrawable=" + progressDrawable
+ ", scaleType=" + scaleType + "]";
}
}