Picasso圖片顯示
Square公司開源的一個Android圖形緩存庫
Picasso實現了圖片的異步加載,並解決了Android中加載圖片時常見的一些問題,它有以下特點:
1.在Adapter中取消了不在視圖範圍內的ImageView的資源加載,因爲可能會產生圖片錯位;
2.使用複雜的圖片轉換技術降低內存的使用
3.自帶內存和硬盤的二級緩存機制
引用庫
Android Studio 3
implementation 'com.squareup.picasso:picasso:2.5.2'
Android Studio 2
compile 'com.squareup.picasso:picasso:2.5.2'
權限獲取
<!--網絡權限--> <uses-permission android:name="android.permission.INTERNET" /> <!--文件讀寫權限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
函數調用
private ImageView im; im=findViewById(R.id.imageView2); //picben("https://pic.cnblogs.com/face/1485202/20180908195218.png",im); picben("/dongxiaodong/ww.jpg",im);
封裝函數
1 //參數(地址,顯示的控件) 2 public void picben(String panx, ImageView imgviewx){ 3 File sd= Environment.getExternalStorageDirectory();//得到SD卡的路徑 4 Picasso.with(MainActivity.this) 5 .load(new File(sd,panx))//文件路徑,必須打開文件讀寫權限 6 //.load(urlx)//URL,必須添加網絡權限 7 //.memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)//待測試,圖片加載跳過內存緩存中查找,圖片不緩存到內存緩存中 8 .memoryPolicy(MemoryPolicy.NO_CACHE)//無緩存,加下面一句可實現 9 .networkPolicy(NetworkPolicy.NO_CACHE) 10 .placeholder(R.mipmap.mainon)//正在加載圖片 11 .error(R.mipmap.mainoff)//嘗試三次失敗後 加載錯誤顯示圖片 12 .fit()//填充控件與resize不可共用 13 //.noFade()//無淡入淡出效果 14 //.resize(100,100)//圖片要調整後的大小,必須配合下面兩個之一 15 //.centerCrop()//配合resize使用,僅顯示被框佔位 16 //.centerInside()//配合resize使用,裁剪後填充滿控件 17 //.rotate(30)//旋轉30度 18 // .transform(new tCircle())可變爲圓形狀,但有黑底 19 .config(Bitmap.Config.RGB_565)//比ARGB_8888 質量稍差,但看基本不出 20 .into(imgviewx);//控件位置 21 }
OKHttp網絡訪問
okhttp是Square公司開源的一個非常便捷的輕量級第三方網絡訪問框架。它支持同步請求和異步請求。
HTTP是現代應用網絡的方式。這就是我們交換數據和媒體的方式。有效地執行HTTP可以加快您的負載並節省帶寬。
OkHttp是一個默認有效的HTTP客戶端:
1.HTTP / 2支持允許對同一主機的所有請求共享套接字。
2.連接池減少了請求延遲(如果HTTP / 2不可用)。
3.透明GZIP縮小了下載大小。
4.響應緩存可以完全避免網絡重複請求。
當網絡很麻煩時,OkHttp堅持不懈:它將從常見的連接問題中無聲地恢復。如果您的服務有多個IP地址,如果第一次連接失敗,OkHttp將嘗試備用地址。這對於IPv4 + IPv6和冗餘數據中心中託管的服務是必需的。OkHttp支持現代TLS功能(TLS 1.3,ALPN,證書固定)。它可以配置爲回退以實現廣泛的連接。
使用OkHttp很容易。它的請求/響應API採用流暢的構建器和不變性設計。它支持同步阻塞調用和帶回調的異步調用。
引用庫
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
權限
<!--網絡權限--> <uses-permission android:name="android.permission.INTERNET" /> <!--文件讀寫權限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Post請求調用
HashMap<String,String> mapx=new HashMap<String, String>(); mapx.put("yh","dongdong"); mapx.put("mm","xxxxxxx"); OKhttp.getOhttp().okpost("post.php", mapx, new OKhttp.Jkstr() { @Override public void jkstr(String str) { Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); //System.out.println(str); } });
Get請求調用
OKhttp.getOhttp().okget("post.php?dong=dongxiaodong&dong2=dongdongx", new OKhttp.Jkstr() { @Override public void jkstr(String str) { Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); //System.out.println(str); } });
網絡訪問類封裝
1 package com.example.testdelete; 2 import android.os.Environment; 3 import android.os.Handler; 4 import java.io.File; 5 import java.io.IOException; 6 import java.util.Map; 7 import okhttp3.Call; 8 import okhttp3.Callback; 9 import okhttp3.FormBody; 10 import okhttp3.MediaType; 11 import okhttp3.MultipartBody; 12 import okhttp3.OkHttpClient; 13 import okhttp3.Request; 14 import okhttp3.RequestBody; 15 import okhttp3.Response; 16 /** 17 * Created by 東東 on 2018/3/29. 18 */ 19 public class OKhttp { 20 private OKhttp(){}; 21 private static final OKhttp ohttp=new OKhttp(); 22 public static OKhttp getOhttp(){ 23 return ohttp; 24 } 25 private OkHttpClient client=new OkHttpClient(); 26 private Handler handlerx=new Handler(); 27 public static final String UR="http://193.110.8.6/testdelete/"; 28 File sd= Environment.getExternalStorageDirectory();//得到SD卡的路徑 29 //文件上傳 30 //參數(上傳的url,文件地址,文件名,回調函數) 31 public void okpostfile(String url,String path,String filenaem,final Jkstr callbackfile) {//get和post數據同時上傳 32 File file = new File(sd,path); 33 RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file); 34 RequestBody requestBody = new MultipartBody.Builder() 35 .setType(MultipartBody.FORM) 36 .addFormDataPart("mfile",filenaem, fileBody)//Post參數 37 .build(); 38 Request request = new Request.Builder() 39 .url(UR+url) 40 .post(requestBody) 41 .build(); 42 client.newCall(request).enqueue(new Callback() { 43 @Override 44 public void onFailure(Call call, IOException e) {//請求失敗 45 ohttpstr("0",callbackfile); 46 } 47 @Override 48 public void onResponse(Call call, Response response) throws IOException {//請求成功 49 ohttpstr(response.body().string(),callbackfile); 50 } 51 }); 52 } 53 //Get請求 54 //參數(url,回調函數) 55 public void okget(String url,final Jkstr callbakestr){ 56 final Request res=new Request.Builder().url(UR+url).build(); 57 client.newCall(res).enqueue(new Callback() { 58 @Override 59 public void onFailure(Call call, IOException e) {//請求失敗 60 ohttpstr("0",callbakestr); 61 e.printStackTrace(); 62 } 63 @Override 64 public void onResponse(Call call, Response response) throws IOException {//請求成功 65 if(response.isSuccessful()){ 66 ohttpstr(response.body().string(),callbakestr); 67 } 68 } 69 }); 70 } 71 //Post請求 72 //參數(url,Map集合,回調函數) 73 public void okpost(String ur, Map<String,String> mapx,final Jkstr callbackform){ 74 FormBody.Builder formx=new FormBody.Builder(); 75 if(mapx!=null&&!mapx.isEmpty()){ 76 for(Map.Entry<String,String>xx:mapx.entrySet()){ 77 formx.add(xx.getKey(),xx.getValue()); 78 } 79 RequestBody resbody=formx.build(); 80 Request req=new Request.Builder().url(UR+ur).post(resbody).build(); 81 client.newCall(req).enqueue(new Callback() {//請求失敗 82 @Override 83 public void onFailure(Call call, IOException e) { 84 ohttpstr("0",callbackform); 85 } 86 87 @Override 88 public void onResponse(Call call, Response response) throws IOException {//請求成功 89 if(response.isSuccessful()){ 90 ohttpstr(response.body().string(),callbackform); 91 } 92 } 93 }); 94 } 95 } 96 //請求返回爲byte數組 97 private void ohttpbyt(final byte[] bytx,final Jkbyt callbackx){ 98 handlerx.post(new Runnable() { 99 @Override 100 public void run() { 101 if(callbackx!=null){ 102 try{ 103 callbackx.jkbyt(bytx); 104 }catch (Exception e){ 105 e.printStackTrace(); 106 } 107 } 108 } 109 }); 110 } 111 //處理返回爲string類型數據 112 private void ohttpstr(final String strx, final Jkstr callback){ 113 handlerx.post(new Runnable() { 114 @Override 115 public void run() { 116 if(callback!=null){ 117 try{ 118 callback.jkstr(strx.trim()); 119 }catch (Exception e){ 120 e.printStackTrace(); 121 } 122 } 123 } 124 }); 125 } 126 public interface Jkstr{ void jkstr(String str); } 127 public interface Jkbyt{void jkbyt(byte [] bytexx);} 128 }
參考:育知同創