安卓图片显示与网络访问

安卓图片显示与网络访问,第1张

概述Picasso图片显示 Square公司开源的一个Android图形缓存库Picasso实现了图片的异步加载,并解决了Android中加载图片时常见的一些问题,它有以下特点: 1.在Adapter中取 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" />文件读写权限="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     jkstr(String str) {        Toast.makeText(MainActivity.,str,Toast.LENGTH_SHORT).show();        System.out.println(str);    }});

Get请求调用

OKhttp.getohttp().okget("post.PHP?dong=dongxiaodong&dong2=dongdongx",1)">    }});

网络访问类封装

  1 package com.example.testdelete;  2 import androID.os.Environment;  3  androID.os.Handler;  4  java.io.file;  5  java.io.IOException;  6  java.util.Map;  7  okhttp3.Call;  8  okhttp3.Callback;  9  okhttp3.FormBody; 10  okhttp3.MediaType; 11  okhttp3.Multipartbody; 12  okhttp3.OkhttpClIEnt; 13  okhttp3.Request; 14  okhttp3.Requestbody; 15  okhttp3.Response; 16 /** 17  * Created by 东东 on 2018/3/29. 18  */ 19 class OKhttp { 20      OKhttp(){}; 21     private static final OKhttp ohttp= OKhttp(); 22     static OKhttp getohttp(){ 23         return ohttp; 24     } 25     private OkhttpClIEnt clIEnt= OkhttpClIEnt(); 26     private Handler handlerx= Handler(); 27     final String UR="http://193.110.8.6/testdelete/"; 28     file sd= Environment.getExternalStorageDirectory();得到SD卡的路径 29     文件上传 30     参数(上传的url,文件地址,文件名,回调函数) 31     voID okpostfile(String url,String path,String filenaem,1)">final Jkstr callbackfile) {get和post数据同时上传 32         file file =  file(sd,path); 33         Requestbody fileBody = Requestbody.create(MediaType.parse("application/octet-stream"),file); 34         Requestbody requestbody =  Multipartbody.Builder() 35                 .setType(Multipartbody.FORM) 36                 .addFormDataPart("mfile",filenaem,fileBody)Post参数 37                 .build(); 38         Request request =  Request.Builder() 39                 .url(UR+url) 40                 .post(requestbody) 41  42         clIEnt.newCall(request).enqueue( Callback() { 43             @OverrIDe 44             voID onFailure(Call call,IOException e) {请求失败 45                 ohttpstr("0" 46             } 47  48             voID onResponse(Call call,Response response) throws IOException {请求成功 49                 ohttpstr(response.body().string(),1)"> 50  51         }); 52  53     Get请求 54     参数(url,回调函数) 55     voID okget(String url,1)">final Jkstr callbakestr){ 56         final Request res=new Request.Builder().url(UR+url).build(); 57         clIEnt.newCall(res).enqueue( 58  59              60                 ohttpstr("0" 61                 e.printstacktrace(); 62  63  64              65                if(response.isSuccessful()){ 66                    ohttpstr(response.body().string(),1)"> 67                } 68  69  70  71     Post请求 72      73     voID okpost(String ur,Map<String,String> mapx,1)"> Jkstr callbackform){ 74         FormBody.Builder formx= FormBody.Builder(); 75         if(mapx!=null&&!mapx.isEmpty()){ 76             for(Map.Entry<String,1)">xx:mapx.entrySet()){ 77                 formx.add(xx.getKey(),xx.getValue()); 78  79             Requestbody resbody=formx.build(); 80             Request req=ur).post(resbody).build(); 81             clIEnt.newCall(req).enqueue(new Callback() { 82                 @OverrIDe 83                  onFailure(Call call,IOException e) { 84                     ohttpstr("0" 85                 } 86  87  88                  89                    90                       ohttpstr(response.body().string(),1)"> 91                   } 92  93             }); 94         } 95  96     请求返回为byte数组 97     voID ohttpbyt(final byte[] bytx,1)"> Jkbyt callbackx){ 98         handlerx.post( Runnable() { 99 100              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     voID ohttpstr(final String strx,1)"> Jkstr callback){113         handlerx.post(114 115             116                 if(callback!=117                     118                         callback.jkstr(strx.trim());119                     }120                         e.printstacktrace();121 122 123 124 125 126     interface Jkstr{  jkstr(String str); }127     interface Jkbyt{voID jkbyt(byte [] bytexx);}128 }

参考:育知同创

总结

以上是内存溢出为你收集整理的安卓图片显示与网络访问全部内容,希望文章能够帮你解决安卓图片显示与网络访问所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121207.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存