17
17
18
18
import android .graphics .Bitmap ;
19
19
import android .graphics .drawable .Drawable ;
20
- import android .os .Looper ;
21
- import android .view .ViewGroup ;
20
+ import android .view .View ;
22
21
import android .widget .ImageView ;
23
22
import com .nostra13 .universalimageloader .core .assist .ViewScaleType ;
24
23
import com .nostra13 .universalimageloader .utils .L ;
25
24
26
- import java .lang .ref .Reference ;
27
- import java .lang .ref .WeakReference ;
28
25
import java .lang .reflect .Field ;
29
26
30
27
/**
34
31
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
35
32
* @since 1.9.0
36
33
*/
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 {
44
35
45
36
/**
46
37
* Constructor. <br />
@@ -49,7 +40,7 @@ public class ImageViewAware implements ImageAware {
49
40
* @param imageView {@link android.widget.ImageView ImageView} to work with
50
41
*/
51
42
public ImageViewAware (ImageView imageView ) {
52
- this (imageView , true );
43
+ super (imageView );
53
44
}
54
45
55
46
/**
@@ -70,87 +61,65 @@ public ImageViewAware(ImageView imageView) {
70
61
* <p/>
71
62
*/
72
63
public ImageViewAware (ImageView imageView , boolean checkActualViewSize ) {
73
- this .imageViewRef = new WeakReference <ImageView >(imageView );
74
- this .checkActualViewSize = checkActualViewSize ;
64
+ super (imageView , checkActualViewSize );
75
65
}
76
66
77
67
/**
78
68
* {@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 />
86
70
* 3) Get <b>maxWidth</b>.
87
71
*/
88
72
@ Override
89
73
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
96
79
}
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 ;
100
80
}
101
- return 0 ;
81
+ return width ;
102
82
}
103
83
104
84
/**
105
85
* {@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>
114
88
*/
115
89
@ Override
116
90
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
123
96
}
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 ;
127
97
}
128
- return 0 ;
98
+ return height ;
129
99
}
130
100
131
101
@ Override
132
102
public ViewScaleType getScaleType () {
133
- ImageView imageView = imageViewRef .get ();
103
+ ImageView imageView = ( ImageView ) viewRef .get ();
134
104
if (imageView != null ) {
135
105
return ViewScaleType .fromImageView (imageView );
136
106
}
137
- return null ;
107
+ return super . getScaleType () ;
138
108
}
139
109
140
110
@ Override
141
111
public ImageView getWrappedView () {
142
- return imageViewRef . get ();
112
+ return ( ImageView ) super . getWrappedView ();
143
113
}
144
114
145
115
@ Override
146
- public boolean isCollected ( ) {
147
- return imageViewRef . get () == null ;
116
+ protected void setImageDrawableInto ( Drawable drawable , View view ) {
117
+ (( ImageView ) view ). setImageDrawable ( drawable ) ;
148
118
}
149
119
150
120
@ 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 );
154
123
}
155
124
156
125
private static int getImageViewFieldValue (Object object , String fieldName ) {
@@ -167,32 +136,4 @@ private static int getImageViewFieldValue(Object object, String fieldName) {
167
136
}
168
137
return value ;
169
138
}
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
- }
198
139
}
0 commit comments