iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android的多媒体管理库Glide的基本使用示例
  • 999
分享到

Android的多媒体管理库Glide的基本使用示例

示例glide多媒体Android 2022-06-06 08:06:00 999人浏览 薄情痞子
摘要

Glide 是一个Android平台上的快速和高效的开源的多媒体资源管理库, 提供 多媒体文件的压缩,内存和磁盘缓存, 资源池的接口。 Glide 支持获取,解压展示视频, 图

Glide 是一个Android平台上的快速和高效的开源的多媒体资源管理库, 提供 多媒体文件的压缩,内存和磁盘缓存, 资源池的接口。
Glide 支持获取,解压展示视频, 图像和GIFs,  Glide有一个可弹性的api可以让开发者自定义网络栈技术, 默认使用HttpUrlConnection , 你可以替换为  Google's Volley或者 OkHttp

Glide 开始的目的是提供一个快速和平滑展示图片列表, 但是Glide对你需要拉取, resize 和展示远程的图片这些场景都是很管用的.

Glide最简单的使用案例就是从远程服务器或者本地文件系统加载图片,把它们放在磁盘与内存缓存中,然后加载到view上。它可以用在全市图片的app中,Glide为包含图片的滚动列表做了尽可能流畅的优化

对象池
Glide原理的核心是为bitmap维护一个对象池。对象池的主要目的是通过减少大对象的分配以重用来提高性能。

Dalvik和ART虚拟机都没有使用compacting garbage collector,compacting garbage collector是一种模式,这种模式中GC会遍历堆,同时把活跃对象移到相邻内存区域,让更大的内存块可以用在后续的分配中。因为安卓没有这种模式,就可能会出现被分配的对象分散在各处,对象之间只有很小的内存可用。如果应用试图分配一个大于邻近的闲置内存块空间的对象,就会导致OutOfMemoryError,然后崩溃,即使总的空余内存空间大于对象的大小。

使用对象池还可以帮助提高滚动的性能,因为重用bitmap意味着更少的对象被创建与回收。垃圾回收会导致“停止一切(Stop The World)”事件,这个事件指的是回收器执行期间,所有线程(包括UI线程)都会暂停。这个时候,图像帧无法被渲染同时UI可能会停滞,这在滚动期间尤其明显。

2016427141956445.gif (480×360)

下载
Glide的GitHub项目地址 https://github.com/bumptech/glide

可以在github上下载 release page.

或者使用Gradle:


repositories {
 MavenCentral()
}
dependencies {
  compile 'com.github.bumptech.glide:glide:3.3.+'
  compile 'com.android.support:support-v4:19.1.0'
}
repositories {
 mavenCentral()
}
dependencies {
  compile 'com.github.bumptech.glide:glide:3.3.+'
  compile 'com.android.support:support-v4:19.1.0'
}

Maven


<dependency>
 <groupId>com.github.bumptech.glide</groupId>
 <artifactId>glide</artifactId>
 <version>3.3.1</version>
 <type>aar</type>
</dependency>
<dependency>
 <groupId>com.google.android</groupId>
 <artifactId>support-v4</artifactId>
 <version>r7</version>
</dependency>
<dependency>
 <groupId>com.github.bumptech.glide</groupId>
 <artifactId>glide</artifactId>
 <version>3.3.1</version>
 <type>aar</type>
</dependency>
<dependency>
 <groupId>com.google.android</groupId>
 <artifactId>support-v4</artifactId>
 <version>r7</version>
</dependency>

使用

Glide使用起来很简单,而且不需要任何特别的配置就自动包含了bitmap pooling 。


DrawableRequestBuilder requestBuilder = Glide.with(context).load(imageUrl);
requestBuilder.into(imageView);

这就是加载一张图片的全部要求。就像安卓中的很多地方一样,with() 方法中的context到底是哪种类型是不清楚的。有一点很重要需要记住,就是传入的context类型影响到Glide加载图片的优化程度,Glide可以监视activity的生命周期,在activity销毁的时候自动取消等待中的请求。但是如果你使用Application context,你就失去了这种优化效果。

