中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
1.ability_main.xml
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout ohos:id="$+id:dl" xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <!-- <Text--> <!-- ohos:id="$+id:text_helloworld"--> <!-- ohos:height="match_content"--> <!-- ohos:width="match_content"--> <!-- ohos:background_element="$graphic:background_ability_main"--> <!-- ohos:layout_alignment="horizontal_center"--> <!-- ohos:text="$string:mainability_HelloWorld"--> <!-- ohos:text_size="40vp"--> <!-- />--> <Image ohos:id="$+id:img" ohos:height="match_content" ohos:width="match_content" ohos:image_src="$media:icon1" /> </DirectionalLayout>
2.MainAbilitySlice
public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener { Image image; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); image = (Image)findComponentById(ResourceTable.Id_img); // 获取DirectionalLayout对象 DirectionalLayout directionalLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_dl); directionalLayout.setDoubleClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } boolean flag = false; @Override public void onDoubleClick(Component component) { if (flag){ image.setImageAndDecodeBounds(ResourceTable.Media_icon1); flag = false; }else{ image.setImageAndDecodeBounds(ResourceTable.Media_icon2); flag = true; } } }
3.效果图
4.双击后
微信扫码分享