• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #include "common/screen.h"
16 #include "components/root_view.h"
17 #include "components/ui_image_view.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "components/ui_qrcode.h"
21 #include "components/ui_scroll_view.h"
22 #include "components/ui_view.h"
23 #include "ui_test.h"
24 
25 #define IMAGE_DIR "/data/img/"
26 #define GIF_PATH1 (IMAGE_DIR "01.gif")
27 #define GIF_PATH2 (IMAGE_DIR "02.gif")
28 #define PNG_PATH (IMAGE_DIR "03.png")
29 #define JPG_PATH (IMAGE_DIR "04.jpg")
30 
31 using namespace OHOS;
32 
33 class ImageDemo : public UIView::OnClickListener
34 {
35 public:
GetInstance()36     static ImageDemo *GetInstance()
37     {
38         static ImageDemo inst;
39         return &inst;
40     }
41 
42     void SetUp();
43     UIView *GetView();
44 
45 private:
ImageDemo()46     ImageDemo() {}
47     ~ImageDemo();
48 
49     bool OnClick(UIView &view, const ClickEvent &event) override;
50 
51     void CreateImageSwitch();
52 
53     void SetUpButton(UILabelButton *btn, const char *title);
54 
55     UIScrollView *container_ = nullptr;
56 
57     UIImageView *gifImageView_ = nullptr;
58     UILabelButton *gifToGif_ = nullptr;
59     UILabelButton *gifToJpeg_ = nullptr;
60     UILabelButton *gifToPng_ = nullptr;
61 
62     int16_t g_height = 0;
63 };
64 
DeleteChildren(UIView * view)65 static void DeleteChildren(UIView *view)
66 {
67     if (view == nullptr) {
68         return;
69     }
70     while (view != nullptr) {
71         UIView *tempView = view;
72         view = view->GetNextSibling();
73         if (tempView->IsViewGroup() && (tempView->GetViewType() != UI_DIGITAL_CLOCK)) {
74             DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
75         }
76         if (tempView->GetParent()) {
77             static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
78         }
79         delete tempView;
80     }
81 }
82 
SetUp()83 void ImageDemo::SetUp()
84 {
85     int16_t x = 25;
86     int16_t y = 25;
87     int16_t width = Screen::GetInstance().GetWidth() - 50;
88     int16_t height = Screen::GetInstance().GetHeight() - 50;
89     g_height = 0;
90     if (container_ == nullptr) {
91         container_ = new UIScrollView();
92         container_->SetPosition(x, y, width, height);
93         container_->SetHorizontalScrollState(false);
94     }
95 }
96 
~ImageDemo()97 ImageDemo::~ImageDemo()
98 {
99     DeleteChildren(container_);
100     container_ = nullptr;
101     gifImageView_ = nullptr;
102     gifToGif_ = nullptr;
103     gifToJpeg_ = nullptr;
104     gifToPng_ = nullptr;
105 }
106 
GetView()107 UIView *ImageDemo::GetView()
108 {
109     CreateImageSwitch();
110     return container_;
111 }
112 
CreateImageSwitch()113 void ImageDemo::CreateImageSwitch()
114 {
115     int16_t x = 100;
116     int16_t y = g_height;
117     int16_t width = 200;
118     int16_t height = 29;
119     uint8_t size = 20;
120     if (container_ == nullptr) {
121         return;
122     }
123     UILabel *label = new UILabel();
124     container_->Add(label);
125     label->SetPosition(x, y, width, height);
126     label->SetText("不同类型图片切换");
127     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, size);
128     g_height += 30;
129 
130     gifImageView_ = new UIImageView();
131     x = 48;
132     y = g_height;
133     gifImageView_->SetPosition(x, y);
134     gifImageView_->SetSrc(GIF_PATH1);
135     width = 100;
136     height = 100;
137     gifImageView_->Resize(width, height);
138     // gifImageView_->SetAutoEnable(false);
139     container_->Add(gifImageView_);
140     g_height += 150;
141 
142     gifToGif_ = new UILabelButton();
143     SetUpButton(gifToGif_, "切换GIF");
144     x = 48;
145     y = g_height + 10;
146     gifToGif_->SetPosition(x, y);
147     gifToJpeg_ = new UILabelButton();
148     SetUpButton(gifToJpeg_, "切换JPG");
149     x = 48 + 120;
150     y = g_height + 10;
151     gifToJpeg_->SetPosition(x, y);
152     gifToPng_ = new UILabelButton();
153     SetUpButton(gifToPng_, "切换PNG");
154     x = 48 + 240;
155     y = g_height + 10;
156     gifToPng_->SetPosition(x, y);
157 }
158 
SetUpButton(UILabelButton * btn,const char * title)159 void ImageDemo::SetUpButton(UILabelButton *btn, const char *title)
160 {
161     int16_t width = 100;
162     int16_t height = 50;
163     if (btn == nullptr) {
164         return;
165     }
166     container_->Add(btn);
167     btn->Resize(width, height);
168     btn->SetText(title);
169     btn->SetOnClickListener(this);
170 }
171 
OnClick(UIView & view,const ClickEvent & event)172 bool ImageDemo::OnClick(UIView &view, const ClickEvent &event)
173 {
174     if (gifImageView_ == nullptr) {
175         return false;
176     }
177     if (&view == gifToGif_) {
178         gifImageView_->SetSrc(GIF_PATH2);
179     } else if (&view == gifToJpeg_) {
180         gifImageView_->SetSrc(JPG_PATH);
181     } else if (&view == gifToPng_) {
182         gifImageView_->SetSrc(PNG_PATH);
183     }
184     return true;
185 }
186 
187 class QrcodeDemo
188 {
189 public:
GetInstance()190     static QrcodeDemo *GetInstance()
191     {
192         static QrcodeDemo inst;
193         return &inst;
194     }
195 
196     void SetUp();
197     UIView *GetView();
198 
199 private:
QrcodeDemo()200     QrcodeDemo() {}
~QrcodeDemo()201     ~QrcodeDemo()
202     {
203         DeleteChildren(container_);
204         container_ = nullptr;
205     }
206 
207     UIScrollView *container_ = nullptr;
208 };
209 
SetUp()210 void QrcodeDemo::SetUp()
211 {
212     int16_t x = 25;
213     int16_t y = 25;
214     int16_t width = Screen::GetInstance().GetWidth() - 50;
215     int16_t height = Screen::GetInstance().GetHeight() - 50;
216     if (container_ == nullptr) {
217         container_ = new UIScrollView();
218         container_->SetPosition(x, y, width, height);
219         container_->SetHorizontalScrollState(false);
220     }
221 }
222 
GetView()223 UIView *QrcodeDemo::GetView()
224 {
225     int16_t x = 100;
226     int16_t y = 100;
227     int16_t width = 300;
228     int16_t height = 30;
229     if (container_ == nullptr) {
230         return nullptr;
231     }
232 
233     UILabel *titleLabel = new UILabel();
234     titleLabel->SetPosition(x, y, width, height);
235     titleLabel->SetText("qrcode");
236 
237     UIQrcode *qrcode = new UIQrcode();
238     x = 100;
239     y = 150;
240     width = 200;
241     height = 200;
242     qrcode->SetPosition(100, 150, 200, 200);
243     const char *str = "鸿蒙轻量级GUI";
244     qrcode->SetQrcodeInfo(str);
245 
246     container_->Add(qrcode);
247     container_->Add(titleLabel);
248     return container_;
249 }
250 
ImageDemoStart()251 void ImageDemoStart()
252 {
253     int16_t x = 0;
254     int16_t y = 0;
255     RootView *rootView_ = RootView::GetInstance();
256     rootView_->SetPosition(x, y, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
257 
258     ImageDemo *view = ImageDemo::GetInstance();
259     // QrcodeDemo *view = QrcodeDemo::GetInstance();
260     view->SetUp();
261     rootView_->Add(view->GetView());
262     rootView_->Invalidate();
263 }
264