源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android图片加载框架Glide的基本用法介绍

  • 时间:2021-04-17 04:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android图片加载框架Glide的基本用法介绍
[b]简介[/b] Glide是一款图片加载框架,可以在Android平台上以简单的方式加载和展示图片。
dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
}
[b]在清单文件中加入权限[/b]
<uses-permission android:name="android.permission.INTERNET" />
[b]加载图片[/b] http://sc.jb51.net/uploads/allimg/150709/14-150FZ94211O4.jpg 新建布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:id="@+id/load_image"
    android:layout_marginTop="10dp"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="加载图片"/>

  <ImageView
    android:layout_marginTop="10dp"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

MainActivity.java
package com.zhoujian.glide;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity
{

  private Button mLoad_image;
  private ImageView mImage;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
    clickEvent();
  }


  private void initViews()
  {
    mLoad_image = (Button)findViewById(R.id.load_image);
    mImage = (ImageView)findViewById(R.id.image);
  }

  private void clickEvent()
  {
    mLoad_image.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        String url= "http://sc.jb51.net/uploads/allimg/150709/14-150FZ94211O4.jpg";

        Glide.with(MainActivity.this).load(url).into(mImage);

      }
    });

  }
}
只要一行代码,就可以把图片加载进来 [img]http://files.jb51.net/file_images/article/201704/2017040114275724.gif[/img] 调用Glide.with()方法用于创建一个图片的实例。with方法可以接受Context、Activity和Fragment类型的参数。如果调用不在Activity中或者Fragment中,可以传入ApplicationContext。 Glide支持加载各种图片资源,包括网络图片、本地图片、应用资源、Uri对象等
// 加载本地图片
File file = new File(getExternalCacheDir() + "/demo.jpg");
Glide.with(this).load(file).into(imageView);

// 加载应用资源
Glide.with(this).load(R.drawable.image).into(imageView);

// 加载二进制流
byte[] image = getImageBytes();
Glide.with(this).load(image).into(imageView);

// 加载Uri对象
Uri imageUri = getImageUri();
Glide.with(this).load(imageUri).into(imageView);

占位图
  Glide.with(MainActivity.this)
     .load(url)
     .placeholder(R.mipmap.placeholder)
     .into(mImage);
[img]http://files.jb51.net/file_images/article/201704/2017040114275725.gif[/img] [b]异常占位图[/b]
//错误的图片地址
String url1 = "http://sc.net/uploads/allimg/150709/14-150FZ94211O4.jpg";

Glide.with(MainActivity.this)
    .load(url1)
    .placeholder(R.mipmap.placeholder)//加载占位图
    .error(R.mipmap.error)//异常占位图
    .into(mImage);
[img]http://files.jb51.net/file_images/article/201704/2017040114275826.png[/img] [b]指定图片格式[/b] Glide支持加载GIF图片的,而Picasso是不支持加载GIF图片的。 Glide内部会自动判断图片格式。比如这是一张GIF图片的URL地址:
[u]复制代码[/u] 代码如下:
https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg
String url2 = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg";

Glide.with(MainActivity.this)
   .load(url2)
   .placeholder(R.mipmap.placeholder)//加载占位图
   .error(R.mipmap.error)//异常占位图
   .into(mImage);
[img]http://files.jb51.net/file_images/article/201704/2017040114275827.gif[/img] [b]只允许加载静态图:asBitmap[/b]
String url2 = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg";

Glide.with(MainActivity.this)
   .load(url2)
   .asBitmap()//只允许加载静态图
   .placeholder(R.mipmap.placeholder)//加载占位图
   .error(R.mipmap.error)//异常占位图
   .into(mImage);
如果传入的是一个gif动态图,只会显示第一帧图片 [img]http://files.jb51.net/file_images/article/201704/2017040114275828.gif[/img] [b]只允许加载动态图:.asGif()[/b]
String url2 = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg";

 Glide.with(MainActivity.this)
    .load(url2)
    .asGif()//只允许加载动态图
    .placeholder(R.mipmap.placeholder)//加载占位图
    .error(R.mipmap.error)//异常占位图
    .into(mImage);

[img]http://files.jb51.net/file_images/article/201704/2017040114275827.gif[/img] [b]指定图片大小[/b] 使用Glide,我们就不用担心图片内存浪费,甚至是内存溢出的问题。 因为Glide从来都不会直接将图片的完整尺寸全部加载到内存中,而是用多少加载多少。Glide会自动判断ImageView的大小,然后只将这么大的图片像素加载到内存当中。 当然我们也可以指定图片的固定大小 当指定图片大小的时候,要把ImageView的宽高该为包裹内容
Glide.with(MainActivity.this)
   .load(url)
   .asBitmap()//只允许加载动态图
   .placeholder(R.mipmap.placeholder)//加载占位图
   .error(R.mipmap.error)//异常占位图
   .override(400, 300)
   .into(mImage);
加载效果: [img]http://files.jb51.net/file_images/article/201704/2017040114275929.png[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部