国产69囗曝吞精在线视频,肥臀浪妇太爽了快点再快点,亚洲欧洲成人a∨在线观看,狠狠色丁香久久综合 ,国精一二二产品无人区免费应用,亚洲精品久久久久中文字幕,四虎一区二区成人免费影院网址 ,无码三级中文字幕在线观看

      Bitmap三級緩存 和二次采樣

      2019-7-22    seo達人

      如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點這里

      一.為什么Bitmap三級緩存?
      沒有緩存的弊端 :費流量, 加載速度慢
      加入緩存的優(yōu)點: 省流量,支持離線瀏覽
      二.原理

      從內(nèi)存獲取圖片, 如果存在, 則顯示; 如果不存在, 則從SD卡中獲取圖片
      從SD卡中獲取圖片, 如果文件中存在, 顯示, 并且添加到內(nèi)存中; 否則開啟網(wǎng)絡(luò)下載圖片
      從網(wǎng)絡(luò)下載圖片, 如果下載成功, 則添加到緩存中, 存入SD卡, 顯示圖片
      三.代碼
      (1)添加讀寫SD卡的權(quán)限和網(wǎng)絡(luò)權(quán)限



      // //Lrucache存儲工具類
      public class LruUtils {
      private LruCache<String,Bitmap> lruCache;
      private long max=Runtime.getRuntime().maxMemory();
      public LruUtils(){
      lruCache=new LruCache<String,Bitmap>((int)max/8){

              @Override
              protected int sizeOf(String key, Bitmap value) {
                  return value.getByteCount();
              }
          };
      }
      public Bitmap getBitmap(String key){
          return lruCache.get(key);
      }
      public void setBitmap(String key,Bitmap bitmap){
          lruCache.put(key,bitmap);
      }
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      }
      //SD卡工具類
      public class SDUtils {

      public static void setBitmap(String name, Bitmap bitmap) {

          if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
              File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
              File file1 = new File(file, name);

              try {

                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file1));
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }
          }
      }
      public static Bitmap getBitmap(String name){

          if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
              File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
              File file1=new File(file,name);

              return BitmapFactory.decodeFile(file1.getAbsolutePath());

          }
          return null;
      }

      }
      //網(wǎng)絡(luò)

      import android.app.AlertDialog;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.os.AsyncTask;

      import java.io.InputStream;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.util.concurrent.ExecutionException;

      public class NewUtils {
      public static Bitmap getBitmap(String url) throws ExecutionException, InterruptedException {
      return new MyTask().execute(url).get();
      }
      static class MyTask extends AsyncTask<String,Void,Bitmap>{
      @Override
      protected Bitmap doInBackground(String… strings) {
      String imageUrl = strings[0];
      HttpURLConnection conn = null;
      try {
      URL url = new URL(imageUrl);
      conn = (HttpURLConnection) url.openConnection();
      conn.setReadTimeout(5000);
      conn.setConnectTimeout(5000);
      conn.setRequestMethod(“GET”);
      if (conn.getResponseCode() == 200) {
      InputStream is = conn.getInputStream();
      Bitmap bitmap = BitmapFactory.decodeStream(is);
      return bitmap;
      }
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      if (conn != null) {
      conn.disconnect();
      }
      }
      return null;
      }
      }

      }
      //使用三個工具類完成Bitmap的三級緩存
      package com.example.administrator.myapplication;

      import android.graphics.Bitmap;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.ImageView;
      import android.widget.Toast;

      import java.util.concurrent.ExecutionException;

      public class MainActivity extends AppCompatActivity {
      private ImageView imageView;
      Button button;
      private LruUtils lruUtils= new LruUtils();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button=findViewById(R.id.button);
      imageView=findViewById(R.id.imageview);
      button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Bitmap bitmap=lruUtils.getBitmap(“czn”);
      if (bitmap!=null){
      imageView.setImageBitmap(bitmap);
      Toast.makeText(MainActivity.this, “圖片存內(nèi)存”, Toast.LENGTH_SHORT).show();
      }else{
      bitmap=SDUtils.getBitmap(“czn.jpg”);
      if (bitmap!=null){
      imageView.setImageBitmap(bitmap);
      Toast.makeText(MainActivity.this, “圖片存SD卡”, Toast.LENGTH_SHORT).show();
      lruUtils.setBitmap(“czn”,bitmap);
      }else{
      try {
      bitmap=NewUtils.getBitmap(“http://pic1.win4000.com/wallpaper/e/50d80458e1373.jpg”);
      if (bitmap!=null){
      imageView.setImageBitmap(bitmap);
      Toast.makeText(MainActivity.this, “圖片存網(wǎng)絡(luò)”, Toast.LENGTH_SHORT).show();
      SDUtils.setBitmap(“czn.jpg”,bitmap);
      lruUtils.setBitmap(“czn”,bitmap);
      }else{
      Toast.makeText(MainActivity.this, “沒有找到”, Toast.LENGTH_SHORT).show();
      }

                          } catch (ExecutionException e) {
                              e.printStackTrace();
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }

                      }
                  }
              }
          });
      }

      }
      Bitmap二次采樣


      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.os.AsyncTask;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.ImageView;

      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.HttpURLConnection;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.util.concurrent.ExecutionException;

      public class Main2Activity extends AppCompatActivity {
      Button bt;
      ImageView imageView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main2);
      bt=findViewById(R.id.bt);
      imageView=findViewById(R.id.mimage);
      bt.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      try {
      Bitmap bitmap = new MyTask().execute(“https://cdn.duitang.com/uploads/item/201211/24/20121124230042_Bfhim.jpeg”).get();
      imageView.setImageBitmap(bitmap);
      } catch (InterruptedException e) {
      e.printStackTrace();
      } catch (ExecutionException e) {
      e.printStackTrace();
      }

              }

          });
      }
      class MyTask extends AsyncTask<String,Object,Bitmap>{

          @Override
          protected Bitmap doInBackground(String... strings) {
              try {
                  URL url = new URL(strings[0]);
                  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                  if(urlConnection.getResponseCode()==200){
                      InputStream inputStream = urlConnection.getInputStream();
                      //將inputStream流存儲起來
                      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                      byte[] bytes = new byte[1024];
                      int len=0;
                      while((len=inputStream.read(bytes))!=-1){
                          byteArrayOutputStream.write(bytes,0,len);
                      }
                      //桶:網(wǎng)絡(luò)的圖片都放在數(shù)組里面了
                      byte[] data = byteArrayOutputStream.toByteArray();
                      //TODO 1:第一次采樣:只采邊框 計算壓縮比例
                      BitmapFactory.Options options = new BitmapFactory.Options();
                      options.inJustDecodeBounds=true;//設(shè)置只采邊框
                      BitmapFactory.decodeByteArray(data,0,data.length,options);//采樣
                      int outWidth = options.outWidth;//獲得原圖的寬
                      int outHeight = options.outHeight;//獲得原圖的高
                      //計算縮放比例
                      int size=1;
                      while(outWidth/size>100||outHeight/size>100){
                          size*=2;
                      }
                      //TODO 2:第二次采樣:按照比例才像素
                      options.inJustDecodeBounds=false;//設(shè)置只采邊框為fasle
                      options.inSampleSize=size;//設(shè)置縮放比例
                      Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length,options);//采樣
                      return  bitmap;
                  }

              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }
      }
      藍藍設(shè)計m.izc.net.cn )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計 、 cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標定制 、 用戶體驗 、交互設(shè)計、網(wǎng)站建設(shè) 平面設(shè)計服務(wù)

      日歷

      鏈接

      個人資料

      藍藍設(shè)計的小編 http://m.izc.net.cn

      存檔

      主站蜘蛛池模板: 狠狠色综合久久狠狠色综合| 国产在线色| 亚洲国产精品久久无人区| 久久tv中文字幕首页| 日本高清久久| 日本精品aⅴ一区二区三区| 综合 欧美 小说 另类 图| 日韩少妇高潮抽搐| 国产精品久久久免费观看| 37p粉嫩大胆色噜噜噜| 无码尹人久久相蕉无码| 产后漂亮奶水人妻无码| 国产乱人视频| 久热在线中文字幕色999舞| 羞羞影院午夜男女爽爽免费视频| 欧美精品入口蜜桃| 国产精品自在线拍国产| 亚洲熟女精品中文字幕| 日韩少妇高潮抽搐| www.69视频| 日本va在线视频播放| 欧美在线| 黄色伊人网| 五十路熟女一区二区三区| 亚洲大尺度无码无码专线| 麻豆黄色片| 亚洲视频99| 18禁真人抽搐一进一出免费| 亚洲中国最大av网站| 国产精品99在线观看| 四库影院永久国产精品| 中国熟妇毛多多裸交视频| 影音先锋国产精品| 欧美性生活xxx| 一本精品中文字幕在线| 欧美精品亚洲日韩aⅴ| 思思久久精品| 自偷自拍亚洲综合精品| 亚洲综合区图片小说区| 天天爽天天干| 亚洲激情视频网站|