• 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     g_height = 0;
86     if (container_ == nullptr) {
87         container_ = new UIScrollView();
88         container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
89         container_->SetHorizontalScrollState(false);
90     }
91 }
92 
~ImageDemo()93 ImageDemo::~ImageDemo()
94 {
95     DeleteChildren(container_);
96     container_ = nullptr;
97     gifImageView_ = nullptr;
98     gifToGif_ = nullptr;
99     gifToJpeg_ = nullptr;
100     gifToPng_ = nullptr;
101 }
102 
GetView()103 UIView *ImageDemo::GetView()
104 {
105     CreateImageSwitch();
106     return container_;
107 }
108 
CreateImageSwitch()109 void ImageDemo::CreateImageSwitch()
110 {
111     if (container_ == nullptr) {
112         return;
113     }
114     UILabel *label = new UILabel();
115     container_->Add(label);
116     label->SetPosition(100, g_height, 200, 29);
117     label->SetText("不同类型图片切换");
118     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
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     // gifImageView_->SetAutoEnable(false);
126     container_->Add(gifImageView_);
127     g_height += 150;
128 
129     gifToGif_ = new UILabelButton();
130     SetUpButton(gifToGif_, "切换GIF");
131     gifToGif_->SetPosition(48, g_height + 10);
132     gifToJpeg_ = new UILabelButton();
133     SetUpButton(gifToJpeg_, "切换JPG");
134     gifToJpeg_->SetPosition(48 + 120, g_height + 10);
135     gifToPng_ = new UILabelButton();
136     SetUpButton(gifToPng_, "切换PNG");
137     gifToPng_->SetPosition(48 + 240, g_height + 10);
138 }
139 
SetUpButton(UILabelButton * btn,const char * title)140 void ImageDemo::SetUpButton(UILabelButton *btn, const char *title)
141 {
142     if (btn == nullptr) {
143         return;
144     }
145     container_->Add(btn);
146     btn->Resize(100, 50);
147     btn->SetText(title);
148     btn->SetOnClickListener(this);
149 }
150 
OnClick(UIView & view,const ClickEvent & event)151 bool ImageDemo::OnClick(UIView &view, const ClickEvent &event)
152 {
153     if (gifImageView_ == nullptr) {
154         return false;
155     }
156     if (&view == gifToGif_) {
157         gifImageView_->SetSrc(GIF_PATH2);
158     } else if (&view == gifToJpeg_) {
159         gifImageView_->SetSrc(JPG_PATH);
160     } else if (&view == gifToPng_) {
161         gifImageView_->SetSrc(PNG_PATH);
162     }
163     return true;
164 }
165 
166 class QrcodeDemo
167 {
168 public:
GetInstance()169     static QrcodeDemo *GetInstance()
170     {
171         static QrcodeDemo inst;
172         return &inst;
173     }
174 
175     void SetUp();
176     UIView *GetView();
177 
178 private:
QrcodeDemo()179     QrcodeDemo() {}
~QrcodeDemo()180     ~QrcodeDemo()
181     {
182         DeleteChildren(container_);
183         container_ = nullptr;
184     }
185 
186     UIScrollView *container_ = nullptr;
187 };
188 
SetUp()189 void QrcodeDemo::SetUp()
190 {
191     if (container_ == nullptr) {
192         container_ = new UIScrollView();
193         container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
194         container_->SetHorizontalScrollState(false);
195     }
196 }
197 
GetView()198 UIView *QrcodeDemo::GetView()
199 {
200     if (container_ == nullptr) {
201         return nullptr;
202     }
203 
204     UILabel *titleLabel = new UILabel();
205     titleLabel->SetPosition(100, 100, 300, 30);
206     titleLabel->SetText("qrcode");
207 
208     UIQrcode *qrcode = new UIQrcode();
209     qrcode->SetPosition(100, 150, 200, 200);
210     const char *str = "鸿蒙轻量级GUI";
211     qrcode->SetQrcodeInfo(str);
212 
213     container_->Add(qrcode);
214     container_->Add(titleLabel);
215     return container_;
216 }
217 
ImageDemoStart()218 void ImageDemoStart()
219 {
220     RootView *rootView_ = RootView::GetInstance();
221     rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
222 
223     ImageDemo *view = ImageDemo::GetInstance();
224     // QrcodeDemo *view = QrcodeDemo::GetInstance();
225     view->SetUp();
226     rootView_->Add(view->GetView());
227     rootView_->Invalidate();
228 }
229