Skip to content

Commit

Permalink
Adding setDefaultImageDrawable and setErrorImageDrawable to NetworkIm…
Browse files Browse the repository at this point in the history
…ageView. (#267)
  • Loading branch information
Poligun authored and jpd236 committed Apr 8, 2019
1 parent 83ea07d commit dbfc439
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
71 changes: 62 additions & 9 deletions src/main/java/com/android/volley/toolbox/NetworkImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
Expand All @@ -32,25 +33,37 @@ public class NetworkImageView extends ImageView {

/**
* Resource ID of the image to be used as a placeholder until the network image is loaded. Won't
* be set at the same time as mDefaultImageBitmap.
* be set at the same time as mDefaultImageDrawable or mDefaultImageBitmap.
*/
private int mDefaultImageId;

/**
* Drawable of the image to be used as a placeholder until the network image is loaded. Won't be
* set at the same time as mDefaultImageId or mDefaultImageBitmap.
*/
@Nullable private Drawable mDefaultImageDrawable;

/**
* Bitmap of the image to be used as a placeholder until the network image is loaded. Won't be
* set at the same time as mDefaultImageId.
* set at the same time as mDefaultImageId or mDefaultImageDrawable.
*/
@Nullable Bitmap mDefaultImageBitmap;
@Nullable private Bitmap mDefaultImageBitmap;

/**
* Resource ID of the image to be used if the network response fails. Won't be set at the same
* time as mErrorImageBitmap.
* time as mErrorImageDrawable or mErrorImageBitmap.
*/
private int mErrorImageId;

/**
* Bitmap of the image to be used if the network response fails. Won't be set at the same time
* as mErrorImageId.
* as mErrorImageId or mErrorImageBitmap.
*/
@Nullable private Drawable mErrorImageDrawable;

/**
* Bitmap of the image to be used if the network response fails. Won't be set at the same time
* as mErrorImageId or mErrorImageDrawable.
*/
@Nullable private Bitmap mErrorImageBitmap;

Expand Down Expand Up @@ -100,43 +113,77 @@ public void setImageUrl(String url, ImageLoader imageLoader) {
* Sets the default image resource ID to be used for this view until the attempt to load it
* completes.
*
* <p>This will clear anything set by {@link NetworkImageView#setDefaultImageBitmap}.
* <p>This will clear anything set by {@link NetworkImageView#setDefaultImageBitmap} or {@link
* NetworkImageView#setDefaultImageDrawable}.
*/
public void setDefaultImageResId(int defaultImage) {
mDefaultImageBitmap = null;
mDefaultImageDrawable = null;
mDefaultImageId = defaultImage;
}

/**
* Sets the default image drawable to be used for this view until the attempt to load it
* completes.
*
* <p>This will clear anything set by {@link NetworkImageView#setDefaultImageResId} or {@link
* NetworkImageView#setDefaultImageBitmap}.
*/
public void setDefaultImageDrawable(@Nullable Drawable defaultImageDrawable) {
mDefaultImageId = 0;
mDefaultImageBitmap = null;
mDefaultImageDrawable = defaultImageDrawable;
}

/**
* Sets the default image bitmap to be used for this view until the attempt to load it
* completes.
*
* <p>This will clear anything set by {@link NetworkImageView#setDefaultImageResId}.
* <p>This will clear anything set by {@link NetworkImageView#setDefaultImageResId} or {@link
* NetworkImageView#setDefaultImageDrawable}.
*/
public void setDefaultImageBitmap(Bitmap defaultImage) {
mDefaultImageId = 0;
mDefaultImageDrawable = null;
mDefaultImageBitmap = defaultImage;
}

/**
* Sets the error image resource ID to be used for this view in the event that the image
* requested fails to load.
*
* <p>This will clear anything set by {@link NetworkImageView#setErrorImageBitmap}.
* <p>This will clear anything set by {@link NetworkImageView#setErrorImageBitmap} or {@link
* NetworkImageView#setErrorImageDrawable}.
*/
public void setErrorImageResId(int errorImage) {
mErrorImageBitmap = null;
mErrorImageDrawable = null;
mErrorImageId = errorImage;
}

/**
* Sets the error image drawable to be used for this view in the event that the image requested
* fails to load.
*
* <p>This will clear anything set by {@link NetworkImageView#setErrorImageResId} or {@link
* NetworkImageView#setDefaultImageBitmap}.
*/
public void setErrorImageDrawable(@Nullable Drawable errorImageDrawable) {
mErrorImageId = 0;
mErrorImageBitmap = null;
mErrorImageDrawable = errorImageDrawable;
}

/**
* Sets the error image bitmap to be used for this view in the event that the image requested
* fails to load.
*
* <p>This will clear anything set by {@link NetworkImageView#setErrorImageResId}.
* <p>This will clear anything set by {@link NetworkImageView#setErrorImageResId} or {@link
* NetworkImageView#setDefaultImageDrawable}.
*/
public void setErrorImageBitmap(Bitmap errorImage) {
mErrorImageId = 0;
mErrorImageDrawable = null;
mErrorImageBitmap = errorImage;
}

Expand Down Expand Up @@ -202,6 +249,8 @@ void loadImageIfNecessary(final boolean isInLayoutPass) {
public void onErrorResponse(VolleyError error) {
if (mErrorImageId != 0) {
setImageResource(mErrorImageId);
} else if (mErrorImageDrawable != null) {
setImageDrawable(mErrorImageDrawable);
} else if (mErrorImageBitmap != null) {
setImageBitmap(mErrorImageBitmap);
}
Expand Down Expand Up @@ -232,6 +281,8 @@ public void run() {
setImageBitmap(response.getBitmap());
} else if (mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
} else if (mDefaultImageDrawable != null) {
setImageDrawable(mDefaultImageDrawable);
} else if (mDefaultImageBitmap != null) {
setImageBitmap(mDefaultImageBitmap);
}
Expand All @@ -245,6 +296,8 @@ public void run() {
private void setDefaultImageOrNull() {
if (mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
} else if (mDefaultImageDrawable != null) {
setImageDrawable(mDefaultImageDrawable);
} else if (mDefaultImageBitmap != null) {
setImageBitmap(mDefaultImageBitmap);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;
Expand Down Expand Up @@ -90,8 +91,10 @@ public void publicMethods() throws Exception {

assertNotNull(
NetworkImageView.class.getMethod("setImageUrl", String.class, ImageLoader.class));
assertNotNull(NetworkImageView.class.getMethod("setDefaultImageDrawable", Drawable.class));
assertNotNull(NetworkImageView.class.getMethod("setDefaultImageBitmap", Bitmap.class));
assertNotNull(NetworkImageView.class.getMethod("setDefaultImageResId", int.class));
assertNotNull(NetworkImageView.class.getMethod("setErrorImageDrawable", Drawable.class));
assertNotNull(NetworkImageView.class.getMethod("setErrorImageBitmap", Bitmap.class));
assertNotNull(NetworkImageView.class.getMethod("setErrorImageResId", int.class));
}
Expand Down

0 comments on commit dbfc439

Please sign in to comment.