android利用lrucache存储图片的demo
2015-07-03 10:19
441 查看
代码
xml<?xml version="1.0" encoding="utf-8"?> <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<String,Bitmap> 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<String, Bitmap>(_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<Bitmap>() { @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); } }
相关文章推荐
- Android性能调优
- 性能优化之Java(Android)代码优化
- Android setTextColor无效_安卓setTextColor()的参数设置方式
- 制作自己的android升级包(update.zip)
- android启动外部应用程序
- Android入门(2)——点击事件的三种方式监听方式
- Android连续两次后退键退出程序
- Android组件:Fragment切换后保存状态
- Android Studio 导入依赖包 出现问题的解决方法
- android当中显式intent和隐式intent的区别
- 如何预置Android 手机 APK
- Xamarin.Android开发实践(五)
- android的Jni层调用.so库(小记)
- Android xml资源文件中@、@android:type、@*、?、@+含义和区别
- Android属性动画-Property Animation(四) 组合动画
- android处理图片方法
- 百度语音识别(Baidu Voice) Android studio版本
- Android自定义DataTimePicker(日期选择器)
- ANdroid网易客户端
- 在mac上搭建Android开发环境