Afianl加载网络图片(续)
上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用:
看效果图:
listview在滑动过程中没用明显卡顿,很流畅,这点优化的很不错,Afianl使用前当然是要先添加jar包啦,接下来看代码:
activity_main.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=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:dividerHeight="10dp" />
</RelativeLayout>
listview的条目布局list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>
MainActivity:
package com.example.afinaltest2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.tsz.afinal.FinalBitmap;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
ImageView img=null;
FinalBitmap finalBitMap=null;
ListView listview;
ListAdapter listAdapter;
HashMap<String, String> map ;
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] imgurl={
"http://a.hiphotos.baidu.com/image/pic/item/1f178a82b9014a90e68f8138ab773912b21bee86.jpg",
"http://f.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01faf8fd3dde2fe9925bc317d0b.jpg",
"http://imgt8.bdstatic.com/it/u=2,687769429&fm=25&gp=0.jpg",
"http://imgt6.bdstatic.com/it/u=2,687777173&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687769721&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687776524&fm=25&gp=0.jpg",
"http://h.hiphotos.baidu.com/image/pic/item/1b4c510fd9f9d72a2fd4db05d62a2834349bbb72.jpg",
"http://imgt6.bdstatic.com/it/u=2,687777467&fm=25&gp=0.jpg",
"http://a.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef4fb685fdbdfc451da80cb3eb7.jpg",
"http://d.hiphotos.baidu.com/image/pic/item/0b7b02087bf40ad141490d60552c11dfa8ecce80.jpg",
"http://g.hiphotos.baidu.com/image/pic/item/03087bf40ad162d9cc4ab20413dfa9ec8a13cd06.jpg",
"http://imgt7.bdstatic.com/it/u=2,687775967&fm=25&gp=0.jpg",
"http://imgt8.bdstatic.com/it/u=2,687775693&fm=25&gp=0.jpg",
"http://imgt9.bdstatic.com/it/u=2,686139825&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687769677&fm=25&gp=0.jpg",
"http://d.hiphotos.baidu.com/image/pic/item/0bd162d9f2d3572c22bf5b598813632763d0c3d2.jpg"
};
img=(ImageView) findViewById(R.id.img);
listview=(ListView) findViewById(R.id.listview);
for(int i=0;i<15;i++){
map = new HashMap<String, String>();
map.put("imgurl", imgurl[i]);
listItem.add(map);
}
listAdapter=new ListAdapter(this, listItem);
listview.setAdapter(listAdapter);
}
}
MainActivity未继承FianlActivity即未用注解方式,不过大家可以使用这种方式;
ListAdapter:
package com.example.afinaltest2;
import java.util.ArrayList;
import java.util.HashMap;
import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public FinalBitmap imageLoader;
public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=FinalBitmap.create(activity.getApplicationContext());
imageLoader.configLoadingImage(R.drawable.default_img);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
ImageView img = (ImageView) vi.findViewById(R.id.img);
HashMap<String, String> map = new HashMap<String, String>();
map = data.get(position);
imageLoader.display(img, map.get("imgurl"));
return vi;
}
}
其中,
imageLoader.configLoadingImage(R.drawable.default_img);
是设置图片加载未完成时显示的默认图片,最后依然不要忘了加权限。
使用Afianl框架时,不要只是将其中的方法拿来使用就算了,要学习它的编程思想,去思考为什么用这种方法,也可以指出它的不足之处,达到学以致用,而不是盲目的拿来主义。
Afianl加载网络图片(续)的更多相关文章
- Afianl加载网络图片(延续)
上一页"已经谈到了如何使用Afianl网络负载的图片和下载文件,本文将继续介绍使用Afinal使用网络负载图片,主绑定listview采用: 看效果图: listview在滑动过程中没用明显 ...
- 【WPF】wpf image控件加载网络图片不显示问题,
1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...
- 有关DTCoreText无法加载网络图片及应用问题
至于DTCoreText是干嘛的,不清楚的同学自行网上脑补,这就不啰嗦了,只说一下其用法. 里面有三种控件供大家使用,DTAttributedTextView, DTAttributedLabel 和 ...
- [原创]cocos2dx加载网络图片&异步加载图片
[动机] 之前看到一款卡牌游戏,当你要看全屏高清卡牌的时候,游戏会单独从网络上下载,本地只存了非高清的,这样可以省点包大小,所以我萌生了实现一个读取网络图片的类. [联想] 之前浏览网页的时候经常看到 ...
- SDWebImage 加载网络图片失败,重新运行,就能加载成功。
现象: 使用SDWebImage 加载网络图片,偶尔会有一两张图片就是显示不出来.重新运行有时又可以了. 这个问题的原因是: 当SDWebImage 在加载图片的时候 我用的是- (void)sd_s ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...
- 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放
http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
随机推荐
- 机器学习基石:16 Three Learning Principles
三个理论上界: 三个线性模型: 三个关键工具: 三条学习规则: 1.奥卡姆剃刀定律 先从简单模型开始, 训练后出现欠拟合, 再尝试复杂点模型. 2.采样误差 训练.验证.测试数据尽量同分布. 3.数据 ...
- Caffe的运行mnist手写数字识别
老规矩,首先附上官方教程:http://caffe.berkeleyvision.org/gathered/examples/mnist.html 1.必要软件 因为Caffe中使用的是Linux才能 ...
- POJ 3590 The shuffle Problem
Any case of shuffling of n cards can be described with a permutation of 1 to n. Thus there are total ...
- [Sdoi2016]征途
Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...
- bzoj 2560: 串珠子
Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不 ...
- 例10-7 uva10820(欧拉)
题意:输入n,要求满足1≤x,y≤n,且x,y互素的个数. 若输入2,则答案3为(1,1),(1,2),(2,1);所以欧拉函数求出所有数的phi值,除了1之外都加上phi值的2倍即可 通过推导: p ...
- HNOI2002 营业额统计(Splay Tree)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意: Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并 ...
- 【集训第三天·疯狂训练】哦,顺带学习了manacher
虽然说是疯狂训练吧,但是也没写多少题,就把伸展树的操作熟悉了一下,ac了5个题目. 一整天没啥可吐槽的,除了昨天在机房打游戏的某位朋友翻车后和教练谈了谈心2333 说题吧.. 1.BZOJ1208 H ...
- spaCy is a library for advanced natural language processing in Python and Cython:spaCy 工业级自然语言处理工具
spaCy is a library for advanced natural language processing in Python and Cython. spaCy is built on ...
- 谷歌发布 TensorFlow Serving
TensorFlow服务是一个灵活的,高性能的机器学习模型的服务系统,专为生产环境而设计. TensorFlow服务可以轻松部署新的算法和实验,同时保持相同的服务器体系结构和API. TensorFl ...