1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common/screen.h"
17 #include "components/root_view.h"
18 #include "components/ui_image_view.h"
19 #include "components/ui_label.h"
20 #include "components/ui_label_button.h"
21 #include "components/ui_qrcode.h"
22 #include "components/ui_scroll_view.h"
23 #include "components/ui_view.h"
24 #include "ui_test.h"
25
26 #define IMAGE_DIR "/data/img/"
27 #define GIF_PATH1 (IMAGE_DIR "01.gif")
28 #define GIF_PATH2 (IMAGE_DIR "02.gif")
29 #define PNG_PATH (IMAGE_DIR "03.png")
30 #define JPG_PATH (IMAGE_DIR "04.jpg")
31
32 using namespace OHOS;
33
34 class ImageDemo : public UIView::OnClickListener
35 {
36 public:
GetInstance()37 static ImageDemo *GetInstance()
38 {
39 static ImageDemo inst;
40 return &inst;
41 }
42
43 void SetUp();
44 UIView *GetView();
45
46 private:
ImageDemo()47 ImageDemo() {}
48 ~ImageDemo();
49
50 bool OnClick(UIView &view, const ClickEvent &event) override;
51
52 void CreateImageSwitch();
53
54 void SetUpButton(UILabelButton *btn, const char *title);
55
56 UIScrollView *container_ = nullptr;
57
58 UIImageView *gifImageView_ = nullptr;
59 UILabelButton *gifToGif_ = nullptr;
60 UILabelButton *gifToJpeg_ = nullptr;
61 UILabelButton *gifToPng_ = nullptr;
62
63 int16_t g_height = 0;
64 };
65
DeleteChildren(UIView * view)66 static void DeleteChildren(UIView *view)
67 {
68 if (view == nullptr) {
69 return;
70 }
71 while (view != nullptr) {
72 UIView *tempView = view;
73 view = view->GetNextSibling();
74 if (tempView->IsViewGroup() && (tempView->GetViewType() != UI_DIGITAL_CLOCK)) {
75 DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
76 }
77 if (tempView->GetParent()) {
78 static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
79 }
80 delete tempView;
81 }
82 }
83
SetUp()84 void ImageDemo::SetUp()
85 {
86 g_height = 0;
87 if (container_ == nullptr) {
88 container_ = new UIScrollView();
89 container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
90 container_->SetHorizontalScrollState(false);
91 }
92 }
93
~ImageDemo()94 ImageDemo::~ImageDemo()
95 {
96 DeleteChildren(container_);
97 container_ = nullptr;
98 gifImageView_ = nullptr;
99 gifToGif_ = nullptr;
100 gifToJpeg_ = nullptr;
101 gifToPng_ = nullptr;
102 }
103
GetView()104 UIView *ImageDemo::GetView()
105 {
106 CreateImageSwitch();
107 return container_;
108 }
109
CreateImageSwitch()110 void ImageDemo::CreateImageSwitch()
111 {
112 if (container_ == nullptr) {
113 return;
114 }
115 UILabel *label = new UILabel();
116 container_->Add(label);
117 label->SetPosition(100, g_height, 200, 29);
118 label->SetText("不同类型图片切换");
119 g_height += 30;
120
121 gifImageView_ = new UIImageView();
122 gifImageView_->SetPosition(48, g_height);
123 gifImageView_->SetSrc(GIF_PATH1);
124 gifImageView_->Resize(100, 100);
125 container_->Add(gifImageView_);
126 g_height += 150;
127
128 gifToGif_ = new UILabelButton();
129 SetUpButton(gifToGif_, "切换GIF");
130 gifToGif_->SetPosition(48, g_height + 10);
131 gifToJpeg_ = new UILabelButton();
132 SetUpButton(gifToJpeg_, "切换JPG");
133 gifToJpeg_->SetPosition(48 + 120, g_height + 10);
134 gifToPng_ = new UILabelButton();
135 SetUpButton(gifToPng_, "切换PNG");
136 gifToPng_->SetPosition(48 + 240, g_height + 10);
137 }
138
SetUpButton(UILabelButton * btn,const char * title)139 void ImageDemo::SetUpButton(UILabelButton *btn, const char *title)
140 {
141 if (btn == nullptr) {
142 return;
143 }
144 container_->Add(btn);
145 btn->Resize(100, 50);
146 btn->SetText(title);
147 btn->SetOnClickListener(this);
148 }
149
OnClick(UIView & view,const ClickEvent & event)150 bool ImageDemo::OnClick(UIView &view, const ClickEvent &event)
151 {
152 if (gifImageView_ == nullptr) {
153 return false;
154 }
155 if (&view == gifToGif_) {
156 gifImageView_->SetSrc(GIF_PATH2);
157 } else if (&view == gifToJpeg_) {
158 gifImageView_->SetSrc(JPG_PATH);
159 } else if (&view == gifToPng_) {
160 gifImageView_->SetSrc(PNG_PATH);
161 }
162 return true;
163 }
164
165 class QrcodeDemo
166 {
167 public:
GetInstance()168 static QrcodeDemo *GetInstance()
169 {
170 static QrcodeDemo inst;
171 return &inst;
172 }
173
174 void SetUp();
175 UIView *GetView();
176
177 private:
QrcodeDemo()178 QrcodeDemo() {}
~QrcodeDemo()179 ~QrcodeDemo()
180 {
181 DeleteChildren(container_);
182 container_ = nullptr;
183 }
184
185 UIScrollView *container_ = nullptr;
186 };
187
SetUp()188 void QrcodeDemo::SetUp()
189 {
190 if (container_ == nullptr) {
191 container_ = new UIScrollView();
192 container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
193 container_->SetHorizontalScrollState(false);
194 }
195 }
196
GetView()197 UIView *QrcodeDemo::GetView()
198 {
199 if (container_ == nullptr) {
200 return nullptr;
201 }
202
203 UILabel *titleLabel = new UILabel();
204 titleLabel->SetPosition(100, 100, 300, 30);
205 titleLabel->SetText("qrcode");
206
207 UIQrcode *qrcode = new UIQrcode();
208 qrcode->SetPosition(100, 150, 200, 200);
209 const char *str = "鸿蒙轻量级GUI";
210 qrcode->SetQrcodeInfo(str);
211
212 container_->Add(qrcode);
213 container_->Add(titleLabel);
214 return container_;
215 }
216
ImageDemoStart()217 void ImageDemoStart()
218 {
219 RootView *rootView_ = RootView::GetInstance();
220 rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
221
222 ImageDemo *view = ImageDemo::GetInstance();
223 view->SetUp();
224 rootView_->Add(view->GetView());
225 rootView_->Invalidate();
226 }
227