[英]Getting resource ID or InputStream from ImageView Drawable
我正在开发GifView
类,扩展了android.view.ImageView
以在Android中显示动画gif。
问题是从XML布局获取android:src="@drawable/myGif"
以自动将其加载到Movie中。 当我重写setImageDrawable(Drawable drawable)
来拦截默认的android行为时,我有两种不同的方法来做到这一点:从@drawable/myGif
获取id资源并保存以供以后使用,或按需将其加载到Movie中。 但是使用后一种选项,我需要将Bitmap类中InputStream未使用的compress方法中的Drawable从Bitmap类转换为可以安全的GIF层。
我该怎么做?
public class GifView extends ImageView {
...
private Movie movie;
private int resId;
public GifView(Context context) {
super(context);
this.context = context;
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@Override
public void setImageDrawable(Drawable drawable) {
// ONE OF THOSE WAYS TO DO
// this.redId = ...
// this.movie = Movie.decodeStream(...);
}
...
}
好的,您可以自动执行此操作,但是确实可以付钱吗?
public class GifImageView extends ImageView {
private final static String TAG = "GifImageView";
public GifImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int[] attrArray = {
android.R.attr.src
};
TypedArray a = context.obtainStyledAttributes(attrs, attrArray);
int id = a.getResourceId(0, 0);
a.recycle();
if (id != 0) {
try {
Drawable d = new GifDrawable(getResources(), id);
setImageDrawable(d);
} catch (Exception e) {
Log.d(TAG, "GifImageView " + e.getMessage());
}
}
}
class GifDrawable extends Drawable implements Runnable {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.