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 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file ui_image_view.h 28 * 29 * @brief Declares an image view. 30 * 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_UI_IMAGE_VIEW_H 36 #define GRAPHIC_LITE_UI_IMAGE_VIEW_H 37 38 #include "common/image.h" 39 #include "components/ui_view.h" 40 #include "gfx_utils/graphic_types.h" 41 #if defined(ENABLE_GIF) && (ENABLE_GIF == 1) 42 #include "animator/animator.h" 43 #endif 44 45 namespace OHOS { 46 /** 47 * @brief Defines the functions related to an image view. 48 * 49 * @since 1.0 50 * @version 1.0 51 */ 52 class UIImageView : public UIView { 53 public: 54 /** 55 * @brief A default constructor used to create a <b>UIImageView</b> instance. 56 * 57 * @since 1.0 58 * @version 1.0 59 */ 60 UIImageView(); 61 62 /** 63 * @brief A destructor used to delete the <b>UIImageView</b> instance. 64 * 65 * @since 1.0 66 * @version 1.0 67 */ 68 virtual ~UIImageView(); 69 70 /** 71 * @brief Obtains the view type. 72 * 73 * @return Returns <b>UI_IMAGE_VIEW</b>, as defined in {@link UIViewType}. 74 * @since 1.0 75 * @version 1.0 76 */ GetViewType()77 UIViewType GetViewType() const override 78 { 79 return UI_IMAGE_VIEW; 80 } 81 82 /** 83 * @brief Obtains the width of this image view. 84 * 85 * @return Returns the width of this image view. 86 * @since 1.0 87 * @version 1.0 88 */ GetWidth()89 int16_t GetWidth() override 90 { 91 if (needRefresh_ && autoEnable_) { 92 ReMeasure(); 93 } 94 return UIView::GetWidth(); 95 } 96 97 /** 98 * @brief Obtains the height of this image view. 99 * 100 * @return Returns the height of this image view. 101 * @since 1.0 102 * @version 1.0 103 */ GetHeight()104 int16_t GetHeight() override 105 { 106 if (needRefresh_ && autoEnable_) { 107 ReMeasure(); 108 } 109 return UIView::GetHeight(); 110 } 111 112 /** 113 * @brief Checks whether this image view needs to be covered to optimize the drawing process. 114 * 115 * @param invalidatedArea Indicates the area to draw. 116 * @return Returns <b>true</b> if this image view needs to be covered; returns <b> false</b> otherwise. 117 * @since 1.0 118 * @version 1.0 119 */ 120 bool OnPreDraw(Rect& invalidatedArea) const override; 121 122 /** 123 * @brief Draws this image view. 124 * 125 * @param invalidatedArea Indicates the area to draw. 126 * @since 1.0 127 * @version 1.0 128 */ 129 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 130 131 /** 132 * @brief Sets the image path. 133 * 134 * @param src Indicates the pointer to the image path represented by a string. 135 * @since 1.0 136 * @version 1.0 137 */ 138 virtual void SetSrc(const char* src); 139 140 /** 141 * @brief Sets the image information. 142 * 143 * @param src Indicates the pointer to the image information. For details, see {@link ImageInfo}. 144 * @since 1.0 145 * @version 1.0 146 */ 147 virtual void SetSrc(const ImageInfo* src); 148 149 /** 150 * @brief Sets whether the image view size needs to be adaptive to the image size. 151 * 152 * @param enable Specifies whether the image view size needs to be adaptive to the image size. 153 * <b>true</b> indicates that automatic adaption is enabled, and <b> false</b> indicates the opposite case. 154 * @since 1.0 155 * @version 1.0 156 */ SetAutoEnable(bool enable)157 void SetAutoEnable(bool enable) 158 { 159 if (autoEnable_ != enable) { 160 needRefresh_ = autoEnable_ ? needRefresh_ : true; 161 autoEnable_ = enable; 162 UpdateDrawTransMap(true); 163 } 164 } 165 166 /* 167 * @brief Checks whether automatic adaptation is enabled. 168 * 169 * @return Returns <b>true</b> if automatic adaptation is enabled; returns <b> false</b> otherwise. 170 * @since 1.0 171 * @version 1.0 172 */ GetAutoEnable()173 bool GetAutoEnable() const 174 { 175 return autoEnable_; 176 } 177 178 /** 179 * @brief Sets the blur level for this image when it is rotated or scaled. 180 * 181 * @param level Indicates the blur level to set. For details, see {@link BlurLevel}. 182 * @since 1.0 183 * @version 1.0 184 */ SetBlurLevel(BlurLevel level)185 void SetBlurLevel(BlurLevel level) 186 { 187 blurLevel_ = level; 188 } 189 190 /** 191 * @brief Obtains the blur level of this image when it is rotated or scaled. 192 * 193 * @return Returns the blur level of this image, as defined in {@link BlurLevel}. 194 * @since 1.0 195 * @version 1.0 196 */ GetBlurLevel()197 BlurLevel GetBlurLevel() const 198 { 199 return static_cast<BlurLevel>(blurLevel_); 200 } 201 202 /** 203 * @brief Sets the algorithm used for image rotation and scaling. 204 * 205 * @param algorithm Indicates the image transformation algorithm. For details, see {@link TransformAlgorithm}. 206 * @since 1.0 207 * @version 1.0 208 */ SetTransformAlgorithm(TransformAlgorithm algorithm)209 void SetTransformAlgorithm(TransformAlgorithm algorithm) 210 { 211 algorithm_ = algorithm; 212 } 213 214 /** 215 * @brief Obtains the algorithm used for image rotation and scaling. 216 * 217 * @return Returns the image transform algorithm, as defined in {@link TransformAlgorithm}. 218 * @since 1.0 219 * @version 1.0 220 */ GetTransformAlgorithm()221 TransformAlgorithm GetTransformAlgorithm() const 222 { 223 return static_cast<TransformAlgorithm>(algorithm_); 224 } 225 226 /** 227 * @brief Obtains the image path in binary. 228 * 229 * @return Returns the pointer to the image path. 230 * @since 1.0 231 * @version 1.0 232 */ GetPath()233 const char* GetPath() const 234 { 235 return image_.GetPath(); 236 } 237 238 /** 239 * @brief Obtains the image information in an array. 240 * 241 * @return Returns the pointer to the image information. 242 * @since 1.0 243 * @version 1.0 244 */ GetImageInfo()245 const ImageInfo* GetImageInfo() const 246 { 247 return image_.GetImageInfo(); 248 } 249 250 /** 251 * @brief Obtains the image type. 252 * 253 * @return Returns <b>IMG_SRC_VARIABLE</b> for image information in an array; returns <b>IMG_SRC_FILE</b> for an 254 * image path in binary. 255 * @since 1.0 256 * @version 1.0 257 */ GetSrcType()258 uint8_t GetSrcType() const 259 { 260 return image_.GetSrcType(); 261 } 262 263 enum ImageResizeMode : uint8_t { 264 NONE, 265 COVER, 266 CONTAIN, 267 FILL, 268 CENTER, 269 SCALE_DOWN, 270 }; 271 void SetResizeMode(ImageResizeMode mode); 272 void SetWidth(int16_t width) override; 273 void SetHeight(int16_t height) override; 274 275 /** 276 * @brief Obtains the ImageResizeMode. 277 * 278 * @return Returns ImageResizeMode. 279 */ GetResizeMode()280 ImageResizeMode GetResizeMode() const 281 { 282 return imageResizeMode_; 283 } 284 #if defined(ENABLE_GIF) && (ENABLE_GIF == 1) GetGifImageAnimator()285 Animator* GetGifImageAnimator() const 286 { 287 return gifImageAnimator_; 288 } 289 #endif 290 protected: 291 /** 292 * @brief Represents the width of this image. 293 */ 294 int16_t imageWidth_; 295 /** 296 * @brief Represents the height of this image. 297 */ 298 int16_t imageHeight_; 299 /** 300 * @brief Specifies whether automatic adaptation is enabled. 301 */ 302 bool autoEnable_; 303 /** 304 * @brief Specifies whether a refresh is needed. 305 */ 306 bool needRefresh_; 307 /** 308 * @brief Represents the color format of this image. 309 */ 310 uint8_t colorFormat_; 311 /** 312 * @brief Represents the blur level of this image when it is rotated or scaled. 313 */ 314 uint8_t blurLevel_ : 2; 315 /** 316 * @brief Represents the algorithm used for image rotation and scaling. 317 */ 318 uint8_t algorithm_ : 1; 319 uint8_t reserve_ : 1; 320 Image image_; 321 ImageResizeMode imageResizeMode_ = ImageResizeMode::NONE; 322 TransformMap* drawTransMap_ = nullptr; 323 Matrix4<float>* contentMatrix_ = nullptr; 324 bool transMapInvalid_ = true; 325 326 private: 327 void ReMeasure() override; 328 #if defined(ENABLE_GIF) && (ENABLE_GIF == 1) 329 friend class GifImageAnimator; 330 void AddAndStartGifAnimator(); 331 void RemoveAndStopGifAnimator(); 332 Animator* gifImageAnimator_; 333 bool gifFrameFlag_; 334 #endif 335 void UpdateContentMatrix(); 336 void UpdateDrawTransMap(bool updateContentMatrix = false); 337 void AdjustScaleAndTranslate(Vector3<float>& scale, Vector3<int16_t>& translate, 338 int16_t widgetWidth, int16_t widgetHeight) const; 339 void SetCordsTmpRect(BufferInfo& gfxDstBuffer, Rect& viewRect, Rect& trunc, Rect& cordsTmp, OpacityType opa); 340 }; 341 } // namespace OHOS 342 #endif // GRAPHIC_LITE_UI_IMAGE_VIEW_H 343