鸿蒙Java开发模式11:鸿蒙图片裁剪功能的实现

开发
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[383592]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

1. 鸿蒙版图片裁剪功能,效果如下:

首页

图片裁剪区域:

裁剪结果:

2.Java代码实现如下:

  1. package com.example.javahm9.slice; 
  2.  
  3. import com.example.javahm9.ResourceTable; 
  4. import com.example.javahm9.util.CropImage; 
  5. import ohos.aafwk.ability.AbilitySlice; 
  6. import ohos.aafwk.content.Intent; 
  7. import ohos.agp.colors.RgbColor; 
  8. import ohos.agp.components.Button; 
  9. import ohos.agp.components.Component; 
  10. import ohos.agp.components.Text; 
  11. import ohos.agp.components.element.ShapeElement; 
  12.  
  13. public class MainAbilitySlice extends AbilitySlice { 
  14.  
  15.     //定义一个图片 
  16.     Component image; 
  17.     //定义一个文本 
  18.     Text text; 
  19.  
  20.     @Override 
  21.     public void onStart(Intent intent) { 
  22.         super.onStart(intent); 
  23.         super.setUIContent(ResourceTable.Layout_ability_main); 
  24.  
  25.         //获取图片对象对应的component 
  26.         image = findComponentById(ResourceTable.Id_result_image); 
  27.  
  28.         /* 
  29.          * 如果接收的cropFlag为true 
  30.          * 处理剪裁后的图片 
  31.          * 否则跳过 
  32.          */ 
  33.         if(intent.getBooleanParam("cropFlag",false)){ 
  34.             handleCrop(intent); 
  35.         } 
  36.         /* 自定义--获取文本对象对应的component 
  37.          * 根据intent里面的cropStatus来显示不同的文本 
  38.          * 0表示未接收到数据 
  39.          * 1表示剪裁取消 
  40.          * 2表示剪裁成功 有数据 
  41.          */ 
  42.         text = (Text) findComponentById(ResourceTable.Id_text); 
  43.         if(intent.getIntParam("cropStatus",0) == 0){ 
  44.             text.setText("欢迎使用"); 
  45.         }else if(intent.getIntParam("cropStatus",0) == 1){ 
  46.             text.setText("剪裁取消"); 
  47.         }else if(intent.getIntParam("cropStatus",0) == 2){ 
  48.             text.setText("剪裁成功"); 
  49.         } 
  50.  
  51.  
  52.         //获取button对象对应的component 
  53.         Button button = (Button) findComponentById(ResourceTable.Id_button); 
  54.         // 设置button的属性及背景 
  55.         ShapeElement background = new ShapeElement(); 
  56.        // background.setRgbColor(new RgbColor(125, 125, 255)); 
  57.         background.setCornerRadius(25); 
  58.        // button.setBackground(background); 
  59.         if (button != null) { 
  60.             // 绑定点击事件 
  61.             button.setClickedListener(new Component.ClickedListener() { 
  62.                 public void onClick(Component v) { 
  63.                     begincrop(); 
  64.                 } 
  65.             }); 
  66.         } 
  67.     } 
  68.  
  69.     public void begincrop(){ 
  70.         CropImage.activity() 
  71.                 .setContext(this) 
  72.                 .setSource(ResourceTable.Media_a9) 
  73.                 .setBundleName("com.example.javahm9"
  74.                 .setAbilityName("com.example.javahm9.MainAbility"
  75.                 .setRequset_code(1234) 
  76.                 .start(super.getAbility(),this); 
  77.     } 
  78.  
  79.     //处理剪裁结果 
  80.     private void handleCrop(Intent result) { 
  81.         int resultImg = result.getIntParam("resultImg",0); 
  82.         int result_code = result.getIntParam("result_code" , 0); 
  83.         if(resultImg != 0){ 
  84.             CropImage.handleImage(result_code , image); 
  85.              
  86.         } 
  87.     } 
  88.  
  89.     @Override 
  90.     public void onActive() { 
  91.         super.onActive(); 
  92.     } 
  93.  
  94.     @Override 
  95.     public void onForeground(Intent intent) { 
  96.         super.onForeground(intent); 
  97.     } 

3.裁剪工具类实现

  1. package com.example.javahm9.util; 
  2.  
  3. import ohos.aafwk.ability.Ability; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Component; 
  7. import ohos.agp.render.Canvas; 
  8. import ohos.agp.render.Paint; 
  9. import ohos.agp.render.PixelMapHolder; 
  10. import ohos.app.Context; 
  11. import ohos.media.image.PixelMap; 
  12. import ohos.utils.net.Uri; 
  13.  
  14. /** 
  15.  * @Author 李欣 
  16.  * <p> 
  17.  * 此类是一个工具类 
  18.  * <p> 
  19.  * 此类是面向于用户的 面向于MainAbilitySlice的 
  20.  * <p> 
  21.  * 此类提供设置一些参数的功能,提供了页面跳转,跳转到CropImageActivity的方法,有一些属性暂时用不到。 
  22.  */ 
  23.  
  24. public final class CropImage { 
  25.  
  26.     //存一个context 
  27.     private static Context mContext; 
  28.  
  29.     //图片资源文件 
  30.     private static int msource; 
  31.  
  32.     //裁减结果 
  33.     public static final boolean CROPFLAG = true
  34.  
  35.     //空的构造器 
  36.     private CropImage() { 
  37.     } 
  38.  
  39.     //内部类初始化 
  40.     public static ActivityBuilder activity() { 
  41.         return new ActivityBuilder(null , null , null , 0); 
  42.     } 
  43.  
  44.     /** 
  45.      * 这个方法对传进来的 component进行操作 
  46.      * 给这个空的component增加图片 旋转等等 
  47.      * @param result 一个参数用来判断返回的结果 以及如何处理 
  48.      * @param component 对这个component进行操作 
  49.      */ 
  50.     public static void handleImage(int result, Component component) { 
  51.  
  52.         //203表示裁剪成功(203为自定义的数字) 
  53.         if (result == 203) { 
  54.             //获得原始位图 
  55.             PixelMap pixelMap = BitmapUtils.getOriginalPixelMap(mContext , msource).get(); 
  56.  
  57.             //创建屏幕工具类 获得屏幕的宽度 
  58.             CropWindowHandler windowHandler = new CropWindowHandler(mContext); 
  59.  
  60.             /** 
  61.              * 缩放指数 原始的图片的缩放大小 
  62.              * 数值为原始图片的宽度(pixelMap.getImageInfo().size.width)除以屏幕的宽度(windowHandler.getWindowWidth()) 的倒数 
  63.              */ 
  64.             float ratio = (float) windowHandler.getWindowWidth()/(float) pixelMap.getImageInfo().size.width; 
  65.  
  66.             //获得裁剪后的位图 
  67.             PixelMap cropped = CropOverlayView.getCroppedPixelMap(); 
  68.             PixelMapHolder pixelMapHolder = new PixelMapHolder(cropped); 
  69.  
  70.             //创建bitmaputils工具类,获得位图相关数据 
  71.             BitmapUtils bitmapUtils = new BitmapUtils(mContext, cropped, 400, msource); 
  72.  
  73.             /** 
  74.              * 创建画位图的方法 
  75.              * 获取到之前的点击动作数组 这样就可以获得到具体最终的图片的方向 
  76.              * 并且依次按照图片方向进行旋转 
  77.              */ 
  78.             Component.DrawTask drawTask = new Component.DrawTask() { 
  79.                 @Override 
  80.                 public void onDraw(Component component, Canvas canvas) { 
  81.  
  82.                     //获得行为动作 
  83.                     int[] action = CropImageView.getActionResult(); 
  84.                     //获得动作数目 
  85.                     int actionIndex = CropImageView.getActionIndexResult(); 
  86.                     //循环执行旋转、翻转动作 
  87.                     for (int i = 0; i < actionIndex; i++) { 
  88.                         if (action[i] == 1) { 
  89.                             //rotate图像 
  90.                             canvas.rotate(90, bitmapUtils.getRealPixelMapWidth() / 2 * ratio, bitmapUtils.getRealPixelMapHeight() / 2 * ratio); 
  91.                         } else if (action[i] == 2) { 
  92.                             //水平翻转 
  93.                             //向下移动高度 
  94.                             canvas.translate(bitmapUtils.getRealPixelMapWidth() * ratio, 0); 
  95.                             //向y轴负方向缩放一倍 
  96.                             canvas.scale(-1f, 1f); 
  97.                         } else if (action[i] == 3) { 
  98.                             //垂直翻转 
  99.                             //向下移动高度 
  100.                             canvas.translate(0, bitmapUtils.getRealPixelMapHeight() * ratio); 
  101.                             //向y轴负方向缩放一倍 
  102.                             canvas.scale(1f, -1f); 
  103.                         } 
  104.                     } 
  105.  
  106.                     //按照原来的比例进行缩放 
  107.                     canvas.scale(ratio , ratio); 
  108.                     //画图 
  109.                     canvas.drawPixelMapHolder(pixelMapHolder, 200, 230, new Paint()); 
  110.                 } 
  111.             }; 
  112.  
  113.             //为component增加drawtask方法 
  114.             component.addDrawTask(drawTask); 
  115.         } 
  116.     } 
  117.  
  118.     public static PixelMap getCroppedPixelMap(){ 
  119.         return CropOverlayView.getCroppedPixelMap(); 
  120.     } 
  121.  
  122.     public static final class ActivityBuilder { 
  123.  
  124.         //设置图片的Uri 
  125.         private final Uri mSource; 
  126.  
  127.         //需要跳转回的bundle的名字 
  128.         private String bundleName; 
  129.  
  130.         //需要跳转回的Ability的名字 
  131.         private String abilityName; 
  132.  
  133.         //request_code 
  134.         private int request_code; 
  135.  
  136.         //初始化 记录一些信息 
  137.         private ActivityBuilder(Uri source , String bundle , String ability , int request) { 
  138.             mSource = source; 
  139.             bundleName = bundle; 
  140.             abilityName = ability; 
  141.             request_code = request; 
  142.         } 
  143.  
  144.         //返回一个intent 
  145.         public Intent getIntent(Context context) { 
  146.             return getIntent(context, CropImageActivity.class); 
  147.         } 
  148.  
  149.         //返回一个intent 用来实现页面跳转 
  150.         public Intent getIntent(Context context, Class<?> cls) { 
  151.             Intent intent = new Intent(); 
  152.             intent.setParam("source", msource); 
  153.             if(bundleName != null){ 
  154.                 intent.setParam("bundleName" , bundleName); 
  155.             } 
  156.             if(abilityName != null){ 
  157.                 intent.setParam("abilityName" , abilityName); 
  158.             } 
  159.             if(request_code != 0){ 
  160.                 intent.setParam("request_code" , request_code); 
  161.             } 
  162.             return intent; 
  163.         } 
  164.  
  165.         //页面跳转 
  166.         public void start(Ability ability, AbilitySlice abilitySlice) { 
  167.             start(ability, abilitySlice, request_code); 
  168.         } 
  169.  
  170.         //页面跳转 
  171.         public void start(Ability ability, AbilitySlice abilitySlice, int requestCode) { 
  172.             //给crop添加操作 
  173.             AbilitySlice cropImageAbilitySlice = new CropImageActivity(); 
  174.             abilitySlice.presentForResult(cropImageAbilitySlice, getIntent(ability), requestCode); 
  175.         } 
  176.  
  177.         //设置资源图片,被裁减的图片的id 
  178.         public ActivityBuilder setSource(int source) { 
  179.             msource = source; 
  180.             return this; 
  181.         } 
  182.  
  183.         //设置context 
  184.         public ActivityBuilder setContext(Context context) { 
  185.             mContext = context; 
  186.             return this; 
  187.         } 
  188.  
  189.         //设置需要跳转回的bundle名字 
  190.         public ActivityBuilder setBundleName(String s){ 
  191.             bundleName = s; 
  192.             return this; 
  193.         } 
  194.  
  195.         //设置需要跳转回的ability名字 
  196.         public ActivityBuilder setAbilityName(String s){ 
  197.             abilityName = s; 
  198.             return this; 
  199.         } 
  200.  
  201.         //设置需要跳转回时的code 
  202.         public ActivityBuilder setRequset_code(int i){ 
  203.             request_code = i; 
  204.             return this; 
  205.         } 
  206.  
  207.     } 
  208.  
  209.  
  210.  
  211.  
  212. package com.example.javahm9.util; 
  213.  
  214. import ohos.aafwk.ability.AbilitySlice; 
  215. import ohos.aafwk.content.Intent; 
  216. import ohos.aafwk.content.Operation; 
  217. import ohos.agp.colors.RgbColor; 
  218. import ohos.agp.components.Button; 
  219. import ohos.agp.components.Component; 
  220. import ohos.agp.components.DependentLayout; 
  221. import ohos.agp.components.element.ShapeElement; 
  222. import ohos.agp.utils.RectFloat; 
  223. import ohos.bundle.AbilityInfo; 
  224. import ohos.media.image.PixelMap; 
  225.  
  226. import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT; 
  227.  
  228. /** 
  229.  * @Author 李欣 
  230.  * 
  231.  * 此类为裁剪功能实现的主要页面 
  232.  * 
  233.  * 此页面上显示了一些功能性的button 
  234.  * 还有被裁减图片以及裁剪框 
  235.  */ 
  236.  
  237. public class CropImageActivity extends AbilitySlice { 
  238.  
  239.     //定义此slice的Dependent布局 
  240.     private DependentLayout myLayout = new DependentLayout(this); 
  241.  
  242.     //被裁减图片 
  243.     private Component mPicture; 
  244.  
  245.     //被裁减图片的位图 
  246.     private PixelMap mPixelMap; 
  247.  
  248.     //位图工具类 
  249.     private BitmapUtils mBitmapUtils; 
  250.  
  251.     //屏幕工具类 
  252.     private CropWindowHandler mCropWindowHandler; 
  253.  
  254.     //裁剪框 
  255.     private Component mCropBound; 
  256.  
  257.     //裁剪框工具类 
  258.     private CropOverlayView mCropOverlayView; 
  259.  
  260.     //图片资源 
  261.     private int mSource; 
  262.  
  263.     //图片上边距 
  264.     private final int topIndex = 400; 
  265.  
  266.     //图片工具类 
  267.     private CropImageView mCropImageView; 
  268.  
  269.     //裁减结果的矩阵 
  270.     public static RectFloat croppedRectFloat; 
  271.  
  272.     //裁减结果的pixelmap 
  273.     public static PixelMap croppedPixelMap; 
  274.  
  275.     @Override 
  276.     public void onStart(Intent intent) { 
  277.         super.onStart(intent); 
  278.  
  279.         //创建本slice的布局文件 
  280.         DependentLayout.LayoutConfig config = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT); 
  281.  
  282.         //设置默认竖屏 
  283.         setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT); 
  284.         //设置布局的背景 
  285.         myLayout.setLayoutConfig(config); 
  286.         ShapeElement element = new ShapeElement(); 
  287.         element.setRgbColor(new RgbColor(255, 255, 255)); 
  288.         myLayout.setBackground(element); 
  289.  
  290.         //设置button,image等 
  291.         setupViews(intent); 
  292.  
  293.         //加载裁剪框、背景图片等等 
  294.         loadInput(); 
  295.  
  296.         //加载布局 
  297.         super.setUIContent(myLayout); 
  298.     } 
  299.  
  300.     //按钮和图片初始化 
  301.     private void setupViews(Intent intent) { 
  302.         buttonInit(intent); 
  303.         imageInit(intent); 
  304.     } 
  305.  
  306.     //按钮初始化 
  307.     private void buttonInit(Intent intent) { 
  308.         //创建button 
  309.         Button cancel = cancelButton(intent); 
  310.         Button rotate = rotateButton(); 
  311. //        Button horfilp = horizontalFilpButton(); 
  312. //        Button verfilp =  verticalFilpButton(); 
  313.  
  314.         Button crop = cropButton(intent); 
  315.  
  316.         //将button添加到布局 
  317.         myLayout.addComponent(cancel); 
  318.         myLayout.addComponent(rotate); 
  319. //        myLayout.addComponent(horfilp); 
  320. //        myLayout.addComponent(verfilp); 
  321.         myLayout.addComponent(crop); 
  322.     } 
  323.  
  324.     //取消按钮 
  325.     private Button cancelButton(Intent intent) { 
  326.         //创建取消button 
  327.         Button cancel = new Button(this); 
  328.         //为button增加布局条件 
  329.         DependentLayout.LayoutConfig cancelLayoutConfig = new DependentLayout.LayoutConfig(); 
  330.         cancelLayoutConfig.setMargins(140, 50, 0, 0); 
  331.         cancelLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_TOP); 
  332.         cancelLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT); 
  333.         //设置背景颜色 
  334.         cancel.setLayoutConfig(cancelLayoutConfig); 
  335.         ShapeElement cancelElement = new ShapeElement(); 
  336.         cancelElement.setRgbColor(new RgbColor(155, 155, 155)); 
  337.         cancelElement.setCornerRadius(25); 
  338.         cancel.setBackground(cancelElement); 
  339.         //设置文本 
  340.         cancel.setText("取消"); 
  341.         cancel.setTextSize(55); 
  342.         cancel.setHeight(180); 
  343.         cancel.setWidth(220); 
  344.         //绑定点击方法 
  345.         cancel.setClickedListener(new Component.ClickedListener() { 
  346.             public void onClick(Component v) { 
  347.                 //为按钮绑定方法 
  348.                 cancel(intent); 
  349.             } 
  350.         }); 
  351.  
  352.         return cancel; 
  353.     } 
  354.  
  355.     //旋转按钮 
  356.     private Button rotateButton() { 
  357.         //创建旋转button 
  358.         Button rotate = new Button(this); 
  359.         //为button增加布局条件 
  360.         DependentLayout.LayoutConfig rotateLayoutConfig = new DependentLayout.LayoutConfig(); 
  361.         rotateLayoutConfig.setMargins(500, 50, 0, 0); 
  362.         rotateLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_TOP); 
  363.         rotateLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT); 
  364.         rotate.setLayoutConfig(rotateLayoutConfig); 
  365.         //设置背景颜色 
  366.         ShapeElement rotateElement = new ShapeElement(); 
  367.         rotateElement.setRgbColor(new RgbColor(255, 228, 181)); 
  368.         rotateElement.setCornerRadius(25); 
  369.         rotate.setBackground(rotateElement); 
  370.         //设置文本 
  371.         rotate.setText("旋转"); 
  372.         rotate.setTextSize(55); 
  373.         rotate.setHeight(180); 
  374.         rotate.setWidth(220); 
  375.         //绑定点击方法 
  376.         rotate.setClickedListener(new Component.ClickedListener() { 
  377.             public void onClick(Component v) { 
  378.                 rotate(); 
  379.             } 
  380.         }); 
  381.  
  382.         return rotate; 
  383.     } 
  384.  
  385.     //水平翻转按钮 
  386.     private Button horizontalFilpButton() { 
  387.         //创建翻转button 
  388.         Button filp = new Button(this); 
  389.         //为button增加布局条件 
  390.         DependentLayout.LayoutConfig filpLayoutConfig = new DependentLayout.LayoutConfig(); 
  391.         filpLayoutConfig.setMargins(0, 50, 300, 0); 
  392.         filpLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_TOP); 
  393.         filpLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT); 
  394.         //设置背景颜色 
  395.         filp.setLayoutConfig(filpLayoutConfig); 
  396.         ShapeElement filpElement = new ShapeElement(); 
  397.         filpElement.setRgbColor(new RgbColor(180, 238, 180)); 
  398.         filpElement.setCornerRadius(25); 
  399.         filp.setBackground(filpElement); 
  400.         //设置文本 
  401.         filp.setText("horFilp"); 
  402.         filp.setTextSize(40); 
  403.         filp.setHeight(85); 
  404.         filp.setWidth(220); 
  405.         //绑定点击方法 
  406.         filp.setClickedListener(new Component.ClickedListener() { 
  407.             public void onClick(Component v) { 
  408.                 horizontalFlip(); 
  409.             } 
  410.         }); 
  411.  
  412.         return filp; 
  413.     } 
  414.  
  415.     //垂直翻转按钮 
  416.     private Button verticalFilpButton() { 
  417.         //创建翻转button 
  418.         Button filp = new Button(this); 
  419.         //为button增加布局条件 
  420.         DependentLayout.LayoutConfig filpLayoutConfig = new DependentLayout.LayoutConfig(); 
  421.         filpLayoutConfig.setMargins(0, 145, 300, 0); 
  422.         filpLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_TOP); 
  423.         filpLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT); 
  424.         //设置背景颜色 
  425.         filp.setLayoutConfig(filpLayoutConfig); 
  426.         ShapeElement filpElement = new ShapeElement(); 
  427.         filpElement.setRgbColor(new RgbColor(180, 238, 180)); 
  428.         filpElement.setCornerRadius(25); 
  429.         filp.setBackground(filpElement); 
  430.         //设置文本 
  431.         filp.setText("verFilp"); 
  432.         filp.setTextSize(40); 
  433.         filp.setHeight(85); 
  434.         filp.setWidth(220); 
  435.         //绑定点击方法 
  436.         filp.setClickedListener(new Component.ClickedListener() { 
  437.             public void onClick(Component v) { 
  438.                 verticalFlip(); 
  439.             } 
  440.         }); 
  441.  
  442.         return filp; 
  443.     } 
  444.  
  445.     //裁剪按钮 
  446.     private Button cropButton(Intent intent) { 
  447.         //创建裁剪button 
  448.         Button crop = new Button(this); 
  449.         //为button增加布局条件 
  450.         DependentLayout.LayoutConfig cropLayoutConfig = new DependentLayout.LayoutConfig(); 
  451.         cropLayoutConfig.setMargins(820, 50, 0, 0); 
  452.         cropLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_TOP); 
  453.         cropLayoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT); 
  454.         //设置背景颜色 
  455.         crop.setLayoutConfig(cropLayoutConfig); 
  456.         ShapeElement cropElement = new ShapeElement(); 
  457.         cropElement.setRgbColor(new RgbColor(0, 125, 155)); 
  458.         cropElement.setCornerRadius(25); 
  459.         crop.setBackground(cropElement); 
  460.         //设置文本 
  461.         crop.setText("裁剪"); 
  462.         crop.setTextSize(55); 
  463.         crop.setHeight(180); 
  464.         crop.setWidth(220); 
  465.         //绑定点击方法 
  466.         crop.setClickedListener(new Component.ClickedListener() { 
  467.             public void onClick(Component v) { 
  468.                 crop(intent); 
  469.             } 
  470.         }); 
  471.  
  472.         return crop; 
  473.     } 
  474.  
  475.     //图片初始化 
  476.     private void imageInit(Intent intent) { 
  477.  
  478.         //获得图片的id 
  479.         int source = intent.getIntParam("source", 0); 
  480.         mSource = source; 
  481.  
  482.         //根据图片id获取pixelmap 
  483.         PixelMap pixelMapOriginal = BitmapUtils.getOriginalPixelMap(this, mSource).get(); 
  484.         mPixelMap = pixelMapOriginal; 
  485.  
  486.         //创建bitmaputils工具类,获得位图相关数据 
  487.         BitmapUtils bitmapUtils = new BitmapUtils(this , mPixelMap ,topIndex , mSource ); 
  488.  
  489.         //创建cropwindowhandler工具类,获得windows相关数据 
  490.         CropWindowHandler cropWindowHandler = new CropWindowHandler(this); 
  491.  
  492.         //创建图片工具类,用来获得图片 
  493.         mCropImageView = new CropImageView(mPixelMap , this ,  mSource , topIndex); 
  494.  
  495.         //获取展示图片的component 
  496.         mPicture = mCropImageView.getmPicture(); 
  497.  
  498.         //计算图片的位置,使得图片居中显示,计算出图片距离屏幕左边的空白 
  499.         int margin = cropWindowHandler.getWindowWidth()/2 - bitmapUtils.getPixelMapWidth()/2; 
  500.  
  501.         //给mPicture增加布局 
  502.         DependentLayout.LayoutConfig componentLayoutConfig = new DependentLayout.LayoutConfig(); 
  503.         componentLayoutConfig.setMargins(0, topIndex, 0, 0);//边距 
  504.         mPicture.setLayoutConfig(componentLayoutConfig); 
  505.  
  506.         //将图片加入布局 
  507.         myLayout.addComponent(mPicture); 
  508.  
  509.     } 
  510.  
  511.     //初始化工具类,加载裁剪框等等 
  512.     private void loadInput() { 
  513.  
  514.         //创建位图工具类 
  515.         mBitmapUtils = new BitmapUtils(this, mPixelMap, 400, mSource); 
  516.  
  517.         //创建屏幕工具类 
  518.         mCropWindowHandler = new CropWindowHandler(this); 
  519.  
  520.         //创建裁剪框的工具类 
  521.         mCropOverlayView = new CropOverlayView(this, mBitmapUtils, mCropWindowHandler); 
  522.  
  523.         //获得裁剪框 
  524.         mCropBound = mCropOverlayView.getmCropBound(); 
  525.  
  526.         //将裁剪框加入布局文件 
  527.         myLayout.addComponent(mCropBound); 
  528.     } 
  529.  
  530.     //取消裁剪方法 
  531.     private void cancel(Intent intentOriginal) { 
  532.         Intent intent = new Intent(); 
  533.  
  534.         //增加裁剪状态及结果 
  535.         intent.setParam("cropFlag", !CropImage.CROPFLAG); 
  536.         intent.setParam("cropStatus", 1); 
  537.  
  538.         // 通过Intent中的OperationBuilder类构造operation对象,指定设备标识(空串表示当前设备)、应用包名、Ability名称 
  539.         Operation operation = new Intent.OperationBuilder() 
  540.                 .withDeviceId(""
  541.                 .withBundleName(intentOriginal.getStringParam("bundleName")) 
  542.                 .withAbilityName(intentOriginal.getStringParam("abilityName")) 
  543.                 .build(); 
  544.         // 把operation设置到intent中 
  545.         intent.setOperation(operation); 
  546.  
  547.         //跳转 
  548.         startAbility(intent); 
  549.     } 
  550.  
  551.     //图片旋转的方法 
  552.     private void rotate(){ 
  553.         //图片旋转 
  554.         mCropImageView.rotateOnce(); 
  555.         //裁剪框旋转 
  556.         mCropOverlayView.rotateOnce(); 
  557.     } 
  558.  
  559.     //图片水平翻转的方法 
  560.     private void horizontalFlip(){ 
  561.         //图片翻转 
  562.         mCropImageView.horizontalFilp(); 
  563.         //裁剪框翻转 
  564.         mCropOverlayView.horizontalFilpOnce(); 
  565.     } 
  566.  
  567.     //图片垂直翻转的方法 
  568.     private void verticalFlip(){ 
  569.         mCropImageView.verticalFilp(); 
  570.         //裁剪框翻转 
  571.         mCropOverlayView.verticalFilpOnce(); 
  572.     } 
  573.  
  574.     //成功裁剪方法 
  575.     private void crop(Intent intentOriginal) { 
  576.  
  577.         //计算裁减后的pixelmap并存放于cropoverlayview中 
  578.         mCropOverlayView.croppedPixel(this); 
  579.  
  580.         //显示到MainActivity 
  581.         Intent intent = new Intent(); 
  582.  
  583.         //增加裁剪状态及结果 
  584.         intent.setParam("cropFlag", CropImage.CROPFLAG); 
  585.         intent.setParam("cropStatus", 2); 
  586.         intent.setParam("result_code" , 203); 
  587.  
  588.         RectFloat cropRect = mCropOverlayView.getmCropRect(); 
  589.         //塞入裁剪结果 
  590.         intent.setParam("resultImg", mSource); 
  591.         croppedRectFloat = mCropOverlayView.getmCropRect(); 
  592.         croppedPixelMap = mPixelMap; 
  593.  
  594.         // 通过Intent中的OperationBuilder类构造operation对象,指定设备标识(空串表示当前设备)、应用包名、Ability名称 
  595.         Operation operation = new Intent.OperationBuilder() 
  596.                 .withDeviceId(""
  597.                 .withBundleName(intentOriginal.getStringParam("bundleName")) 
  598.                 .withAbilityName(intentOriginal.getStringParam("abilityName")) 
  599.                 .build(); 
  600.         // 把operation设置到intent中 
  601.         intent.setOperation(operation); 
  602.  
  603.         //跳转 
  604.         startAbility(intent); 
  605.  
  606.     } 
  607.  
  608.     @Override 
  609.     protected void onStop() { 
  610.         super.onStop(); 
  611.     } 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-02-26 14:15:27

鸿蒙HarmonyOS应用开发

2021-03-03 09:42:26

鸿蒙HarmonyOS图片裁剪

2020-12-24 12:01:16

鸿蒙HarmonyOS应用开发

2021-03-02 14:34:48

鸿蒙HarmonyOS应用开发

2021-04-08 14:57:52

鸿蒙HarmonyOS应用

2021-03-09 09:35:09

鸿蒙HarmonyOS应用开发

2021-02-26 14:13:48

鸿蒙HarmonyOS应用开发

2021-03-19 10:05:23

鸿蒙HarmonyOS应用

2020-11-12 11:44:42

ubuntu

2021-01-06 11:21:56

鸿蒙HarmonyOS应用开发

2020-11-17 08:59:28

MQTT

2022-01-06 09:55:19

鸿蒙HarmonyOS应用

2022-08-09 16:01:24

应用开发鸿蒙

2020-12-08 09:45:33

鸿蒙OS应用开发

2020-11-10 11:58:17

鸿蒙应用开发

2022-07-15 12:58:02

鸿蒙携程华为

2020-12-03 10:05:07

鸿蒙OS
点赞
收藏

51CTO技术栈公众号