[英]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.