比较强大 优秀的开源框架 :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文件复制到这里:
xml文件:
运行效果图:
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>
运行效果图:
相关文章推荐
- android canvas画图
- Android中Handler详解,线程间通信机制
- Gradle打包android 项目
- Android点住按钮同时再点击按钮外的效果
- 活动生命周期
- android拍照和本地选择图片
- Android各个support library特征和区别(官方文档)
- Google Nexus5关于Android 6.0源码的同步,编译和烧写
- android zxing二维码横屏改竖屏
- Android 输入系统之InputDispatcher篇
- Shape Drawable解析
- android layout_weight 用法
- Android的BroadcastReceiver和EventBus区别是什么
- Android设置TextView的Selector来控制点击的颜色
- Android 中的 Service 全面总结
- 与android不得不说的故事之KJFrameForAndroid
- 老李分享:android手机测试之适配(1)
- 关于安卓R文件丢失
- Android中常用的5大布局详述
- Gradle-Android 实战应用