猿问

如何使用滑行将图像下载到位图中?

ImageView使用Glide 将网址下载到中非常容易:


Glide

   .with(context)

   .load(getIntent().getData())

   .placeholder(R.drawable.ic_loading)

   .centerCrop()

   .into(imageView);

我想知道是否也可以下载到中Bitmap?我想下载到原始位图中,然后可以使用其他工具进行操作。我已经看过代码,看不到该怎么做。


POPMUISE
浏览 441回答 3
3回答

湖上湖

确保您使用的是最新版本implementation 'com.github.bumptech.glide:glide:4.9.0'科特林:Glide.with(this)&nbsp; &nbsp; &nbsp; &nbsp; .asBitmap()&nbsp; &nbsp; &nbsp; &nbsp; .load(imagePath)&nbsp; &nbsp; &nbsp; &nbsp; .into(object : CustomTarget<Bitmap>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageBitmap(resource)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onLoadCleared(placeholder: Drawable?) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this is called when imageView is cleared on lifecycle call or for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // some other reason.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if you are referencing the bitmap somewhere else too other than this imageView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // clear it here as you can no longer have the bitmap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })位图大小:如果要使用图像的原始大小,请使用上面的默认构造函数,否则可以将所需的大小传递给位图into(object : CustomTarget<Bitmap>(1980, 1080)Java:Glide.with(this)&nbsp; &nbsp; &nbsp; &nbsp; .asBitmap()&nbsp; &nbsp; &nbsp; &nbsp; .load(path)&nbsp; &nbsp; &nbsp; &nbsp; .into(new CustomTarget<Bitmap>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageBitmap(resource);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onLoadCleared(@Nullable Drawable placeholder) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });旧答案:随着&nbsp; compile 'com.github.bumptech.glide:glide:4.8.0'以下Glide.with(this)&nbsp; &nbsp; &nbsp; &nbsp; .asBitmap()&nbsp; &nbsp; &nbsp; &nbsp; .load(path)&nbsp; &nbsp; &nbsp; &nbsp; .into(new SimpleTarget<Bitmap>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageBitmap(resource);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });对于compile 'com.github.bumptech.glide:glide:3.7.0'及以下Glide.with(this)&nbsp; &nbsp; &nbsp; &nbsp; .load(path)&nbsp; &nbsp; &nbsp; &nbsp; .asBitmap()&nbsp; &nbsp; &nbsp; &nbsp; .into(new SimpleTarget<Bitmap>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageBitmap(resource);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });现在您可能会看到一个警告 SimpleTarget is deprecated原因:弃用SimpleTarget的主要目的是警告您诱使您违反Glide的API合同的方式。具体来说,一旦清除SimpleTarget,它并不会迫使您停止使用已加载的任何资源,这可能导致崩溃和图形损坏。将SimpleTarget仍然可以只要你确保你没有使用位图,一旦ImageView的清除使用。
随时随地看视频慕课网APP

相关分类

Android
我要回答