-
-
Save benvd/5683818 to your computer and use it in GitHub Desktop.
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.*; | |
import android.util.AttributeSet; | |
import com.android.volley.toolbox.NetworkImageView; | |
public class FadeInNetworkImageView extends NetworkImageView { | |
private static final int FADE_IN_TIME_MS = 250; | |
public FadeInNetworkImageView(Context context) { | |
super(context); | |
} | |
public FadeInNetworkImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public FadeInNetworkImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
public void setImageBitmap(Bitmap bm) { | |
TransitionDrawable td = new TransitionDrawable(new Drawable[]{ | |
new ColorDrawable(android.R.color.transparent), | |
new BitmapDrawable(getContext().getResources(), bm) | |
}); | |
setImageDrawable(td); | |
td.startTransition(FADE_IN_TIME_MS); | |
} | |
} |
Thanks for the example code, but would it not be more efficient to use an AlphaAnimation on the ImageView instead? Like this: https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/core/display/FadeInBitmapDisplayer.java
Just to note - this adds an extra layer of overdraw. You can use:
td.setCrossFadeEnabled(true);
to remove this extra overdraw.
thanks !
Any way to pinchzoom the network image view???
Creating a new array, ColorDrawable, and BitmapDrawable is very inefficient I would suggest to declare them as private variables and reuse them instead.
For starters this will eliminate one "new".
private static ColorDrawable cd = new ColorDrawable(android.R.color.transparent);
and then
TransitionDrawable td = new TransitionDrawable(new Drawable[]{
cd,
new BitmapDrawable(getContext().getResources(), bm)
});
ColorDrawable can be replaced by getDrawable() if setDefaultImageResId() was called before.
Very useful! Is possible to remove the effect once that an image has been viewed? In a list of images the effect is annoying when scroll down and scroll up
Thanks a lot for the gist. It's succinct and works perfectly!