1.在项目根目录下的build.gradle文件中,
allprojects {
repositories {
maven {
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
}
}
}
2.在entry模块的build.gradle文件中,
dependencies {
implementation('com.gitee.chinasoft_ohos:FloatingViewLib:1.0.0')
......
}
在sdk6,DevEco Studio 2.2 Beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下
初始化
FloatingElement builder = new FloatingBuilder()
.anchorView(Component)
.targetView(Component)
.offsetX(int)
.offsetY(int)
.floatingTransition(FloatingTransition)
.build();
创建一个 Floating
作为 FloatingElement
的容器,然后让你的 Component
飞起来
Floating floating = new Floating(AbilitySlice ability, Component topComponent);
floating.startFloating(builder);
实现漂浮动画很简单,你只需要实现 FloatingTransition
接口就可以
public interface FloatingTransition {
void applyFloating(YumFloating yumFloating);
}
在 applyFloating
方法,你可以使用 Animation
创建动画,然后使用 YumFloating
进行 Alpha
,Scale
,Translate
,Rotate
等变换
如果你想加入 Facebook Rebound 回弹动画效果,你可以使用 SpringHelper,例如 ScaleFloatingTransition:
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
实现:
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.接口说明:
/**
* 锚点,也就是你想在哪个 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.调用差异说明
由于openharmony无对应API Window.ID_CONTENT所以需要传递当前的顶层Component在其上面进行操作
所以在初始化Floating的时候需要传递当前的Component
CodeCheck代码测试无异常
CloudTest代码测试无异常
病毒安全检测通过
当前版本demo功能与原组件基本无差异
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。