2021-07-18 18:27:07
0
需求分析
现在有鸿蒙设备或者编辑器的用户,不妨打开鸿蒙图库的卡片看看,应该很容易发现它具有哪些功能。
比如,鸿蒙图库的卡片可以根据用户的选择,显示指定图片内容到卡片之上,这里就需要我们实现卡片与用户的交互与编辑。
下面,我们来分别实现卡片的功能以及交互。
卡片设计
首先,我们需要实现卡片的默认样式,因为我们这里设计的是一个2*2的Java卡片,所以其样式在XML布局文件中,代码如下所示(默认的方式创建Java卡片即可):
- <?xml version="1.0" encoding="utf-8"?>
- <DependentLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:background_element="#FFFFFF"
- ohos:remote="true">
- <Image
- ohos:id="$+id:form_grid_pattern_widget_image"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:image_src="$media:form_grid_pattern_widget_default_image_2"
- ohos:scale_mode="stretch"/>
- </DependentLayout>
复制代码
接着,我们需要实现用户卡片编辑功能,所以这里,我们需要在配置文件config.json文件中配置卡片的编辑界面,代码如下:
- "forms": [
- {
- "landscapeLayouts": [
- "$layout:form_grid_pattern_widget_2_2"
- ],
- "isDefault": true,
- "scheduledUpdateTime": "10:30",
- "formConfigAbility": "ability://com.liyuanjinglyj.gifcard.ChoiceImageAbility",
- "defaultDimension": "2*2",
- "name": "widget",
- "description": "This is a service widget",
- "colorMode": "auto",
- "type": "Java",
- "supportDimensions": [
- "2*2"
- ],
- "portraitLayouts": [
- "$layout:form_grid_pattern_widget_2_2"
- ],
- "updateEnabled": true,
- "updateDuration": 1
- }
- ],
复制代码
这里的编辑界面就是ChoiceImageAbility。而这个界面我们只需要实现选择图片功能即可。所以,一个按钮就可以了,布局文件如下(ability_choice_image.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="center"
- ohos:orientation="vertical">
- <Button
- ohos:id="$+id:ability_choice_image_button"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text_size="25vp"
- ohos:text="选择图片"
- ohos:text_color="#FF0000"/>
- </DirectionalLayout>
复制代码
而ChoiceImageAbility界面的交互代码如下所示:
- public class ChoiceImageAbility extends Ability {
- private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x0001, "选择图片测试");
- private Button button;
- private long formId;
- private final int imgRequestCode=123;
- private ImageSource imageSource = null;
- private PixelMap pixelMap=null;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_choice_image);
- WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.BLACK.getValue());
- formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, -1);
- requestPermissionsFromUser(new String[]{"ohos.permission.READ_USER_STORAGE"},imgRequestCode);
- this.button=(Button)findComponentById(ResourceTable.Id_ability_choice_image_button);
- this.button.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- Intent intent = new Intent();
- Operation opt=new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();
- intent.setOperation(opt);
- intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
- intent.setType("image/*");
- startAbilityForResult(intent, imgRequestCode);
- }
- });
- }
- @Override
- protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
- if(requestCode==imgRequestCode)
- {
- HiLog.info(label,"选择图片getUriString:"+resultData.getUriString());
- String chooseImgUri=resultData.getUriString();
- HiLog.info(label,"选择图片getScheme:"+chooseImgUri.substring(chooseImgUri.lastIndexOf('/')));
- DataAbilityHelper helper=DataAbilityHelper.creator(getContext());
- String chooseImgId=null;
- if(chooseImgUri.lastIndexOf("%3A")!=-1){
- chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A")+3);
- }
- else {
- chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf('/')+1);
- }
- Uri uri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,chooseImgId);
- HiLog.error(label,"选择图片dataability路径:"+uri.toString());
- TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
- Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
- @Override
- public void run() {
- try {
- FileDescriptor fd = helper.openFile(uri, "r");
- imageSource = ImageSource.create(fd, null);
- pixelMap = imageSource.createPixelmap(null);
- getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
- @Override
- public void run() {
- ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_form_grid_pattern_widget_2_2, getContext());
- componentProvider.setPixelMap(ResourceTable.Id_form_grid_pattern_widget_image,"setPixelMap", pixelMap);
- try {
- updateForm(formId,componentProvider);
- } catch (FormException e) {
- e.printStackTrace();
- }
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (imageSource != null) {
- terminateAbility();
- }
- }
- }
- });
- }
- }
- }
复制代码
当用户点击卡片进行交互的时候,默认就会弹出获取存储的权限,毕竟我们是需要选择手机图库中的文件用于显示的。
而且当用户选择图片完成之后,除了替换卡片上的图片之外,编辑界面肯定也是需要关闭的,所以finally之后,需要调用terminateAbility()关闭编辑界面。
到这里,我们仿照图库选择自定义卡片图片的功能就完全实现了。具体效果如头视频所示。
代码地址:https://gitee.com/liyuanjinglyj/GIFCard
附件:
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。
侵权投诉