android利用lrucache存储图片的demo

代码

xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图片 缓存 demo"/>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="远程图片地址"/>
        <EditText android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/txt_img1"/>
    LinearLayout>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始抓取" android:id="@+id/btn_fetch_img1"/>
    <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img_res1"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_tips_1"/>
    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/panel_imgList">


        LinearLayout>
    ScrollView>

LinearLayout>

java

package com.example.apppractise.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.widget.*;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;

/**
 * Created by Administrator on 2015/7/3.
 */
public class ImageCacheDemo extends Activity {

    private LruCache imgcache;
    RequestQueue queue;
    private static final String TAG="image cache demo";
    private EditText txt_img1;
    private Button btn_fetch_img1;
    private ImageView img_res1;
    private LinearLayout layout_imgList;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_cache_demo);
        queue= Volley.newRequestQueue(this);
        caculateMemory();
        initImgCache();
        initUI();
        initEvents();
    }

    private void initImgCache(){
        int _size_space=(int)(Runtime.getRuntime().maxMemory()/1024);
        _size_space=_size_space/8;
        imgcache=new LruCache(_size_space){
            @Override
        protected int sizeOf(String key,Bitmap bitmap){

                return bitmap.getByteCount()/1024;
            }
        };

    }
    private void caculateMemory(){
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        Log.d(TAG,"当前最大容量:"+maxMemory+"mb");
        Long free_memory=(Long)(Runtime.getRuntime().freeMemory()/1024);
        Log.d((TAG),"剩余容量:"+free_memory+"mb");

    }
    private void initUI(){

        txt_img1=(EditText)findViewById(R.id.txt_img1);
        btn_fetch_img1=(Button)findViewById(R.id.btn_fetch_img1);
        img_res1=(ImageView)findViewById(R.id.img_res1);
        layout_imgList=(LinearLayout)findViewById(R.id.panel_imgList);
    }
    private void initEvents(){
        btn_fetch_img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String path=txt_img1.getText().toString();
                if(path.length()<=0){
                    Toast.makeText(ImageCacheDemo.this, "请输入需要显示的图片的路径。", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.e("log","开始抓图片:"+path);
                if(imgcache.get(path)!=null){

                    Bitmap _bmp=imgcache.get(path);
                    appendImage(_bmp);
                    Log.d(TAG,"直接从内存里面拿出图片缓存来用");
                    caculateMemory();
                    return;
                }
                ImageRequest imageRequest=new ImageRequest(path, new Response.Listener() {
                    @Override
                    public void onResponse(Bitmap response) {
                        imgcache.put(path,response);
                        //img_res1.setImageBitmap(response);
                        appendImage(response);
                        Log.e("log","抓取成功");
                        caculateMemory();
                    }
                }, 400, 400, Bitmap.Config.ARGB_4444, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        //img_res1.setImageResource(R.drawable.ui_imgselect_albums_bg);
                        Log.e("log","抓取失败");
                    }
                });
                queue.add(imageRequest);
            }
        });
    }

    private void appendImage(Bitmap bitmap){
        ImageView imageView=new ImageView(this);
        imageView.setImageBitmap(bitmap);
        layout_imgList.addView(imageView);
    }
}

你可能感兴趣的:(安卓开发)