diff --git a/animation/ValueAnimator.md b/animation/ValueAnimator.md index 6eca56998bd731dd744f38f0340fb47425cbcac1..c511592563cd4e42d6ca7e06aa4e3ac1deba0371 100644 --- a/animation/ValueAnimator.md +++ b/animation/ValueAnimator.md @@ -7,6 +7,7 @@ **对于ValueAnimator ofFloat(float... values)处理** **原三方库:** + ```java final ValueAnimator valueAnimator = ValueAnimator.ofFloat(values); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @@ -461,4 +462,14 @@ public class MainAbility extends Ability { } } -``` \ No newline at end of file +``` + + + +### **addUpdateListener(AnimatorUpdateListener listener)** + +> * openharmony API: ohos.agp.animation.AnimatorValue.setValueUpdateListener +> * openharmony SDK版本:2.1.1.21 +> * IDE版本:2.1.0.501 +> * 实现方案:直接替换即可 + diff --git a/graphics/Bitmap.md b/graphics/Bitmap.md index 55b86311601e8d3e6d5cc86756b27394c32e23c4..b2699f5293322c5cc4f580af1d012081390ddaff 100644 --- a/graphics/Bitmap.md +++ b/graphics/Bitmap.md @@ -87,4 +87,58 @@ int height = pixelMap.getImageInfo().size.height; >+ openharmony API: ohos.media.image.PixelMap.release() >+ openharmony SDK版本:2.1.0.17 >+ IDE版本:2.1.0.141 ->+ 实现方案:替换使用 \ No newline at end of file +>+ 实现方案:替换使用 + +### **createBitmap(int width, int height, @NonNull Config config)** +>+ openharmony API: 无 +> +>+ openharmony SDK版本:2.1.0.17以上 +> +>+ IDE版本:2.1.0.501 +> +>+ 实现方案:参考如下代码进行实现 +> +> ```java +> public static PixelMap createBitmap(int width, int height, PixelFormat pixelFormat) { +> PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions(); +> initializationOptions.size = new Size(width, height); +> initializationOptions.pixelFormat = pixelFormat; +> initializationOptions.editable = true; +> return PixelMap.create(initializationOptions); +> } +> ``` + + + +### **boolean compress(CompressFormat format, int quality, OutputStream stream)** + +>+ openharmony API: 无 +> +>+ openharmony SDK版本:2.1.0.17以上 +> +>+ IDE版本:2.1.0.501 +> +>+ 实现方案:参考如下代码进行实现 +> +>```java +>public static boolean compress(PixelMap pixelMap, String compressFormat, int quality, OutputStream outputStream) { +> if (pixelMap == null) { +> LogUtils.e(HI_LOG_LABEL, "pixelMap is empty"); +> return false; +> } +> ImagePacker imagePacker = ImagePacker.create(); +> ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions(); +> packingOptions.format = compressFormat; +> packingOptions.quality = quality; +> boolean result = imagePacker.initializePacking(outputStream, packingOptions); +> if (result) { +> imagePacker.addImage(pixelMap); +> imagePacker.finalizePacking(); +> return true; +> } else { +> LogUtils.e(HI_LOG_LABEL, "imagePacker failed to initial"); +> return false; +> } +>} +>``` + diff --git a/graphics/BitmapFactory.md b/graphics/BitmapFactory.md index 2c6dbf9d33bfcbf5279a2dddc5066e455a94d508..90e3b143543886c51673daf1d28e41998f85a0f9 100644 --- a/graphics/BitmapFactory.md +++ b/graphics/BitmapFactory.md @@ -51,4 +51,29 @@ public static PixelMap decodeStream(FileDescriptor fd) { ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); return source.createPixelmap(decodingOptions);//decodingOptions可为null } -``` \ No newline at end of file +``` + + + +### **decodeFile(String pathName)** + +>+ openharmony API: `ohos.media.image.ImageSource` +>+ openharmony SDK版本:2.1.0.17以上 +>+ IDE版本:2.1.0.501 +>+ 实现方案:自定义 + +``` java +public static PixelMap decodeFile(String inputFile) { + if (TextUtils.isEmpty(inputFile)) { + return null; + } + File file = new File(inputFile); + ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); + sourceOptions.formatHint = "image/jpeg"; + ImageSource imageSource = ImageSource.create(file, sourceOptions); + PixelMap pixelMap = getPixelMapFromImageSource(imageSource); + imageSource.release(); + return pixelMap; +} +``` + diff --git a/os/AsyncTask.md b/os/AsyncTask.md new file mode 100644 index 0000000000000000000000000000000000000000..b7d8fb65b6023e53dc7aa0da479467f6eb2a463f --- /dev/null +++ b/os/AsyncTask.md @@ -0,0 +1,24 @@ +### **AsyncTask** + +> + openharmony API: ohos.app.dispatcher.TaskDispatcher +> + openharmony SDK版本:2.1.0.17以上 +> + IDE版本:2.1.0.501 +> + 实现方案:在TaskDispatcher中执行异步任务,并通过EventHandler传递任务结果。可参考如下的示例代码 + +```java +public void setImageResource(int resourceId) { + Context context = getContext(); + SvgEventHanlder svgEventHanlder = new SvgEventHanlder(EventRunner.current()); + TaskDispatcher globalTaskDispatcher = getContext().getGlobalTaskDispatcher(TaskPriority.HIGH); + globalTaskDispatcher.asyncDispatch(() -> { + try { + svgg = SVG.getFromResource(context, resourceId); + } catch (SVGParseException | IOException | NotExistException e) { + HiLog.error(LOG_LABEL, e.getMessage()); + } + + InnerEvent event = InnerEvent.get(SvgEventHanlder.EVENT_ID_DO_RENDER, 0, svgg); + svgEventHanlder.sendEvent(event, 0, EventHandler.Priority.IMMEDIATE); + }); +} +``` \ No newline at end of file diff --git a/view/View.md b/view/View.md index 48127a92a4768d7ee2339fc03462f1cc8ff600ca..86e424b4d604aa509c5c3bd5151f17398a5a7aa3 100644 --- a/view/View.md +++ b/view/View.md @@ -3,12 +3,12 @@ >+ openharmony SDK版本:2.1.0.17 以上 >+ IDE版本:2.1.0.141 >+ 实现方案:实现接口 implements Component.EstimateSizeListener,设置监听 setEstimateSizeListener(this) - + @Override public boolean onEstimateSize(int widthEstimatedConfig, int heightEstimatedConfig) { return true; } - + >+ 补充说明:onEstimateSize在sdk 2.1.0.17以前未实现,临时方案可以通过在构造函数中调用setLayoutRefreshedListener或者addDrawTask中去获取宽高信息 @@ -18,15 +18,15 @@ >+ openharmony SDK版本:2.1.0.17 以上 >+ IDE版本:2.1.0.141 >+ 实现方案:实现接口 implements ComponentContainer.ArrangeListener,设置监听 setArrangeListener(this) - + @Override public boolean onArrange(int l, int t, int r, int b) { return true; } - + >+ 补充说明:onArrange在sdk 2.1.0.17以前未实现,临时方案为,如果不涉及到View.layout()操作可以直接将逻辑放到onDraw里面,否则可以通过component.setComponentPosition()对子控件重新布局; - - + + ### **onDraw** >+ openharmony API: ohos.agp.components.Component.addDrawTask >+ openharmony SDK版本:2.1.0.17 @@ -69,4 +69,19 @@ >+ openharmony API: ohos.agp.components.Component.invalidate >+ openharmony SDK版本:2.1.0.17 >+ IDE版本:2.1.0.141 ->+ 实现方案:在onDraw()方法中调用直接invalidate()方法无效,需在asyncDispatch方法中调用,如下:getContext().getUITaskDispatcher().asyncDispatch(this::invalidate)。 \ No newline at end of file +>+ 实现方案:在onDraw()方法中调用直接invalidate()方法无效,需在asyncDispatch方法中调用,如下:getContext().getUITaskDispatcher().asyncDispatch(this::invalidate)。 + +### **setTag(int key, final Object tag)** + +>+ openharmony API: +ohos.agp.components.Component.setTag +>+ openharmony SDK版本:2.1.0.13以上 +>+ IDE版本:2.1.0.501 +>+ 实现方案:openharmony的Component.setTag方法只支持一个参数,Android支持两个参数,在getTag的时候通过key取出对应的参数,因此在openharmony上可以改用存储HashMap来记录,取值的时候先获取存入的HashMap再通过key获取对应的记录, +如下: +```java +Map tag = new HashMap<>(); +tag.put(1, "testTag"); +image.setTag(tag); +``` + diff --git a/widget/ImageView.md b/widget/ImageView.md index ac6cb10c39b43cd113d1df8fc543a28d13913523..26237c1a2642b513ab1e0b9b911b909001b44463 100644 --- a/widget/ImageView.md +++ b/widget/ImageView.md @@ -14,4 +14,60 @@ public static void setImageTint(Element element, int intColor, BlendMode mode, I image.setImageElement(element); } +``` + + + +### **setImageDrawable(Drawable drawable)** + +> * openharmony API: 暂无 +> * openharmony SDK版本:2.1.1.21 +> * IDE版本:2.1.0.501 +> * 实现方案:目前可行的一种实现方案是继承Component并实现Component.DrawTask接口,在自定义控件中将传入的图片通过Canvas进行绘制。参考代码如下: + +```java +public class SVGImageView extends Component implements Component.DrawTask { + + private Picture mPictureToDraw = new Picture(); + + ... + + //该示例是对svg进行绘制,可自行替换为其它方式 + public void setSVG(SVG svg) { + if (svg == null) { + throw new IllegalArgumentException(" Null value passed to setSVG()"); + } + this.svg = svg; + doRender(); + } + + //将svg转为Picture + private void doRender() { + if (svg == null) { + return; + } + mPictureToDraw.beginRecording(mPictureToDraw.getWidth(), mPictureToDraw.getHeight()); + mPictureToDraw = svg.renderToPicture(mRenderOptions); + mPictureToDraw.endRecording(); + addDrawTask(this); + } + + //最后将得到的Picture通过Canvas进行绘制 + @Override + public void onDraw(Component component, Canvas canvas) { + float widthHeightRatio = (float) mPictureToDraw.getWidth() / (float) mPictureToDraw.getHeight(); + float thisWidth = this.getWidth(); + float thisHeight = this.getHeight(); + float tarWidth; + float tarHeight; + if (thisWidth <= thisHeight) { + tarHeight = Math.min((thisWidth / widthHeightRatio), thisHeight); + tarWidth = ((thisWidth / widthHeightRatio) > thisHeight) ? (thisHeight * widthHeightRatio) : thisWidth; + } else { + tarWidth = (Math.min(thisHeight * widthHeightRatio, thisWidth)); + tarHeight = ((thisHeight * widthHeightRatio > thisWidth) ? (thisWidth / widthHeightRatio) : thisHeight); + } + canvas.drawPicture(mPictureToDraw, new RectFloat(0, 0, tarWidth, tarHeight)); + } +} ``` \ No newline at end of file