所以,我正在使用Glide使用下面的代码从imgur加载照片。
try {
ViewTarget viewTarget = new ViewTarget<SubsamplingScaleImageView, BitmapDrawable>(image_view) {
@Override
public void onResourceReady(BitmapDrawable bitmap, Transition<? super BitmapDrawable> transition) {
this.view.setImage(ImageSource.bitmap(bitmap.getBitmap()));
}
};
GlideApp.with(this).load(post_to_show.getUrl()).into(viewTarget);
} catch (ClassCastException e){}
其中SubsamplingScaleImageView是 - 好 - 一个自定义ImageView扩展View。 但这并不重要。
它正常工作,直到我尝试加载this
之类的内容它有一个.jpg扩展名,但它是一个gif。
我收到此错误:
E / AndroidRuntime:致命异常:主要 处理:me.ekhaled667.ridit,PID:6202 java.lang.ClassCastException:com.bumptech.glide.load.resource.gif.GifDrawable无法强制转换为android.graphics.drawable.BitmapDrawable at me.ekhaled667.ridit.PostFragment $ 1.onResourceReady(PostFragment.java:257) 在com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:579) 在com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:549) 在com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:218) 在com.bumptech.glide.load.engine.EngineJob $ MainThreadCallback.handleMessage(EngineJob.java:324) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:163) 在android.app.ActivityThread.main(ActivityThread.java:6348) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:880) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
首先:为什么它没有被捕获作为例外?
第二:我怎么可能绕过这个,换句话说,知道这是一个gif而不是jpg并加载它?
答案 0 :(得分:0)
您可以使用此代码显示gif。
load=(ImageView) findViewById(R.id.loadimg);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(load);
Glide.with(this).load(post_to_show.getUrl()).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageViewTarget);
答案 1 :(得分:0)
请检查下面的代码,它可能会有所帮助:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.imageView)SubsamplingScaleImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
gifImage();
}
public void gifImage(){
ViewTarget viewTarget = new ViewTarget<SubsamplingScaleImageView,GifDrawable>(imageView) {
@Override
public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
this.view.setImage(ImageSource.bitmap(resource.getFirstFrame()));
}
};
Glide.with(this)
.asGif()
.load("https://metrouk2.files.wordpress.com/2018/03/giphy9.gif?w=748&h=561&crop=1")
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(viewTarget);
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
图片文件:
public class GlideActivity extends AppCompatActivity {
@BindView(R.id.imageView)ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glide);
ButterKnife.bind(this);
gifImage();
}
public void gifImage() {
Glide.with(this).load("http://imagesourceteam.com/wp-content/uploads/2017/01/rr.jpg").into(imageView);
}
}
activity_glide.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
答案 2 :(得分:0)
所以我想通过@Kaushal Gosaliya找到了一种方法,但是GIF只会显示为静态图像。
ViewTarget viewTarget = new ViewTarget<SubsamplingScaleImageView, Drawable>(image_view) {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
Bitmap mutableBitmap = Bitmap.createBitmap(resource.getIntrinsicWidth(), resource.getIntrinsicHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(mutableBitmap);
resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
resource.draw(canvas);
this.view.setImage(ImageSource.bitmap(mutableBitmap));
}
};
这是您要使用的ViewTarget,您可以将BitmapDrawable更改为Drawable以包含所有drawable。 然后将drawable转换为位图。