如何将一张图片转化为PixelMapElement

Android实现:

Drawable mCompleteIcon = ContextCompat.getDrawable(getContext(), R.drawable.complted);

mCompleteIcon.setBounds(rect);
mCompleteIcon.draw(canvas);

 

说明:R.drawable.complted为一张图片资源

请问鸿蒙如何实现该功能

可绘制图片资源
2021-04-12 15:18:13
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
红叶亦知秋
1

使用鸿蒙获取并设置网络图片

String   urlImage = "https://www.harmonyos.com/resource/image/community/20201009-164134eSpace.jpg";
 

HttpURLConnection   connection = null;
 

try {
 

         URL url = new URL(urlImage);
 

         URLConnection urlConnection =   url.openConnection();
 

         if (urlConnection instanceof   HttpURLConnection) {
 

                   connection =   (HttpURLConnection) urlConnection;
 

         }
 

         if (connection != null) {
 

                   connection.connect();
 

                   // 之后可进行url的其他操作
 

                   // 得到服务器返回过来的流对象
 

                   InputStream inputStream =   urlConnection.getInputStream();
 

                   ImageSource imageSource = ImageSource.create(inputStream,   new ImageSource.SourceOptions());
 

                   ImageSource.DecodingOptions   decodingOptions = new ImageSource.DecodingOptions();
 

                   decodingOptions.desiredPixelFormat   = PixelFormat.ARGB_8888;
 

                   // 普通解码叠加旋转、缩放、裁剪
 

                   PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
 

                   // 普通解码
 

                   getUITaskDispatcher().syncDispatch(()   -> {
 

                            Image image = new   Image(HttpImageSlice.this);
 

                            DirectionalLayout.LayoutConfig   config = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT,   DirectionalLayout.LayoutConfig.MATCH_CONTENT);
 

                       config.setMargins(10, 10,   10, 10);
 

                            image.setLayoutConfig(config);
 

                            image.setPixelMap(pixelMap);
 

                            myLayout.addComponent(image);
 

                            pixelMap.release();
 

                   });
 

         }
 

}   catch (Exception e) {
 

         e.printStackTrace();
 

}
 

 

分享
微博
QQ
微信
回复2
2021-04-12 15:37:24
相关问题
指定页面区域转化为图片
892浏览 • 1回复 待解决
如何保存一张PNG图片到相册中
703浏览 • 1回复 待解决
ets中如何将图片转为byte[]?
1975浏览 • 1回复 待解决
如何将图片PixelMap压缩到指定大小
652浏览 • 1回复 待解决
如何将像素点保存到图片文件
1218浏览 • 1回复 待解决
如何将图片文本视频分享给其他应用
433浏览 • 1回复 待解决
有谁知道如何将图片保存到相册
458浏览 • 1回复 待解决
如何将点击事件透传到下
846浏览 • 1回复 待解决
如何将ArrayBuffer转成string
886浏览 • 1回复 待解决
如何将多工程的hap打包成个app
332浏览 • 1回复 待解决