Last active
August 29, 2015 14:06
-
-
Save sohamtriveous/7cec605aaa9ac9d55a62 to your computer and use it in GitHub Desktop.
A simple ImageView that fades in/out when the image is changed, image loading powered by picasso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.triveous.recorder.ui.widget; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Target; | |
// optional log to benchmark load times | |
import hugo.weaving.DebugLog; | |
/** | |
* A simple ImageView that fades in and out when the enclosed image is changed | |
* How much of the old imagei shown before the new one is shown can be controlled by changing the FADEIN/OUT constants | |
* Sample invocation: | |
* Picasso.with(context).load(new File(filename)).resize(imageWidth, imageWidth).centerCrop().error(R.drawable.error).noFade().into((Target)listImageView); | |
*/ | |
public class ListImageView extends ImageView implements Target { | |
private static final int TIME_FADE_IN = 180; | |
private static final int TIME_FADE_OUT = 180; | |
public ListImageView(Context context) { | |
super(context); | |
} | |
public ListImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ListImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { | |
animate().alpha(0.0f).setDuration(TIME_FADE_OUT).setListener(new Animator.AnimatorListener() { | |
@Override | |
public void onAnimationStart(Animator animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
setImageBitmap(bitmap); | |
animate().alpha(1f).setDuration(TIME_FADE_IN); | |
} | |
@Override | |
public void onAnimationCancel(Animator animation) { | |
} | |
@Override | |
public void onAnimationRepeat(Animator animation) { | |
} | |
}); | |
} | |
@Override | |
public void onBitmapFailed(Drawable errorDrawable) { | |
} | |
@Override | |
public void onPrepareLoad(Drawable placeHolderDrawable) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment