上一篇已经讲了如何利用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加载网络图片(续)的更多相关文章

  1. Afianl加载网络图片(延续)

    上一页"已经谈到了如何使用Afianl网络负载的图片和下载文件,本文将继续介绍使用Afinal使用网络负载图片,主绑定listview采用: 看效果图: listview在滑动过程中没用明显 ...

  2. 【WPF】wpf image控件加载网络图片不显示问题,

    1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...

  3. 有关DTCoreText无法加载网络图片及应用问题

    至于DTCoreText是干嘛的,不清楚的同学自行网上脑补,这就不啰嗦了,只说一下其用法. 里面有三种控件供大家使用,DTAttributedTextView, DTAttributedLabel 和 ...

  4. [原创]cocos2dx加载网络图片&异步加载图片

    [动机] 之前看到一款卡牌游戏,当你要看全屏高清卡牌的时候,游戏会单独从网络上下载,本地只存了非高清的,这样可以省点包大小,所以我萌生了实现一个读取网络图片的类. [联想] 之前浏览网页的时候经常看到 ...

  5. SDWebImage 加载网络图片失败,重新运行,就能加载成功。

    现象: 使用SDWebImage 加载网络图片,偶尔会有一两张图片就是显示不出来.重新运行有时又可以了. 这个问题的原因是: 当SDWebImage 在加载图片的时候 我用的是- (void)sd_s ...

  6. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  7. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

  8. 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放

    http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...

  9. android官方开源的高性能异步加载网络图片的Gridview例子

    这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...

随机推荐

  1. [CQOI 2011]动态逆序对

    Description 题库链接 对于序列 \(A\) ,它的逆序对数定义为满足 \(i<j\) ,且 \(A_i>A_j\) 的数对 \((i,j)\) 的个数.给 \(1\) 到 \( ...

  2. ●BZOJ 1076 [SCOI2008]奖励关

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1076题解: 期望dp. (模糊的题意,2333) 题中的:"现在决定不吃的宝物以后 ...

  3. hdu 5645 DZY Loves Balls

    DZY Loves Balls  Accepts: 659  Submissions: 1393  Time Limit: 4000/2000 MS (Java/Others)  Memory Lim ...

  4. bzoj 1899: [Zjoi2004]Lunch 午餐

    Description 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口)不同,所以 ...

  5. [poj1279]Art Gallery

    题意:求多边形的核的面积. 敲一下半平面交模板........  然后我wa了一早上就因为写了%lf  不知道poj什么破机制还不能用lf的,真的想跳楼 #include<iostream> ...

  6. [hdu5608]function

    题意:$\sum_{d|n}f(d)=n^{2}-3n+2$,求$\sum_{i=1}^{n}f(i)\mod 10^{9}+7$ , $n \leqslant 10^{9}$ $\left( T \ ...

  7. Ubuntu16 编译源码安装MXNet 可变卷积Deformable-ConvNets GPU版

    [引言]最近接手了公司的关于虫子识别的项目,使用MXNet框架开发,但是实际用的是Deformable-ConvNets. Deformable-ConvNets为微软研究研究院提出的可变卷积网络,可 ...

  8. .net如何引用System.Drawing.Drawing2D 命名空间和System.Drawing.Image及其相关概念

    其实这个很简单,直接在引用那里单击右键选择添加框架,然后找到System.Drawing就OK了, 其实并没有网上所说的那样需要下载什么Drawing.BLL. 首先Syetem.Drawing.Dr ...

  9. 【docker简易笔记】docker基础信息的分享

    docker 使用的频率越来越高,所以在后续的一些博客中会分享一些docker的安装和使用. 一.docker介绍   "Docker 最初是 dotCloud 公司创始人 Solomon ...

  10. 用 ConfigMap 管理配置 - 每天5分钟玩转 Docker 容器技术(159)

    Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...