首页 > 编程知识 正文

鸿蒙系统界面图片,鸿蒙天经

时间:2023-05-06 01:19:39 阅读:10459 作者:3751

将web图像加载到目录中获取web图像将图像输入流转换为PixelMap子线程,进行网络请求网络请求枚举类

加载web图像

在实际项目中,并不总是显示资源文件中的图像,而是在浏览信息或在微博时基本上从网络加载。

所以,加载网络图片,或者掌握网络获取的知识,才是真正发展APP的基础。 当然,网络任务不能在主线程上执行。 其中也包括有关线程的知识。

要获取网络图像,必须先获取网络图像。

一般来说,我们是请求从网站获取图像的输入流InputStream。 代码示例如下所示。 (https实用程序) :

publicclasshttpsutils { privatestaticinputstreaminputstream; 私有状态连接; publicstaticinputstreamgetinputstream (字符串urlstr,stringmethodtype (netmanagernetmanager=net manager.getinstance ) ) netManager.hasDefaultNet () { return null; } nethandlenethandle=net manager.getdefaultnet (; try{URLURL=newURL(Urlstr; urlconnectionurlconnection=nethandle.open connection (URL,java.net.Proxy.NO_PROXY ); urlconnectioninstanceofhttpsurlconnection (if ) connection=(httpsurlconnection ) URLconnection; } connection.setrequestmethod (方法类型; connection.connect (; inputstream=connection.get inputstream (; }catch(ioexceptione ) { e.printStackTrace ); }返回输入流; }公共静态语音关闭流(() try ) if ) Inputstream==null ) ) { return; } inputStream.close (; if (连接==null ) { return; } connection.disconnect (; }catch(ioexceptione ) { e.printStackTrace ); }}如上代码所示,获取图像或获取网络信息主要分为四个步骤。

调用netmanager.getinstance(context )获取网络管理的实例对象。 调用NetManager.getDefaultNet ()获取默认数据网络。 调用NetHandle.openConnection ()打开URL。 通过URL链接实例访问网站或图像网站。 因为这里不仅是网络的要求,还涉及网络的管理。 因此,除了网络权限之外,还需要设置两个网络权限config.json。

' module ' : { ' req permissions ' : [ { ' name ' : ' ohos.permission.internet ' },{ ' name ' 3360 ' ohos.} }当然,您没有积极切换网络的权限,所以可以不使用set_network_info权限,但get_network

将图像输入流转换为PixelMap。 因为Image组件只能包含资源文件和PixelMap格式的图像。 所以,需要将获取的网络数据转换为PixelMap。 代码如下。

publicclassimageutils { publicstaticpixelmapcreatepixelmap (字符串图像) in

putStream inputStream = HttpsUtils.getInputStream(imageUrl, RequestMethod.GET.name()); ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); sourceOptions.formatHint = "image/jpeg"; ImageSource imageSource = ImageSource.create(inputStream,sourceOptions); PixelMap pixelMap = imageSource.createPixelmap(null); HttpsUtils.closeStream(); return pixelMap; }}

如上面代码所示,将InputStream转换为PixelMap分为以下4步骤:

首先创建SourceOptions,指定图片的格式然后通过ImageSource.create()创建ImageSource最后,通过imageSource.createPixelmap创建pixelMap 子线程进行网络请求

到这里,我们就完成了图片的获取,并且可以将获取的网络图片设置到Image组件中。

但是有GUI开发经验的程序员都应该知道,耗时任务(网络请求)是不能在主线程运行的,因为这样会造成卡顿。

所以,我们需要使用鸿蒙提供给我们的线程进行处理。示例如下:

public class MainAbilitySlice extends AbilitySlice { HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG"); private Button button; private Image image; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); this.button=(Button)findComponentById(ResourceTable.Id_ability_main_button); this.image = (Image) findComponentById(ResourceTable.Id_ability_main_image); this.button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { TaskDispatcher refreshUITask = createParallelTaskDispatcher("", TaskPriority.DEFAULT); refreshUITask.syncDispatch(()->{ PixelMap pixelMap = ImageUtils.createPixelMap("https://images-1300376177.cos.ap-shanghai.myqcloud.com/homepage1.jpg"); getContext().getUITaskDispatcher().asyncDispatch(new Runnable() { @Override public void run() { //Image组件填充位图数据,ui界面更新 image.setPixelMap(pixelMap); pixelMap.release(); } }); }); } }); }}

ParallelTaskDispatcher:并发任务分发器,由Ability执行createParallelTaskDispatcher()创建并返回。

UITaskDispatcher:绑定到应用主线程的专有任务分发器, 由Ability执行getUITaskDispatcher()创建并返回。

网络请求枚举类

当然,上面的RequestMethod.GET.name()是一个枚举类,用于定义网络请求的各种方式。代码如下:

public enum RequestMethod { GET("GET"), POST("POST"), HEAD("HEAD"), OPTIONS("OPTIONS"), PUT("PUT"), DELETE("DELETE"), TRACE("TRACE"); private String method; RequestMethod(String method) { this.method = method; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; }}}

运行之后,效果如下:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。