Skip to content

Commit 7af7841

Browse files
committed
Extracted ViewAware from ImageViewAware, Java docs
1 parent 43a02f5 commit 7af7841

File tree

4 files changed

+227
-95
lines changed

4 files changed

+227
-95
lines changed

library/src/com/nostra13/universalimageloader/core/imageaware/ImageAware.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,27 @@ public interface ImageAware {
3636
/**
3737
* Returns width of image aware view. This value is used to define scale size for original image.
3838
* Can return 0 if width is undefined.<br />
39-
* Called on UI thread.
39+
* Is called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
4040
*/
4141
int getWidth();
4242

4343
/**
4444
* Returns height of image aware view. This value is used to define scale size for original image.
4545
* Can return 0 if height is undefined.<br />
46-
* Called on UI thread.
46+
* Is called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
4747
*/
4848
int getHeight();
4949

5050
/**
5151
* Returns {@linkplain com.nostra13.universalimageloader.core.assist.ViewScaleType scale type} which is used for
52-
* scaling image for this image aware view.
52+
* scaling image for this image aware view. Must <b>NOT</b> return <b>null</b>.
5353
*/
5454
ViewScaleType getScaleType();
5555

5656
/**
57-
* Returns wrapped Android {@link android.view.View View}. Can return <b>null</b> if no view is wrapped.<br />
58-
* Called on UI thread.
57+
* Returns wrapped Android {@link android.view.View View}. Can return <b>null</b> if no view is wrapped or view was
58+
* collected by GC.<br />
59+
* Is called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
5960
*/
6061
View getWrappedView();
6162

@@ -64,7 +65,7 @@ public interface ImageAware {
6465
* of task for this image aware view and fires
6566
* {@link com.nostra13.universalimageloader.core.listener.ImageLoadingListener#onLoadingCancelled(String,
6667
* android.view.View) ImageLoadingListener#onLoadingCancelled(String, View)} callback.<br />
67-
* May be called on UI thread.
68+
* Mey be called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
6869
*
6970
* @return <b>true</b> - if view is collected by GC and ImageLoader should stop processing this image aware view;
7071
* <b>false</b> - otherwise
@@ -84,25 +85,27 @@ public interface ImageAware {
8485

8586
/**
8687
* Sets image drawable into this image aware view.<br />
87-
* Called on UI thread to display drawable in this image aware view
88+
* Displays drawable in this image aware view
8889
* {@linkplain com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#showImageForEmptyUri(
8990
*android.graphics.drawable.Drawable) for empty Uri},
9091
* {@linkplain com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#showImageOnLoading(
9192
*android.graphics.drawable.Drawable) on loading} or
9293
* {@linkplain com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#showImageOnFail(
9394
*android.graphics.drawable.Drawable) on loading fail}. These drawables can be specified in
9495
* {@linkplain com.nostra13.universalimageloader.core.DisplayImageOptions display options}.<br />
95-
* Also can be called in {@link com.nostra13.universalimageloader.core.display.BitmapDisplayer BitmapDisplayer}.
96+
* Also can be called in {@link com.nostra13.universalimageloader.core.display.BitmapDisplayer BitmapDisplayer}.< br />
97+
* Is called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
9698
*
9799
* @return <b>true</b> if drawable was set successfully; <b>false</b> - otherwise
98100
*/
99101
boolean setImageDrawable(Drawable drawable);
100102

101103
/**
102104
* Sets image bitmap into this image aware view.<br />
103-
* Called on UI thread to display loaded and decoded image {@link android.graphics.Bitmap} in this image view aware.
105+
* Displays loaded and decoded image {@link android.graphics.Bitmap} in this image view aware.
104106
* Actually it's used only in
105-
* {@link com.nostra13.universalimageloader.core.display.BitmapDisplayer BitmapDisplayer}.
107+
* {@link com.nostra13.universalimageloader.core.display.BitmapDisplayer BitmapDisplayer}.< br />
108+
* Is called on UI thread if ImageLoader was called on UI thread. Otherwise - on background thread.
106109
*
107110
* @return <b>true</b> if bitmap was set successfully; <b>false</b> - otherwise
108111
*/

library/src/com/nostra13/universalimageloader/core/imageaware/ImageNonViewAware.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public ImageNonViewAware(ImageSize imageSize, ViewScaleType scaleType) {
4242
}
4343

4444
public ImageNonViewAware(String imageUri, ImageSize imageSize, ViewScaleType scaleType) {
45+
if (imageUri == null) throw new IllegalArgumentException("imageUri must not be null");
46+
if (imageSize == null) throw new IllegalArgumentException("imageSize must not be null");
47+
if (scaleType == null) throw new IllegalArgumentException("scaleType must not be null");
48+
4549
this.imageUri = imageUri;
4650
this.imageSize = imageSize;
4751
this.scaleType = scaleType;

library/src/com/nostra13/universalimageloader/core/imageaware/ImageViewAware.java

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
import android.graphics.Bitmap;
1919
import android.graphics.drawable.Drawable;
20-
import android.os.Looper;
21-
import android.view.ViewGroup;
20+
import android.view.View;
2221
import android.widget.ImageView;
2322
import com.nostra13.universalimageloader.core.assist.ViewScaleType;
2423
import com.nostra13.universalimageloader.utils.L;
2524

26-
import java.lang.ref.Reference;
27-
import java.lang.ref.WeakReference;
2825
import java.lang.reflect.Field;
2926

3027
/**
@@ -34,13 +31,7 @@
3431
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
3532
* @since 1.9.0
3633
*/
37-
public class ImageViewAware implements ImageAware {
38-
39-
public static final String WARN_CANT_SET_DRAWABLE = "Can't set a drawable into view. You should call ImageLoader on UI thread for it.";
40-
public static final String WARN_CANT_SET_BITMAP = "Can't set a bitmap into view. You should call ImageLoader on UI thread for it.";
41-
42-
protected Reference<ImageView> imageViewRef;
43-
protected boolean checkActualViewSize;
34+
public class ImageViewAware extends ViewAware {
4435

4536
/**
4637
* Constructor. <br />
@@ -49,7 +40,7 @@ public class ImageViewAware implements ImageAware {
4940
* @param imageView {@link android.widget.ImageView ImageView} to work with
5041
*/
5142
public ImageViewAware(ImageView imageView) {
52-
this(imageView, true);
43+
super(imageView);
5344
}
5445

5546
/**
@@ -70,87 +61,65 @@ public ImageViewAware(ImageView imageView) {
7061
* <p/>
7162
*/
7263
public ImageViewAware(ImageView imageView, boolean checkActualViewSize) {
73-
this.imageViewRef = new WeakReference<ImageView>(imageView);
74-
this.checkActualViewSize = checkActualViewSize;
64+
super(imageView, checkActualViewSize);
7565
}
7666

7767
/**
7868
* {@inheritDoc}
79-
* <p/>
80-
* Width is defined by target {@link ImageView view} parameters, configuration
81-
* parameters or device display dimensions.<br />
82-
* Size computing algorithm:<br />
83-
* 1) Get the actual drawn <b>getWidth()</b> of the View. If view haven't drawn yet then go
84-
* to step #2.<br />
85-
* 2) Get <b>layout_width</b>. If it hasn't exact value then go to step #3.<br />
69+
* <br />
8670
* 3) Get <b>maxWidth</b>.
8771
*/
8872
@Override
8973
public int getWidth() {
90-
ImageView imageView = imageViewRef.get();
91-
if (imageView != null) {
92-
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
93-
int width = 0;
94-
if (checkActualViewSize && params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) {
95-
width = imageView.getWidth(); // Get actual image width
74+
int width = super.getWidth();
75+
if (width <= 0) {
76+
ImageView imageView = (ImageView) viewRef.get();
77+
if (imageView != null) {
78+
width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check maxWidth parameter
9679
}
97-
if (width <= 0 && params != null) width = params.width; // Get layout width parameter
98-
if (width <= 0) width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check maxWidth parameter
99-
return width;
10080
}
101-
return 0;
81+
return width;
10282
}
10383

10484
/**
10585
* {@inheritDoc}
106-
* <p/>
107-
* Height is defined by target {@link ImageView view} parameters, configuration
108-
* parameters or device display dimensions.<br />
109-
* Size computing algorithm:<br />
110-
* 1) Get the actual drawn <b>getHeight()</b> of the View. If view haven't drawn yet then go
111-
* to step #2.<br />
112-
* 2) Get <b>layout_height</b>. If it hasn't exact value then go to step #3.<br />
113-
* 3) Get <b>maxHeight</b>.
86+
* <br />
87+
* 3) Get <b>maxHeight</b>
11488
*/
11589
@Override
11690
public int getHeight() {
117-
ImageView imageView = imageViewRef.get();
118-
if (imageView != null) {
119-
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
120-
int height = 0;
121-
if (checkActualViewSize && params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) {
122-
height = imageView.getHeight(); // Get actual image height
91+
int height = super.getHeight();
92+
if (height <= 0) {
93+
ImageView imageView = (ImageView) viewRef.get();
94+
if (imageView != null) {
95+
height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check maxHeight parameter
12396
}
124-
if (height <= 0 && params != null) height = params.height; // Get layout height parameter
125-
if (height <= 0) height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check maxHeight parameter
126-
return height;
12797
}
128-
return 0;
98+
return height;
12999
}
130100

131101
@Override
132102
public ViewScaleType getScaleType() {
133-
ImageView imageView = imageViewRef.get();
103+
ImageView imageView = (ImageView) viewRef.get();
134104
if (imageView != null) {
135105
return ViewScaleType.fromImageView(imageView);
136106
}
137-
return null;
107+
return super.getScaleType();
138108
}
139109

140110
@Override
141111
public ImageView getWrappedView() {
142-
return imageViewRef.get();
112+
return (ImageView) super.getWrappedView();
143113
}
144114

145115
@Override
146-
public boolean isCollected() {
147-
return imageViewRef.get() == null;
116+
protected void setImageDrawableInto(Drawable drawable, View view) {
117+
((ImageView) view).setImageDrawable(drawable);
148118
}
149119

150120
@Override
151-
public int getId() {
152-
ImageView imageView = imageViewRef.get();
153-
return imageView == null ? super.hashCode() : imageView.hashCode();
121+
protected void setImageBitmapInto(Bitmap bitmap, View view) {
122+
((ImageView) view).setImageBitmap(bitmap);
154123
}
155124

156125
private static int getImageViewFieldValue(Object object, String fieldName) {
@@ -167,32 +136,4 @@ private static int getImageViewFieldValue(Object object, String fieldName) {
167136
}
168137
return value;
169138
}
170-
171-
@Override
172-
public boolean setImageDrawable(Drawable drawable) {
173-
if (Looper.myLooper() == Looper.getMainLooper()) {
174-
ImageView imageView = imageViewRef.get();
175-
if (imageView != null) {
176-
imageView.setImageDrawable(drawable);
177-
return true;
178-
}
179-
} else {
180-
L.w(WARN_CANT_SET_DRAWABLE);
181-
}
182-
return false;
183-
}
184-
185-
@Override
186-
public boolean setImageBitmap(Bitmap bitmap) {
187-
if (Looper.myLooper() == Looper.getMainLooper()) {
188-
ImageView imageView = imageViewRef.get();
189-
if (imageView != null) {
190-
imageView.setImageBitmap(bitmap);
191-
return true;
192-
}
193-
} else {
194-
L.w(WARN_CANT_SET_BITMAP);
195-
}
196-
return false;
197-
}
198139
}

0 commit comments

Comments
 (0)