# FloatingView **Repository Path**: chinasoft2_ohos/FloatingView ## Basic Information - **Project Name**: FloatingView - **Description**: 可以让Component在别的Component上执行的漂浮动画。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2021-05-17 - **Last Updated**: 2021-09-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FloatingView #### 项目介绍 - 项目名称:FloatingView - 所属系列:openharmony的第三方组件适配移植 - 功能:可以让Component在别的Component上执行的漂浮动画。 - 项目移植状态:主功能完成 - 调用差异:有,详见调用差异说明 - 开发版本:sdk6,DevEco Studio 2.2 Beta1 - 基线版本:Release v1.0.2 #### 效果演示 #### 安装教程 1.在项目根目录下的build.gradle文件中, ```java allprojects { repositories { maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' } } } ``` 2.在entry模块的build.gradle文件中, ```java dependencies { implementation('com.gitee.chinasoft_ohos:FloatingViewLib:1.0.0') ...... } ``` 在sdk6,DevEco Studio 2.2 Beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下 #### 使用说明 初始化 ```java FloatingElement builder = new FloatingBuilder() .anchorView(Component) .targetView(Component) .offsetX(int) .offsetY(int) .floatingTransition(FloatingTransition) .build(); ``` 创建一个 `Floating` 作为 `FloatingElement` 的容器,然后让你的 `Component` 飞起来 ```java Floating floating = new Floating(AbilitySlice ability, Component topComponent); floating.startFloating(builder); ``` 实现漂浮动画很简单,你只需要实现 `FloatingTransition` 接口就可以 ```java public interface FloatingTransition { void applyFloating(YumFloating yumFloating); } ``` 在 `applyFloating` 方法,你可以使用 `Animation` 创建动画,然后使用 `YumFloating` 进行 `Alpha`,`Scale`,`Translate`,`Rotate` 等变换 如果你想加入 Facebook Rebound 回弹动画效果,你可以使用 SpringHelper,例如 ScaleFloatingTransition: ```java public class ScaleFloatingTransition implements FloatingTransition { ... @Override public void applyFloating(final YumFloating yumFloating) { AnimatorValue alphaAnimator = new AnimatorValue(); alphaAnimator.setDuration(mDuration); alphaAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() { @Override public void onUpdate(AnimatorValue animatorValue, float v) { yumFloating.setAlpha(1 - v); } }); alphaAnimator.start(); SpringHelper.createWithBouncinessAndSpeed(0.0f, 1.0f, mBounciness, mSpeed) .reboundListener(new SimpleReboundListener() { @Override public void onReboundUpdate(double currentValue) { yumFloating.setScaleX((float) currentValue); yumFloating.setScaleY((float) currentValue); } }).start(yumFloating); } } ``` 如果 `SpringHelper` 无法满足你的需求,你可以直接使用 `YumFloating `的 `createSpringByBouncinessAndSpeed(double bounciness, double speed)` 或者 `createSpringByTensionAndFriction(double tension, double friction)` 创建 `Spring`, 然后使用 `transition(double progress, float startValue, float endValue)` 进行数值转换 路径漂浮动画 实现路径漂浮同样很简单,例如 `CurveFloatingPathTransition` ,首先你需要继承 `BaseFloatingPathTransition` 类.和继承 `FloatingTransition` 类不同的是,你需要再实现一个 `getFloatingPath()` 方法. 在 `getFloatingPath()` 方法内使用 Path 创建你想漂浮的路径,然后调用 `FloatingPath.create(path, false)` 进行返回. 例如 `CurveFloatingPathTransition` 实现: ```java public class CurveFloatingPathTransition extends BaseFloatingPathTransition { ... @Override public FloatingPath getFloatingPath() { if (mPath == null) { mPath = new Path(); mPath.moveTo(0, 0); mPath.quadTo(-100, -200, 0, -300); mPath.quadTo(200, -400, 0, -500); } return FloatingPath.create(mPath, false); } @Override public void applyFloating(final YumFloating yumFloating) { AnimatorValue translateAnimator; AnimatorValue alphaAnimator; // 需要指定算法 translateAnimator = ObjectAnimator.ofFloat(getStartPathPosition(), getEndPathPosition()); translateAnimator = new AnimatorValue(); translateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() { @Override public void onUpdate(AnimatorValue animatorValue, float v) { PathPosition floatingPosition = getFloatingPosition(v * getEndPathPosition()); yumFloating.setTranslationX(floatingPosition.x); yumFloating.setTranslationY(floatingPosition.y); } }); } ... } ``` 使用 `Path`将你想要漂浮的路径的描绘出来,然后在 `applyFloating(final YumFloating yumFloating)` 方法中: 使用 `getStartPathPosition()` 方法获取路径的开始位置 使用 `getEndPathPosition()`方法获取路径的结束位置 使用 `getFloatingPosition(float progress)` 获取当前进度的位置 `getFloatingPosition(float progress)` 方法会返回一个 `PathPosition` 对象,其属性 `x`,`y` 分别代表当前路径动画的` x `坐标,和 `y `坐标. 3.接口说明: ```java /** * 锚点,也就是你想在哪个 Component 上面进行漂浮动画 */ FloatingBuilder anchorView(Component view) /** * 目标,你想漂浮的 Component */ FloatingBuilder targetView(Component view) /** * x 方向的偏移量,单位 px */ FloatingBuilder offsetX(int offsetX) /** * y 方向的偏移量,单位 px */ FloatingBuilder offsetY(int offsetY) /** * 漂浮效果,默认是 ScaleFloatingTransition,也可以自己实现漂浮效果 */ FloatingBuilder floatingTransition(FloatingTransition floatingTransition) ``` 4.调用差异说明 ```java 由于openharmony无对应API Window.ID_CONTENT所以需要传递当前的顶层Component在其上面进行操作 所以在初始化Floating的时候需要传递当前的Component ``` #### 测试信息 CodeCheck代码测试无异常 CloudTest代码测试无异常 病毒安全检测通过 当前版本demo功能与原组件基本无差异 #### 版本迭代 - 1.0.0 ## 版权和许可信息 Copyright 2015 UFreedom Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.