这是昨天改进后的,我测试了下,可以加载图片到5万张,估计5万以上也是没问题的,我只试到5万,其实也没必要这么高,现实中1000左右就差不多了,不过我的应用到100就差不多了,

package com.lanlong.test;

import java.io.File;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class AndrodTestActivity extends Activity {
    
    int number = 50000;

Drawable[] array;
    Bitmap [] array2;
    private Map<String, SoftReference<Drawable>> imageCache =
            new HashMap<String, SoftReference<Drawable>>();

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String iconPath = Environment.getExternalStorageDirectory()
                +"/vpn/Bafangtong/image/weixin.png";
        
//        array = new Drawable[number];//BitmapDrawable 一样

//        array2 = new Bitmap[number];
        File mfile = new File(iconPath);
        Log.i("","mfile.exists() = "+mfile.exists());
        
        Drawable draw = null;
        long timeBegin = System.currentTimeMillis();
        
        for(int i = 0; i < number; i++)

{

Log.e("", "测试第" + (i+1) + "张图片");
            // time = 2second 10000张    8second 50000张  不到1秒,    29ms 100张
//            array[i] = getResources().getDrawable(R.drawable.ic_launcher);//1000 ok
//            array2[i] = BitmapFactory.decodeFile(iconPath);//max 222
//            zoomImg(array2[i], 800, 480);
//            array[i] = Drawable.createFromPath(iconPath);//max 221
//            array[i] = BitmapDrawable.createFromPath(iconPath);//max  221
            
            
         //time = 149second  ,10000张  ; 1second ,100张 ; 901second ,50000张
            addDrawableToCache(""+i, iconPath);
            draw = getDrawableByPath(""+i, iconPath);
//            array[i] = getBitmapByPath(""+i, iconPath);
            Log.i("","draw = "+draw);
            try {
                
//                array[i] = Drawable.createFromStream(
//                        new FileInputStream(iconPath), iconPath);//max 111
                
//                array[i] = BitmapDrawable.createFromStream(
//                        new FileInputStream(iconPath), iconPath);//max 111
                
                
//                array2[i] = BitmapFactory.decodeStream(
//                        new FileInputStream(iconPath));//max 221 ;
//                Log.i("", "array["+i+"] = "+array[i]);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        long timeEnd = System.currentTimeMillis();
        Log.v("","time = "+(timeEnd - timeBegin)/1000 +" second");
    }
      public void addDrawableToCache(String id, String path) {
            // 强引用的Bitmap对象
            Drawable drawable = Drawable.createFromPath(path);
            // 软引用的Bitmap对象
            SoftReference<Drawable> softDrawable = new SoftReference<Drawable>(drawable);
            // 添加该对象到Map中使其缓存
            imageCache.put(id, softDrawable);
            Log.w("","add, imageCache.size() = "+imageCache.size());
        }
      
      public Drawable getDrawableByPath(String id, String path) {
            // 从缓存中取软引用的Bitmap对象
            SoftReference<Drawable> softDrawable = imageCache.get(id);
            // 判断是否存在软引用
            if (softDrawable == null) {
                return null;
            }
            // 取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空
            Drawable drable = softDrawable.get();
            return drable;
        }
}

android加载更多的图片的更多相关文章

  1. Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】

    Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)[系列1] Android在加载或者处理超大巨型图片 ...

  2. ajax点击加载更多数据图片(预加载)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  4. [Android] Android 支持下拉刷新、上拉加载更多 的 XRecyclerview

    XRecyclerView一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView.使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊 ...

  5. Android 开发 上拉加载更多功能实现

    实现思维 开始之前先废话几句,Android系统没有提供上拉加载的控件,只提供了下拉刷新的SwipeRefreshLayout控件.这个控件我们就不废话,无法实现上拉刷新的功能.现在我们说说上拉加载更 ...

  6. android 加载图片框架--Glide使用详解

    一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...

  7. Android学习笔记_51_转android 加载大图片防止内存溢出

    首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView, ...

  8. Android之Socket通信、List加载更多、Spinner下拉列表

    Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务 ...

  9. Android ListView加载更多

    先看效果: ListView的footer布局: <?xml version="1.0" encoding="utf-8"?> <Relati ...

随机推荐

  1. php获取当前日期-7天

    // 将目前的时间戳值放入一数组内$strdate = '2014-02-03';$desDate = strtotime($strdate);//var_dump($desDate); $times ...

  2. OpenGL ES 三种类型 uniform attribute varying

    1.uniform变量 uniform变量是外部application程序传递给(vertex和fragment)shader的变量.因此它是application通过函数glUniform**()函 ...

  3. Oracle EBS-SQL (PO-8):检查有供货比例无采购员.sql

    select distinct msr.sourcing_rule_name            名称 , msi.description                          说明 , ...

  4. 简洁的jsp

    在开发 是使用tomcat7版本(7的jslt表达式语法检查更加严格) 1.去除生产html的不必要的空行 <%@ page trimDirectiveWhitespaces="tru ...

  5. 基本的Logstash 例子

    基本的Logstash 例子: 为了测试你的Logstash 安装,运行最基本的Logstash 管道: cd logstash-2.3.0 bin/logstash -e 'input { stdi ...

  6. Android UI SurfaceView的使用-绘制单个图型或多个图形

    新建MyView类继承自SurfaceView: public class MyView extends SurfaceView implements SurfaceHolder.Callback { ...

  7. linux 命令之 uptime

    uptime 命令是用来查询linux系统负载的. 命令格式 uptime [OPTION] -V 显示版本号 不带參数的 uptime 直接输出系统负载. 何为系统负载呢? 系统平均负载被定义为在特 ...

  8. Android提高第十一篇之模拟信号示波器

    上次简单地介绍了AudioRecord和AudioTrack的使用,这次就结合SurfaceView实现一个Android版的手机模拟信号示波器(PS:以前也讲过J2ME版的手机示波器).最近物联网炒 ...

  9. bootstrap注意事项(五)表单

    1.基本实例 单独的表单控件会被自动赋予一些全局样式.所有设置了 .form-control类的 <input>.<textarea> 和 <select> 元素都 ...

  10. 不要伤害指针(5)--void和void指针详解

    原文转载地址:http://blog.csdn.net/sunchaoenter/article/details/6587426 增加自己的想法,作为笔记. 1.概述 许多初学者对C/C++语言中的v ...