package pl.droidsonroids.gif;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.io.IOException;
/**
* An {@link ImageView} which tries treating background and src as {@link GifDrawable}
*
* @author koral--
*/
public class GifImageView extends ImageView {
static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";
/**
* A corresponding superclass constructor wrapper.
*
* @see ImageView#ImageView(Context)
*/
public GifImageView(Context context) {
super(context);
}
/**
* Like eqivalent from superclass but also try to interpret src and background
* attributes as {@link GifDrawable}.
*
* @see ImageView#ImageView(Context, AttributeSet)
*/
public GifImageView(Context context, AttributeSet attrs) {
super(context, attrs);
trySetGifDrawable(attrs, getResources());
}
/**
* Like eqivalent from superclass but also try to interpret src and background
* attributes as GIFs.
*
* @see ImageView#ImageView(Context, AttributeSet, int)
*/
public GifImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
trySetGifDrawable(attrs, getResources());
}
@Override
public void setImageResource(int resId) {
setResource(true, resId, getResources());
}
@Override
public void setBackgroundResource(int resId) {
setResource(false, resId, getResources());
}
void trySetGifDrawable(AttributeSet attrs, Resources res) {
if (attrs != null && res != null && !isInEditMode()) {
int resId = attrs.getAttributeResourceValue(ANDROID_NS, "src", -1);
if (resId > 0 && "drawable".equals(res.getResourceTypeName(resId))) {
setResource(true, resId, res);
}
resId = attrs.getAttributeResourceValue(ANDROID_NS, "background", -1);
if (resId > 0 && "drawable".equals(res.getResourceTypeName(resId))) {
setResource(false, resId, res);
}
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
//new method not avalilable on older API levels
void setResource(boolean isSrc, int resId, Resources res) {
try {
GifDrawable d = new GifDrawable(res, resId);
if (isSrc) {
setImageDrawable(d);
} else if (Build.VERSION.SDK_INT >= 16) {
setBackground(d);
} else {
setBackgroundDrawable(d);
}
return;
} catch (IOException ignored) {
//ignored
} catch (NotFoundException ignored) {
//ignored
}
if (isSrc) {
super.setImageResource(resId);
} else {
super.setBackgroundResource(resId);
}
}
/**
* Sets the content of this GifImageView to the specified Uri.
* If uri destination is not a GIF then {@link android.widget.ImageView#setImageURI(android.net.Uri)}
* is called as fallback.
* For supported URI schemes see: {@link android.content.ContentResolver#openAssetFileDescriptor(android.net.Uri,
* String)}.
* <code>file</code> scheme is always supported.
*
* @param uri The Uri of an image
*/
@Override
public void setImageURI(Uri uri) {
GifDrawable gd = null;
if (uri != null) {
final String scheme = uri.getScheme();
try {
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
gd = new GifDrawable(uri.getPath());
} else {
AssetFileDescriptor afd = getContext().getContentResolver()
.openAssetFileDescriptor(uri, "r");
if (afd != null) {
try {
gd = new GifDrawable(afd);
} catch (IOException ex) {
afd.close();
}
}
}
} catch (IOException ex) {
//ignored
}
}
if (gd != null) {
setImageDrawable(gd);
} else {
super.setImageURI(uri);
}
}
}