您的位置:首页 > 移动开发 > Android开发

比较强大 优秀的开源框架 :Android图片加载与缓存:Android Glide 的用法

2015-11-19 17:22 751 查看
使用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中显示出来):

将下载下来的jar文件复制到这里:



package com.example.testglide;

import com.bumptech.glide.Glide;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
// 网络图片地址
private String Url = "http://img3.imgtn.bdimg.com/it/u=1183223528,3058066243&fm=21&gp=0.jpg";
private MyAdapter mMyAdapter;

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

ListView lv = (ListView) findViewById(R.id.lv);
mMyAdapter = new MyAdapter(this, -1);

lv.setAdapter(mMyAdapter);

}

private class MyAdapter extends ArrayAdapter {
private Context context;
LayoutInflater flater;

public MyAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
flater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return 100;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.list, null);

ImageView iv = (ImageView) convertView.findViewById(R.id.iv);

TextView tv = (TextView) convertView.findViewById(R.id.tv);

tv.setText(position + "");
// placeholder(R.drawable.jinchengtu)这个是加载本地图片 实现加载网络图片过渡用 优化用户体验
// crossFade(1000) 设置显示图片淡出时间

Glide.with(context).load(Url).placeholder(R.drawable.jinchengtu).crossFade(1000).into(iv);

return convertView;
}

}

}


xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testglide.MainActivity" >

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>


<?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" >

<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00" />

</LinearLayout>


运行效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: