zoukankan      html  css  js  c++  java
  • 图片加载与缓存利器(自动缓存)--第三方开源-- Glide

    Android Glide使用便利,短短几行简单明晰的代码,即可完成大多数图片从网络(或者本地)加载、显示的功能需求。

    使用Android Glide,需要先下载Android Glide的库,Android Glide在github上的项目主页:

    https://github.com/bumptech/glide 。

    实际的项目使用只需要到Glide的releases页面把jar包下载后导入到本地的libs里面即可直接使用。Glide的releases的页面地址:https://github.com/bumptech/glide/releases ,在此页面找到最新的jar包,下载后放到自己项目的libs中,比如glide 3.6.0库的jar包下载地址:https://github.com/bumptech/glide/releases/download/v3.6.0/glide-3.6.0.jar


     接下来是在自己的项目中具体使用,现在给出一个具体的使用例子加以简单说明(通过网络加载图片然后在ImageView中显示出来):

    MainActivity.java

     1 import com.bumptech.glide.Glide;
     2 
     3 import android.support.v7.app.ActionBarActivity;
     4 import android.view.LayoutInflater;
     5 import android.view.View;
     6 import android.view.ViewGroup;
     7 import android.widget.ArrayAdapter;
     8 import android.widget.ImageView;
     9 import android.widget.ListView;
    10 import android.app.Activity;
    11 import android.content.Context;
    12 import android.os.Bundle;
    13 
    14 public class MainActivity extends ActionBarActivity {
    15 
    16     private Activity mActivity;
    17     // 将从此URL加载网络图片。
    18     private String img_url = "http://img4.duitang.com/uploads/item/201306/10/20130610100354_3WrWN.gif";
    19 
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         mActivity = this;
    24 
    25         setContentView(R.layout.activity_main);
    26 
    27         ListView lv = (ListView) findViewById(R.id.listView);
    28         lv.setAdapter(new MyAdapter(this, R.layout.item));
    29     }
    30 
    31     private class MyAdapter extends ArrayAdapter {
    32 
    33         private int resource;
    34 
    35         public MyAdapter(Context context, int resource) {
    36             super(context, resource);
    37             this.resource = resource;
    38         }
    39 
    40         @Override
    41         public View getView(int position, View convertView, ViewGroup parent) {
    42             if (convertView == null) {
    43                 convertView = LayoutInflater.from(mActivity).inflate(resource,
    44                         null);
    45             }
    46 
    47             ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
    48 
    49             Glide.with(mActivity).load(img_url).centerCrop()
    50             /*
    51              * 缺省的占位图片,一般可以设置成一个加载中的进度GIF图,placeholder显示加载时的图片,crossDade(时间)代表淡入淡出
    52              */
    53             .placeholder(R.drawable.loading).crossFade().into(iv);
    54 
    55             return convertView;
    56         }
    57 
    58         @Override
    59         public int getCount() {
    60             // 假设加载的数据量很大
    61             return 10000;
    62         }
    63     }
    64 }

    activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <ListView
     7         android:id="@+id/listView"
     8         android:layout_width="match_parent"
     9         android:layout_height="wrap_content" >
    10     </ListView>
    11 
    12 </LinearLayout>

    item.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <ImageView
     8         android:id="@+id/imageView"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content" />
    11 
    12 </LinearLayout>
  • 相关阅读:
    Count and Say leetcode
    Find Minimum in Rotated Sorted Array II leetcode
    Find Minimum in Rotated Sorted Array leetcode
    Search in Rotated Sorted Array II leetcode
    search in rotated sorted array leetcode
    Substring with Concatenation of All Words
    Subsets 子集系列问题 leetcode
    Sudoku Solver Backtracking
    Valid Sudoku leetcode
    《如何求解问题》-现代启发式方法
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4978312.html
Copyright © 2011-2022 走看看