Universal-Image-Loader源码阅读(6)-core/imageaware/ViewAware

来源:互联网 发布:中英文转换器软件 编辑:程序博客网 时间:2024/05/21 11:18

ImageArare的抽象类实现


源码:

/** * Wrapper for Android {@link android.view.View View}. Keeps weak reference of View to prevent memory leaks. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.9.2 */public abstract class ViewAware implements ImageAware {public static final String WARN_CANT_SET_DRAWABLE = "Can't set a drawable into view. You should call ImageLoader on UI thread for it.";public static final String WARN_CANT_SET_BITMAP = "Can't set a bitmap into view. You should call ImageLoader on UI thread for it.";protected Reference<View> viewRef;//view的引用protected boolean checkActualViewSize;//是否检查view的大小/** * Constructor. <br /> * References {@link #ViewAware(android.view.View, boolean) ImageViewAware(imageView, true)}. * * @param view {@link android.view.View View} to work with */public ViewAware(View view) {this(view, true);}/** * Constructor * * @param view                {@link android.view.View View} to work with * @param checkActualViewSize <b>true</b> - then {@link #getWidth()} and {@link #getHeight()} will check actual *                            size of View. It can cause known issues like *                            <a href="https://github.com/nostra13/Android-Universal-Image-Loader/issues/376">this</a>. *                            But it helps to save memory because memory cache keeps bitmaps of actual (less in *                            general) size. *                            <p/> *                            <b>false</b> - then {@link #getWidth()} and {@link #getHeight()} will <b>NOT</b> *                            consider actual size of View, just layout parameters. <br /> If you set 'false' *                            it's recommended 'android:layout_width' and 'android:layout_height' (or *                            'android:maxWidth' and 'android:maxHeight') are set with concrete values. It helps to *                            save memory. */public ViewAware(View view, boolean checkActualViewSize) {if (view == null) throw new IllegalArgumentException("view must not be null");this.viewRef = new WeakReference<View>(view);//view的弱引用this.checkActualViewSize = checkActualViewSize;}/** * {@inheritDoc} * <p/> * Width is defined by target {@link android.view.View view} parameters, configuration * parameters or device display dimensions.<br /> * Size computing algorithm (go by steps until get non-zero value):<br /> * 1) Get the actual drawn <b>getWidth()</b> of the View<br /> * 2) Get <b>layout_width</b> */@Overridepublic int getWidth() {View view = viewRef.get();if (view != null) {final ViewGroup.LayoutParams params = view.getLayoutParams();int width = 0;if (checkActualViewSize && params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) {width = view.getWidth(); // Get actual image width}if (width <= 0 && params != null) width = params.width; // Get layout width parameterreturn width;}return 0;}/** * {@inheritDoc} * <p/> * Height is defined by target {@link android.view.View view} parameters, configuration * parameters or device display dimensions.<br /> * Size computing algorithm (go by steps until get non-zero value):<br /> * 1) Get the actual drawn <b>getHeight()</b> of the View<br /> * 2) Get <b>layout_height</b> */@Overridepublic int getHeight() {View view = viewRef.get();if (view != null) {final ViewGroup.LayoutParams params = view.getLayoutParams();int height = 0;if (checkActualViewSize && params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) {height = view.getHeight(); // Get actual image height}if (height <= 0 && params != null) height = params.height; // Get layout height parameterreturn height;}return 0;}@Overridepublic ViewScaleType getScaleType() {return ViewScaleType.CROP;//默认使用crop缩放模式}@Overridepublic View getWrappedView() {return viewRef.get();}@Overridepublic boolean isCollected() {return viewRef.get() == null;}@Overridepublic int getId() {View view = viewRef.get();return view == null ? super.hashCode() : view.hashCode();//用view的hashCode作为ID值返回}@Overridepublic boolean setImageDrawable(Drawable drawable) {if (Looper.myLooper() == Looper.getMainLooper()) {//尽在主线程中执行,才有可能返回trueView view = viewRef.get();if (view != null) {setImageDrawableInto(drawable, view);return true;}} else {L.w(WARN_CANT_SET_DRAWABLE);}return false;}@Overridepublic boolean setImageBitmap(Bitmap bitmap) {if (Looper.myLooper() == Looper.getMainLooper()) {View view = viewRef.get();if (view != null) {setImageBitmapInto(bitmap, view);return true;}} else {L.w(WARN_CANT_SET_BITMAP);}return false;}/** * Should set drawable into incoming view. Incoming view is guaranteed not null.<br /> * This method is called on UI thread. */protected abstract void setImageDrawableInto(Drawable drawable, View view);/** * Should set Bitmap into incoming view. Incoming view is guaranteed not null.< br /> * This method is called on UI thread. */protected abstract void setImageBitmapInto(Bitmap bitmap, View view);}


0 0
原创粉丝点击