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

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


Glide

   .with(context)

   .load(getIntent().getData())

   .placeholder(R.drawable.ic_loading)

   .centerCrop()

   .into(imageView);

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


梦里花落0921
浏览 439回答 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; });旧答案:随着 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的清除使用。

饮歌长啸

我对Glide不太熟悉,但是看起来如果您知道目标尺寸,则可以使用以下方法:Bitmap theBitmap = Glide.&nbsp; &nbsp; &nbsp; &nbsp; with(this).&nbsp; &nbsp; &nbsp; &nbsp; load("http://....").&nbsp; &nbsp; &nbsp; &nbsp; asBitmap().&nbsp; &nbsp; &nbsp; &nbsp; into(100, 100). // Width and height&nbsp; &nbsp; &nbsp; &nbsp; get();看来您可以通过-1,-1,并获得完整尺寸的图像(纯粹基于测试,看不到文档记录)。注意into(int,int)返回a FutureTarget<Bitmap>,因此您必须将其包装在try和catch块中,覆盖ExecutionException和InterruptedException。这是经过测试并可以正常工作的更完整的示例实现:class SomeActivity extends Activity {&nbsp; &nbsp; private Bitmap theBitmap = null;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; // onCreate stuff ...&nbsp; &nbsp; &nbsp; &nbsp; final ImageView image = (ImageView) findViewById(R.id.imageView);&nbsp; &nbsp; &nbsp; &nbsp; new AsyncTask<Void, Void, Void>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected Void doInBackground(Void... params) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Looper.prepare();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theBitmap = Glide.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with(SomeActivity.this).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; load("https://www.google.es/images/srpr/logo11w.png").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asBitmap().&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; into(-1,-1).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (final ExecutionException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.e(TAG, e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (final InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.e(TAG, e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(Void dummy) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null != theBitmap) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The full bitmap should be available here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.setImageBitmap(theBitmap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Image loaded");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }.execute();&nbsp; &nbsp; }}遵循下面的评论中Monkeyless的建议(这似乎也是官方方式),您可以使用SimpleTarget,可选地加上,override(int,int)以大大简化代码。但是,在这种情况下,必须提供确切的大小(不接受小于1的任何值):Glide&nbsp; &nbsp; .with(getApplicationContext())&nbsp; &nbsp; .load("https://www.google.es/images/srpr/logo11w.png")&nbsp; &nbsp; .asBitmap()&nbsp; &nbsp; .into(new SimpleTarget<Bitmap>(100,100) {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.setImageBitmap(resource); // Possibly runOnUiThread()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });如@hennry所建议,如果您需要相同的图像,则使用new SimpleTarget<Bitmap>()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android