如何将一张图片转化为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();
 

}
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.

 

分享
微博
QQ
微信
回复2
2021-04-12 15:37:24


相关问题
HarmonyOS 如何将任意UI组件转化为图片
797浏览 • 1回复 待解决
如何吸取一张图片的色值?
1243浏览 • 1回复 待解决
HarmonyOS 多张图片拼接为一张
1283浏览 • 1回复 待解决
如何保存一张PNG图片到相册中
2577浏览 • 1回复 待解决
指定页面区域转化为图片
2495浏览 • 1回复 待解决
HarmonyOS 多张string图片合并成一张
807浏览 • 1回复 待解决
HarmonyOS 获取手机最新的一张图片
717浏览 • 1回复 待解决
HarmonyOS 多张画布横向合成一张图片
709浏览 • 1回复 待解决
HarmonyOS 如何使一张图片拉伸但不变形
1354浏览 • 1回复 待解决