1 /*
2 * Copyright (c) 2020-2021 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 "ui_test_image_animator.h"
17 #include "common/screen.h"
18 #include "test_resource_config.h"
19
20 namespace OHOS {
21 namespace {
22 static ImageAnimatorInfo g_imageAnimatorInfo[4] = {
23 {IMAGE_ANIMATOR_0_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
24 {IMAGE_ANIMATOR_1_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
25 {IMAGE_ANIMATOR_2_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
26 {IMAGE_ANIMATOR_3_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
27 };
28
29 static ImageAnimatorInfo g_imageAnimatorInfo2[4] = {
30 {BLUE_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
31 {RED_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
32 {GREEN_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
33 {YELLOW_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
34 };
35 } // namespace
36
SetUp()37 void UITestImageAnimator::SetUp()
38 {
39 if (container_ == nullptr) {
40 container_ = new UIScrollView();
41 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
42
43 UIViewGroup* uiViewGroup = new UIViewGroup();
44 // 320: width; 390: height
45 uiViewGroup->SetPosition(0, 0, 320, 390);
46 container_->Add(uiViewGroup);
47 UILabel* label = new UILabel();
48 uiViewGroup->Add(label);
49 // 288: width; 48: height
50 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
51 label->SetText("UIImageAnimator效果");
52 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
53 UIViewGroup* uiViewGroupFrame_ = new UIViewGroup();
54 uiViewGroup->Add(uiViewGroupFrame_);
55 // 288: width; 336: height
56 uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
57 uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
58 uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
59 uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
60 uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
61 uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
62
63 imageAnimator_ = new UIImageAnimatorView();
64 imageAnimator_->SetPosition(50, 50, 200, 200); // 50 : offset 50 : offset 200 : offset 200: offset
65 imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, 4, 100); // 4: the number of images, 100: updating time
66 imageAnimator_->Start();
67 listener_ = new TestAnimatorStopListener(container_);
68 listener_->Init();
69 uiViewGroupFrame_->Add(imageAnimator_);
70 imageAnimator_->LayoutCenterOfParent();
71 }
72
73 if (layout_ == nullptr) {
74 layout_ = new GridLayout();
75 uint16_t height = Screen::GetInstance().GetHeight();
76 layout_->SetPosition(370, 0, 320, height - 50); // 370: x , 320: width, 50: offset
77 container_->Add(layout_);
78 layout_->SetLayoutDirection(LAYOUT_VER);
79 layout_->SetRows(9); // 9 : rows
80 layout_->SetCols(2); // 2 : columns
81 }
82
83 InitImageInfo();
84 }
85
InitImageInfo()86 void UITestImageAnimator::InitImageInfo()
87 {
88 const uint16_t pixelByteSize = 4; // 4: bytes per pixel
89 const uint16_t width = 100; // 100: image's width
90 const uint16_t height = 100; // 100: image's height
91 uint32_t dataSize = width * height * pixelByteSize;
92 uint8_t* srcData1 = static_cast<uint8_t*>(UIMalloc(dataSize));
93 if (srcData1 == nullptr) {
94 return;
95 }
96
97 for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
98 srcData1[i] = 255; // 255: pixel value
99 srcData1[i + 1] = 0; // 1: set green channel
100 srcData1[i + 2] = 0; // 2: set red channel
101 srcData1[i + 3] = OPA_OPAQUE; // 3: set alpha channel
102 }
103 imageInfo1_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
104 if (imageInfo1_ == nullptr) {
105 UIFree(srcData1);
106 return;
107 }
108
109 imageInfo1_->header.width = width;
110 imageInfo1_->header.height = height;
111 imageInfo1_->header.colorMode = ColorMode::ARGB8888;
112 imageInfo1_->dataSize = dataSize;
113 imageInfo1_->data = srcData1;
114
115 uint8_t* srcData2 = static_cast<uint8_t*>(UIMalloc(dataSize));
116 if (srcData2 == nullptr) {
117 UIFree(imageInfo1_);
118 UIFree(srcData1);
119 return;
120 }
121 for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
122 srcData2[i] = 0; // set blue channel
123 srcData2[i + 1] = 255; // 1: set green channel 255: pixel value
124 srcData2[i + 2] = 0; // 2: set red channel
125 srcData2[i + 3] = OPA_OPAQUE; // 3: set alpha channel
126 }
127
128 imageInfo2_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
129 if (imageInfo2_ == nullptr) {
130 UIFree(srcData2);
131 UIFree(imageInfo1_);
132 UIFree(srcData1);
133 return;
134 }
135 imageInfo2_->header.width = width;
136 imageInfo2_->header.height = height;
137 imageInfo2_->header.colorMode = ARGB8888;
138 imageInfo2_->dataSize = dataSize;
139 imageInfo2_->data = srcData2;
140
141 uint8_t* srcData3 = static_cast<uint8_t*>(UIMalloc(dataSize));
142 if (srcData3 == nullptr) {
143 UIFree(imageInfo2_);
144 UIFree(srcData2);
145 UIFree(imageInfo1_);
146 UIFree(srcData1);
147 return;
148 }
149 for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
150 srcData3[i] = 0; // set blue channel
151 srcData3[i + 1] = 0; // 1: set green channel
152 srcData3[i + 2] = 255; // 2: set red channel 255: pixel value
153 srcData3[i + 3] = OPA_OPAQUE; // 3: set alpha channel
154 }
155
156 imageInfo3_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
157 if (imageInfo3_ == nullptr) {
158 UIFree(srcData3);
159 UIFree(imageInfo2_);
160 UIFree(srcData2);
161 UIFree(imageInfo1_);
162 UIFree(srcData1);
163 return;
164 }
165 imageInfo3_->header.width = width;
166 imageInfo3_->header.height = height;
167 imageInfo3_->header.colorMode = ARGB8888;
168 imageInfo3_->dataSize = dataSize;
169 imageInfo3_->data = srcData3;
170
171 imageAnimatorImageInfo_[0].imageInfo = imageInfo1_;
172 imageAnimatorImageInfo_[0].pos = {84, 108};
173 imageAnimatorImageInfo_[0].width = 100; // 100: width value
174 imageAnimatorImageInfo_[0].height = 100; // 100: height value
175 imageAnimatorImageInfo_[0].imageType = IMG_SRC_IMAGE_INFO;
176
177 imageAnimatorImageInfo_[1].imageInfo = imageInfo2_;
178 imageAnimatorImageInfo_[1].pos = {84, 108};
179 imageAnimatorImageInfo_[1].width = 100; // 100: width value
180 imageAnimatorImageInfo_[1].height = 100; // 100: height value
181 imageAnimatorImageInfo_[1].imageType = IMG_SRC_IMAGE_INFO;
182
183 imageAnimatorImageInfo_[2].imageInfo = imageInfo3_; // 2: image index
184 imageAnimatorImageInfo_[2].pos = {84, 108}; // 2: image index
185 imageAnimatorImageInfo_[2].width = 100; // 2: image index, 100: width value
186 imageAnimatorImageInfo_[2].height = 100; // 2: image index, 100: height value
187 imageAnimatorImageInfo_[2].imageType = IMG_SRC_IMAGE_INFO; // 2: image index
188 }
189
TearDown()190 void UITestImageAnimator::TearDown()
191 {
192 DeleteChildren(container_);
193 container_ = nullptr;
194 imageAnimator_ = nullptr;
195 layout_ = nullptr;
196 delete listener_;
197 listener_ = nullptr;
198
199 UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo1_->data)));
200 imageInfo1_->data = nullptr;
201 UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo2_->data)));
202 imageInfo2_->data = nullptr;
203 UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo3_->data)));
204 imageInfo3_->data = nullptr;
205 UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo1_)));
206 imageInfo1_ = nullptr;
207 UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo2_)));
208 imageInfo2_ = nullptr;
209 UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo3_)));
210 imageInfo3_ = nullptr;
211 }
212
GetTestView()213 const UIView* UITestImageAnimator::GetTestView()
214 {
215 UIKitImageAnimatorTestStart001();
216 UIKitImageAnimatorTestStop002();
217 UIKitImageAnimatorTestPause003();
218 UIKitImageAnimatorTestResume004();
219 UIKitImageAnimatorTestSetImageAnimatorSrc005();
220 UIKitImageAnimatorTestSetTickOfUpdate006();
221 UIKitImageAnimatorTestSetSizeFixed007();
222 UIKitImageAnimatorTestSetRepeat008();
223 UIKitImageAnimatorTestSetReverse009();
224 UIKitImageAnimatorTestSetAnimatorStopListener010();
225 UIKitImageAnimatorTestSetRepeatTimes011();
226 UIKitImageAnimatorTestSetFillModeTrueForward012();
227 UIKitImageAnimatorTestSetFillModeTrueBackward013();
228 UIKitImageAnimatorTestSetFillModeFalseForward014();
229 UIKitImageAnimatorTestSetFillModeFalseBackward015();
230 UIKitImageAnimatorTestSetImageInfo016();
231
232 layout_->LayoutChildren();
233 return container_;
234 }
235
SetUpButton(UILabelButton * btn,const char * title)236 void UITestImageAnimator::SetUpButton(UILabelButton* btn, const char* title)
237 {
238 if (btn == nullptr) {
239 return;
240 }
241 layout_->Add(btn);
242 btn->Resize(BUTTON_WIDHT2, BUTTON_HEIGHT2);
243 btn->SetText(title);
244 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
245 btn->SetOnClickListener(this);
246 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
247 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
248 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
249 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
250 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
251 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
252 }
253
UIKitImageAnimatorTestStart001()254 void UITestImageAnimator::UIKitImageAnimatorTestStart001()
255 {
256 startBtn_ = new UILabelButton();
257 SetUpButton(startBtn_, "Start");
258 }
259
UIKitImageAnimatorTestStop002()260 void UITestImageAnimator::UIKitImageAnimatorTestStop002()
261 {
262 stopBtn_ = new UILabelButton();
263 SetUpButton(stopBtn_, "Stop");
264 }
265
UIKitImageAnimatorTestPause003()266 void UITestImageAnimator::UIKitImageAnimatorTestPause003()
267 {
268 pauseBtn_ = new UILabelButton();
269 SetUpButton(pauseBtn_, "Pause");
270 }
271
UIKitImageAnimatorTestResume004()272 void UITestImageAnimator::UIKitImageAnimatorTestResume004()
273 {
274 resumeBtn_ = new UILabelButton();
275 SetUpButton(resumeBtn_, "Resume");
276 }
277
UIKitImageAnimatorTestSetImageAnimatorSrc005()278 void UITestImageAnimator::UIKitImageAnimatorTestSetImageAnimatorSrc005()
279 {
280 setImageBtn_ = new UILabelButton();
281 SetUpButton(setImageBtn_, "设置图片");
282 }
283
UIKitImageAnimatorTestSetTickOfUpdate006()284 void UITestImageAnimator::UIKitImageAnimatorTestSetTickOfUpdate006()
285 {
286 setSpeedBtn_ = new UILabelButton();
287 SetUpButton(setSpeedBtn_, "设置速度");
288 }
289
UIKitImageAnimatorTestSetSizeFixed007()290 void UITestImageAnimator::UIKitImageAnimatorTestSetSizeFixed007()
291 {
292 fixedBtn_ = new UILabelButton();
293 SetUpButton(fixedBtn_, "图片大小固定");
294 }
295
UIKitImageAnimatorTestSetRepeat008()296 void UITestImageAnimator::UIKitImageAnimatorTestSetRepeat008()
297 {
298 repeatBtn_ = new UILabelButton();
299 noRepeatBtn_ = new UILabelButton();
300 SetUpButton(repeatBtn_, "循环播放");
301 SetUpButton(noRepeatBtn_, "播放一次 ");
302 }
303
UIKitImageAnimatorTestSetReverse009()304 void UITestImageAnimator::UIKitImageAnimatorTestSetReverse009()
305 {
306 reverseOrderBtn_ = new UILabelButton();
307 positiveOrderBtn_ = new UILabelButton();
308 SetUpButton(reverseOrderBtn_, "逆序播放");
309 SetUpButton(positiveOrderBtn_, "正序播放");
310 }
311
UIKitImageAnimatorTestSetAnimatorStopListener010()312 void UITestImageAnimator::UIKitImageAnimatorTestSetAnimatorStopListener010()
313 {
314 listenerBtn_ = new UILabelButton();
315 SetUpButton(listenerBtn_, "播放结束监听");
316 }
317
UIKitImageAnimatorTestSetRepeatTimes011()318 void UITestImageAnimator::UIKitImageAnimatorTestSetRepeatTimes011()
319 {
320 repeatTimesBtn_ = new UILabelButton();
321 SetUpButton(repeatTimesBtn_, "播放次数+");
322 }
323
UIKitImageAnimatorTestSetFillModeTrueForward012()324 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeTrueForward012()
325 {
326 fillModeTrueForwardBtn_ = new UILabelButton();
327 SetUpButton(fillModeTrueForwardBtn_, "fillMode true 正播");
328 }
329
UIKitImageAnimatorTestSetFillModeTrueBackward013()330 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeTrueBackward013()
331 {
332 fillModeTrueBackwardBtn_ = new UILabelButton();
333 SetUpButton(fillModeTrueBackwardBtn_, "fillMode true 逆播");
334 }
335
UIKitImageAnimatorTestSetFillModeFalseForward014()336 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeFalseForward014()
337 {
338 fillModeFalseForwardBtn_ = new UILabelButton();
339 SetUpButton(fillModeFalseForwardBtn_, "fillMode false 正播");
340 }
341
UIKitImageAnimatorTestSetFillModeFalseBackward015()342 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeFalseBackward015()
343 {
344 fillModeFalseBackwardBtn_ = new UILabelButton();
345 SetUpButton(fillModeFalseBackwardBtn_, "fillMode false 逆播");
346 }
347
UIKitImageAnimatorTestSetImageInfo016()348 void UITestImageAnimator::UIKitImageAnimatorTestSetImageInfo016()
349 {
350 setImageInfoBtn_ = new UILabelButton();
351 SetUpButton(setImageInfoBtn_, "设置ImageInfo");
352 }
353
OnClick(UIView & view,const ClickEvent & event)354 bool UITestImageAnimator::OnClick(UIView& view, const ClickEvent& event)
355 {
356 if (&view == startBtn_) {
357 imageAnimator_->Start();
358 } else if (&view == stopBtn_) {
359 imageAnimator_->Stop();
360 } else if (&view == pauseBtn_) {
361 imageAnimator_->Pause();
362 } else if (&view == resumeBtn_) {
363 imageAnimator_->Resume();
364 } else if (&view == setImageBtn_) {
365 imageAnimator_->Stop();
366 imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo2, 4); // 4: the number of images
367 imageAnimator_->Start();
368 } else if (&view == setSpeedBtn_) {
369 imageAnimator_->Stop();
370 imageAnimator_->SetTimeOfUpdate(10); // 10: set time of update
371 imageAnimator_->Start();
372 } else if (&view == fixedBtn_) {
373 imageAnimator_->SetSizeFixed(true);
374 imageAnimator_->SetPosition(50, 50, 200, 200); // 50 : offset 50 : offset 200 : offset 200 :offset
375 } else if (&view == repeatBtn_) {
376 imageAnimator_->Stop();
377 imageAnimator_->SetRepeat(true);
378 imageAnimator_->Start();
379 } else if (&view == noRepeatBtn_) {
380 imageAnimator_->Stop();
381 imageAnimator_->SetRepeat(false);
382 imageAnimator_->Start();
383 } else if (&view == reverseOrderBtn_) {
384 imageAnimator_->Stop();
385 imageAnimator_->SetReverse(true);
386 imageAnimator_->Start();
387 } else if (&view == positiveOrderBtn_) {
388 imageAnimator_->Stop();
389 imageAnimator_->SetReverse(false);
390 imageAnimator_->Start();
391 } else if (&view == listenerBtn_) {
392 imageAnimator_->SetAnimatorStopListener(listener_);
393 } else if (&view == repeatTimesBtn_) {
394 imageAnimator_->Stop();
395 imageAnimator_->SetRepeatTimes(imageAnimator_->GetRepeatTimes() + 1);
396 imageAnimator_->Start();
397 } else if (&view == fillModeTrueForwardBtn_) {
398 imageAnimator_->Stop();
399 imageAnimator_->SetFillMode(true);
400 imageAnimator_->SetRepeat(false);
401 imageAnimator_->SetReverse(false);
402 imageAnimator_->Start();
403 } else if (&view == fillModeTrueBackwardBtn_) {
404 imageAnimator_->Stop();
405 imageAnimator_->SetFillMode(true);
406 imageAnimator_->SetRepeat(false);
407 imageAnimator_->SetReverse(true);
408 imageAnimator_->Start();
409 } else if (&view == fillModeFalseForwardBtn_) {
410 imageAnimator_->Stop();
411 imageAnimator_->SetFillMode(false);
412 imageAnimator_->SetRepeat(false);
413 imageAnimator_->SetReverse(false);
414 imageAnimator_->Start();
415 } else if (&view == fillModeFalseBackwardBtn_) {
416 imageAnimator_->Stop();
417 imageAnimator_->SetFillMode(false);
418 imageAnimator_->SetRepeat(false);
419 imageAnimator_->SetReverse(true);
420 imageAnimator_->Start();
421 } else if (&view == setImageInfoBtn_) {
422 imageAnimator_->Stop();
423 imageAnimator_->SetImageAnimatorSrc(imageAnimatorImageInfo_, 3); // 3: the number of images
424 imageAnimator_->Start();
425 }
426 return true;
427 }
428 } // namespace OHOS
429