android - 从网址

时间:2015-07-03 22:58:42

标签: android url layout bitmap background

我不认为这是重复的,因为我发现的所有答案都是几年前提出的。所以,我从URL加载位图然后这样做:

currentView.setBackground(new BitmapDrawable(result));

...其中“结果”是位图。

但是“BitmapDrawable”已弃用,它不适用于API 22或21。 是否有其他方法将Bitmap转换为Drawable或从URL而不是Bitmap加载drawable?

2 个答案:

答案 0 :(得分:0)

正如文档告诉你的那样 - 不要使用弃用的

new BitmapDrawable(result)

而是使用API​​ 18版本

new BitmapDrawable(getActivity().getResources(), result)

假设您支持API 18+,否则您需要根据运行时API处理这两个版本。

答案 1 :(得分:0)

使用Glide从URL加载图像,Glide是一个快速高效的Android开源媒体管理和图像加载框架,它将媒体解码,内存和磁盘缓存以及资源池包装成一个简单易用的界面。 / p>

Glide负责尽可能平滑地滚动任何图像列表,并接收,调整大小和显示远程图像。

有关更多信息,请参阅here

build.gradle中添加以下依赖项:

dependencies {
    compile 'com.github.bumptech.glide:glide:3.6.0'
    compile 'com.android.support:support-v4:19.1.0'
}

例如项目https://github.com/chrisbanes/cheesesquare

例如用法:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

    Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
}

load方法中,只需传递图片的网址,然后在into中传递视图,在您的情况下为currentview。 对于您的场景:

Glide.with(this).load("http://goo.gl/gEgYUd").into(currentView);