三级缓存原理

//定义一个加载图片的类

public class MyFresco {
    private static MyMemocyCache myMemocyCache;
    private static MyDiskCache myDiskCache;
    private static MyFresco myFresco;
    private static Context mContext;
    public static MyFresco init(Context context){
        myMemocyCache=new MyMemocyCache();
        myDiskCache=new MyDiskCache();
        myFresco=new MyFresco();
        mContext=context;
        return myFresco;
    }
    //加载图片的方法
    public Bitmap loadPic(String url){
        //检查内存里有没有
        Bitmap picFromMemory = myMemocyCache.getPicFromMemory(url);
        if (picFromMemory==null){
            //去磁盘里获取
            Bitmap picFromDisk = myDiskCache.getPicFromDisk(url);
            if (picFromDisk==null){
            //去网络获取图片
                Bitmap picFromNet = getPicFromNet(url);
                if (picFromNet==null){
                    //网络图片没有就返回一个默认图片
                 Bitmap bitmap= BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_launcher);
               return bitmap;
                }else {
                    return picFromNet;
                }
            }else {
                return picFromDisk;
            }
        }else {
            return picFromMemory;
        }

    }
    //从网络中获取图片
    public Bitmap getPicFromNet(String url){
        //使用HttpUrlConnction
        URL u = null;
        try {
            u = new URL(url);
           HttpURLConnection connection = (HttpURLConnection) u.openConnection();
           connection.setConnectTimeout(5000);
           if (connection.getResponseCode()==200){
               InputStream inputStream = connection.getInputStream();//获取输入流相当于Bitmap
               Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
               return bitmap;
           }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
//内存缓存类

public class MyMemocyCache {
    //内存大小的1/8
   private long size=Environment.getDataDirectory().getUsableSpace();
  private LruCache<String,Bitmap> lruCache=new LruCache<>((int) (size/8));
    //取出图片
public Bitmap getPicFromMemory(String url){
    Bitmap bitmap = lruCache.get(url);
    return bitmap;
}
    //向内存中保存图片
    public void setPicToMemocy(String url,Bitmap bitmap){
       lruCache.put(url,bitmap);
    }
}

//sdcard缓存类

public class MyDiskCache {
    private String path= Environment.getExternalStorageDirectory().getAbsolutePath();
    //保存图片
    public void setPicToDisk(String url){
        File file = new File(path, url);
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(file));//将图片压缩到sdcard
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //取出图片
    public Bitmap getPicFromDisk(String url){
        File file = new File(path, url);
        if (file.exists()){
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            return bitmap;
        }

        return null;
    }
}

//主activity调用

public class MainActivity extends AppCompatActivity {
private String pic="http://www.taopic.com/uploads/allimg/140320/235013-14032020515270.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button start = findViewById(R.id.start);
        final ImageView image = findViewById(R.id.image);
        final MyFresco init = MyFresco.init(this);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        final Bitmap bitmap = init.getPicFromNet(pic);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                image.setImageBitmap(bitmap);
                            }
                        });

                    }
                }.start();
            }
        });
    }

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.huancun.MainActivity">

   <Button
       android:text="加载"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/start"/>
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/image"/>
</LinearLayout>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值