注:其实以上的代码是一种比较规范的写法,我们更熟悉的写法是:


Glide.with(context)
  .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
  .into(ivImg);

简单的例子


// For a simple view:
@Override
public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
  Glide.with(this).load("http://goo.gl/h8qOq7").into(imageView);
}
// For a list:
@Override
public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view,
        container, false);
  } else {
    myImageView = (ImageView) recycled;
  }
  String url = myUrls.get(position);
  Glide.with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .crossFade()
    .into(myImageView);
  return myImageView;
}
// For a simple view:
@Override
public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
  Glide.with(this).load("http://goo.gl/h8qOq7").into(imageView);
}
// For a list:
@Override
public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view,
        container, false);
  } else {
    myImageView = (ImageView) recycled;
  }
  String url = myUrls.get(position);
  Glide.with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .crossFade()
    .into(myImageView);
  return myImageView;
}

Volley

如果你想使用Volley:

Gradle


dependencies {
  compile 'com.github.bumptech.glide:volley-integration:1.0.+'
  compile 'com.mcxiaoke.volley:library:1.0.+'
}
dependencies {
  compile 'com.github.bumptech.glide:volley-integration:1.0.+'
  compile 'com.mcxiaoke.volley:library:1.0.+'
}

Maven:


<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>volley-integration</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>
<dependency>
  <groupId>com.mcxiaoke.volley</groupId>
  <artifactId>library</artifactId>
  <version>1.0.5</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>volley-integration</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>
<dependency>
  <groupId>com.mcxiaoke.volley</groupId>
  <artifactId>library</artifactId>
  <version>1.0.5</version>
  <type>aar</type>
</dependency>

然后在你的Activity或者程序中,注册Volley为基本模块


public void onCreate() {
 Glide.get(this).reGISter(GlideUrl.class, InputStream.class,
    new VolleyUrlLoader.Factory(yourRequestQueue));
 ...
}
public void onCreate() {
 Glide.get(this).register(GlideUrl.class, InputStream.class,
    new VolleyUrlLoader.Factory(yourRequestQueue));
 ...
}

OkHttp

Gradle:


dependencies {
  compile 'com.github.bumptech.glide:okhttp-integration:1.0.+'
  compile 'com.squareup.okhttp:okhttp:2.0.+'
}
dependencies {
  compile 'com.github.bumptech.glide:okhttp-integration:1.0.+'
  compile 'com.squareup.okhttp:okhttp:2.0.+'
}

Maven:


<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>okhttp-integration</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>
<dependency>
  <groupId>com.squareup.okhttp</groupId>
  <artifactId>okhttp</artifactId>
  <version>2.0.0</version>
  <type>jar</type>
</dependency>
<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>okhttp-integration</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>
<dependency>
  <groupId>com.squareup.okhttp</groupId>
  <artifactId>okhttp</artifactId>
  <version>2.0.0</version>
  <type>jar</type>
</dependency>

然后在你的Activity或者程序中,注册Volley为基本模块


public void onCreate() {
 Glide.get(this).register(GlideUrl.class, InputStream.class,
    new OkHttpUrlLoader.Factory(yourOkHttpClient));
 ...
}
public void onCreate() {
 Glide.get(this).register(GlideUrl.class, InputStream.class,
    new OkHttpUrlLoader.Factory(yourOkHttpClient));
 ...
}
您可能感兴趣的文章:Android关于Glide的使用(高斯模糊、加载监听、圆角图片)Android App中使用Glide加载图片的教程Android使用glide加载gif动画设置播放次数Android Glide的简单使用Android添加glide库报错Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解决Android中Glide加载库的图片缓存配置究极指南从源码分析Android的Glide库的图片加载流程及特点Android的Glide库加载图片的用法及其与Picasso的对比Android中Glide库的使用小技巧总结


--结束END--

本文标题: Android的多媒体管理库Glide的基本使用示例

本文链接: https://www.lsjlt.com/news/25097.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作