ImageLoader的用法

导入imageloader包:


第一步:创建applacation类

/**
 * @author WJL
 * 
 */
public class MyApplication extends Application {
	private int maxMemory;

	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("MyApplication");
		
		maxMemory = ((int) Runtime.getRuntime().maxMemory()) / 1024 / 1024;
                  System.out.println("最大内存:"+maxMemory);
                  Toast.makeText(this, "   "+maxMemory, 0).show();
                  
         //初始ImageLoader
		initImageLoader();          
	}

	/**
	 *           
         //初始ImageLoader
	 */
	@SuppressWarnings("deprecation")
	private void initImageLoader() {
		
		
		//得到ImageLoader实例
		ImageLoader imageLoader=ImageLoader.getInstance();
		
		//得到ImageLoader配置的建筑者对象
		ImageLoaderConfiguration.Builder builder=new Builder(this);
		
		//设置磁盘(sd卡)缓存的大小限制
//		builder.diskCacheSize(maxCacheSize);
		try {
			//设置磁盘(sd卡)缓存策略,参数一:缓存文件的路径,参数二:文件名的命名方法,有MD5,HashCode,参数三:缓存的最大内存
			builder.discCache(new LruDiskCache(new File("/storage/sdcard0/cache/"), new Md5FileNameGenerator(), 2*1024*1204));
			//设置磁盘(sd卡)缓存策略,限制缓存时间;参数一:缓存文件的路径,参数二:缓存文件的存活时间,系统当前时间-文件的最新修改时间 > maxAge
//			builder.discCache(new LimitedAgeDiskCache(cacheDir, maxAge) );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		//设置内存缓存的大小限制,默认是最大内存的1/8
		builder.memoryCacheSize(maxMemory/8);
		
		//设置内存缓存策略                根据先进先出的原则上删除多余对象  LinkedList  参数一:设置的缓存内存大小
//		builder.memoryCache(new FIFOLimitedMemoryCache(sizeLimit));
		
		//设置内存缓存策略          先删除占内存最大的图片      HashMap    参数一:设置的缓存内存大小
//  	builder.memoryCache(new LargestLimitedMemoryCache(sizeLimit));
		
         //	   设置内存缓存策略    Lru算法   LinkedHashMap  最大的缓存大小
//		builder.memoryCache(new LruMemoryCache(maxSize))
		
		//设置内存缓存策略        最少被用到的对象会被删除      HashMap    参数一:设置的缓存内存大小
//		builder.memoryCache(new UsingFreqLimitedMemoryCache(sizeLimit))
		
		//设置内存缓存策略        最早被添加的对象会被删除     HashMap  参数一:MemoryCache对象   参数二:缓存失效的最大时间
//		builder.memoryCache(new LimitedAgeMemoryCache(cache, maxAge));
		
		
		//初始化imageLoader配置
		imageLoader.init(builder.build());
		
		
		
	<span style="color:#ff0000;">/*	
		使用默认的缓存策略
		ImageLoader imageLoader2=ImageLoader.getInstance();
		ImageLoaderConfiguration.Builder configuration=new ImageLoaderConfiguration.Builder(this);
		imageLoader2.init(configuration.build());
		*/</span>
	}

}

第二步:在配置文件中配置:


 <application
  
	android:name="com.example.day_13_iamgeloader.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        
        <activity
           <span style="color:#ff0000;"> android:name="com.example.day_13_iamgeloader.MainActivity"</span>
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        
    </application>

第三步:使用


public class MyAdapter extends BaseAdapter {
	Context context; String[] paths;
	private DisplayImageOptions.Builder options;
	/**
	 * @param mainActivity
	 * @param paths
	 */
	public MyAdapter(Context context, String[] paths) {
		// TODO Auto-generated constructor stub
		this.context=context;
		this.paths=paths;
		
		<span style="color:#ff0000;">options = new Builder();
		//是否缓存到SD卡
		options.cacheOnDisk(true);
		//是否缓存到内存中
		options.cacheInMemory(true);
		//加载图片中显示的图片
		options.showImageOnLoading(R.drawable.ic_launcher);
//		options.showImageOnLoading(getResources().getDrawable(R.drawable.ic_launcher));
		//图片加载失败时显示的默认图片
//		options.showImageOnFail(R.drawable.ic_launcher);
		//图片路径为空时,显示的图片
//		options.showImageForEmptyUri(R.drawable.ic_launcher)
		options.bitmapConfig(Config.RGB_565);//图片的解码类型
//		ImageSize imageSize=new ImageSize(width, height);</span>
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return paths.length;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
//	http://pic3.bbzhi.com/yingshibizhi/bingheshiji3/movie_oumei_242656_15.jpg
	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		
		View view = View.inflate(context,R.layout.item, null);
		
			
        ImageView iv=(ImageView)view. findViewById(R.id.iv);
        
    	ImageLoader imageLoader=ImageLoader.getInstance();
//		ImageLoader加载图片,参数一:图片网络地址.参数二:装图片的控件
//		imageLoader.displayImage(uri, imageView);
	
		imageLoader.displayImage(paths[position],	 iv, options.build());
        
      
		return view;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值