在鸿蒙中如何实现一屏多页

众所周知,pageslider 是用于页面之间切换的组件,它通过响应滑动事件完成页面间的切换,而 pageflipper 可能知道的人就比较少了。
其实 pageflipper 和 pageslider 类似,都是视图切换组件,它们都继承自 stacklayout,因此可以将多个 component 层叠在一起,每次只显示一个组件。
当视图从一个 component 切换到另一个 component 时,pageflipper 支持指定动画效果。
区别:
pageflipper 通过 addcomponent() 添加 component,可使用动画控制多个 component 之间的切换效果,是个轻量级的组件,适合展示少量静态数据。
而 pageslide 是由 provider 来提供 component 的,更适用复杂的视图切换,实现数据的动态加载。
下面是一个 pageslider 和 pageflipper 结合起来的使用效果,页面中间的卡片使用的是 pageslider,背景图片和底部的数字指示器用的是 pageflipper。
pageslider
pageslider 可以说是鸿蒙中最常用的视图切换组件了,使用方法不用多做介绍,官方文档有详细的说明,这里主要说一下一个特殊的效果。
①一屏多页效果
其实鸿蒙本身有提供一个 setclipenabled() 的方法,作用是设置是否允许在组件超出其父布局时自动裁剪组件。
理论上通过给 pageslider 父布局设置 setclipenabled(false),加上给子组件设置合适的宽度可以实现一屏多页效果,但是经过测试并没达到效果。
这个方法我也单独拿出来在其他场景验证过确实无效,下面是验证的效果。
但是鸿蒙却提供了另外一个方法 setpagemargin(),它的作用是设置 pageslider 中子组件边距的,当传入一个合适的负数时(必须是负数),就能实现一屏同时显示多个子组件的效果:
②动态设置缩放透明度变化
设置透明度和缩放比例就不细说了,主要就是在 pageslider 子组件加载完成后和页面切换中的回调方法中改变 alpha 值和 scale 值。
直接上代码:
public final class alphascalepagetransformer {
/**
* 缩放
*/
public static final float inactive_scale = 0.8f;
/**
* 透明度
*/
public static final float inactive_alpha = 0.5f;
/**
* 设置初始状态的缩放和透明度
*
* @param child
* @param position
* @param current
*/
public static void defaultpage(listcontainer child, int position, float current) {
if (position != current) {
child.setalpha(inactive_alpha);
child.setscalex(inactive_scale);
child.setscaley(inactive_scale);
}
}
/**
* 设置滑动中的缩放和透明度
*
* @param childlist
* @param position
* @param offset
* @param direction
*/
public static void transformpage(list《listcontainer》 childlist, int position, float offset, float direction) {
component child = childlist.get(position);
float scale = inactive_scale + (1 - inactive_scale) * (1 - math.abs(offset));
float alpha = inactive_alpha + (1 - inactive_alpha) * (1 - math.abs(offset));
child.setscalex(scale);
child.setscaley(scale);
child.setalpha(alpha);
if (direction 》 0) {
if (position 《 childlist.size() - 1) {
child = childlist.get(position + 1);
}
} else {
if (position 》= 1) {
child = childlist.get(position - 1);
}
}
scale = inactive_scale + (1 - inactive_scale) * math.abs(offset);
alpha = inactive_alpha + (1 - inactive_alpha) * math.abs(offset);
child.setscalex(scale);
child.setscaley(scale);
child.setalpha(alpha);
}
}
设置两边的 component 透明度和缩放效果:
//设置初始状态缩放和透明度
alphascalepagetransformer.defaultpage(image, i, pageslider.getcurrentpage());
//设置页面切换中缩放和透明度
pageslider.addpagechangedlistener(new pagechangedlistener() {
@override
public void onpagesliding(int position, float positionoffset, int positionoffsetpixels) {
alphascalepagetransformer.transformpage(listcontainers, position,
positionoffset, positionoffsetpixels);
}
});
pageflipper(翻页器)
pageflipper 是一个翻页器,当它有两个或多个子组件时,切换过程中可以轻松设置入场动画和出场动画,以达到意想不到的效果。
虽然 pageflipper 的使用率远不及 pageslider,但这并不意味着 pageflipper 就不强大。
他能通过简单的代码实现许多动画效果,比如淘宝头条的效果,日历翻页效果,背景图淡入淡出效果等等。
常用方法:
getcurrentcomponent()//获取当前组件
shownext():显示下一个组件(如果当前子组件是最后一个,则显示第一个子组件)
showprevious():显示上一个组件(如果当前子组件是第一个,则显示最后一个子组件)
getflipinterval() :获取自动翻转时间
setflipperiod(int period) :设置翻转周期
startflipping() :开启自动翻转
stopflipping() :停止自动翻转
addcomponent() :添加组件
setincominganimationa() :设置转入动画
setoutgoinganimation() :设置转出动画
下面通过设置文字翻页效果来了解下它的使用方法:
代码如下:
public class indicatorcomponent extends directionallayout {
/**
* 文字大小
*/
private static final int text_size = 130;
/**
* 动画时长
*/
private static final int duration = 600;
private pageflipper textswitcher;
private text textcomponent;
/**
* itemscountcomponent
*
* @param context
* @param attrset
*/
public indicatorcomponent(context context, attrset attrset) {
super(context, attrset);
init(context);
}
private void init(context context) {
setorientation(componentcontainer.horizontal);
textswitcher = new pageflipper(context);
//理论上pageflipper只需要添加两个子component就能实现动画效果,但是实际测试发现如果切换速度太快就导致子组件衔接不上出现组件消失的额情况,
//因此这里通过实践多添加了几个子component,防止滑动过快出现bug
textswitcher.addcomponent(createcomponentfortextswitcher(context));
textswitcher.addcomponent(createcomponentfortextswitcher(context));
textswitcher.addcomponent(createcomponentfortextswitcher(context));
textswitcher.addcomponent(createcomponentfortextswitcher(context));
addcomponent(textswitcher, new layoutconfig(componentcontainer.layoutconfig.match_content,
componentcontainer.layoutconfig.match_content));
textcomponent = new text(context);
textcomponent.settextsize(text_size);
textcomponent.setfont(font.default_bold);
textcomponent.settextcolor(new color(color.getintcolor(“#8cffffff”)));
addcomponent(textcomponent, new layoutconfig(componentcontainer.layoutconfig.match_content,
componentcontainer.layoutconfig.match_content));
}
/**
* 创建组件
*
* @param context 上下文
* @return text
*/
private text createcomponentfortextswitcher(context context) {
text text = new text(context);
text.settextsize(text_size);
text.setfont(font.default_bold);
text.settextcolor(color.white);
text.setlayoutconfig(new pageflipper.layoutconfig(componentcontainer.layoutconfig.match_content,
pageflipper.layoutconfig.match_content));
return text;
}
/**
* update
*
* @param newposition 新位置
* @param oldposition 旧位置
* @param totalelements 总数
*/
public void update(int newposition, int oldposition, int totalelements) {
textcomponent.settext(“ / ” + totalelements);
int offset = textswitcher.getheight();
if (newposition 》 oldposition) {
//设置组件进入和退出的动画
textswitcher.setincominganimation(createpositionanimation(-offset, 0, 0f, 1f, duration));
textswitcher.setoutgoinganimation(createpositionanimation(0, offset, 1f, 0f, duration));
} else if (oldposition 》 newposition) {
textswitcher.setincominganimation(createpositionanimation(offset, 0, 0f, 1f, duration));
textswitcher.setoutgoinganimation(createpositionanimation(0, -offset, 1f, 0f, duration));
}
//显示下一个组件并执行动画
textswitcher.shownext();
text text = (text) textswitcher.getcurrentcomponent();
text.settext(string.valueof(newposition + 1));
}
/**
* 创建属性动画
*
* @param fromy
* @param toy
* @param fromalpha
* @param toalpha
* @param duration
* @return
*/
private animatorproperty createpositionanimation(int fromy, int toy, float fromalpha, float toalpha, int duration) {
animatorproperty animatorproperty = new animatorproperty();
animatorproperty.setcurvetype(animator.curvetype.decelerate);
animatorproperty.alphafrom(fromalpha);
animatorproperty.alpha(toalpha);
animatorproperty.movefromy(fromy);
animatorproperty.movetoy(toy);
animatorproperty.setduration(duration);
return animatorproperty;
}
}
结束
以上主要介绍了 pageslider 和 pageflipper 的一些简单使用,最后补充一个小功能,设置渐变效果,这个简单的效果可能很多人还不知道如何设置。
首先生成一个 foreground_gradient.xml:
《shape
xmlns:ohos=“http://schemas.huawei.com/res/ohos”
ohos:shape=“rectangle”》
//设置填充的颜色,可以根据实际需要设置多个
《solid
ohos:colors=“#000000,#00ffffff,#d8000000”/》
//设置渐变方向,有三个值可供选择:linear_gradient,radial_gradient,sweep_gradient
《gradient
ohos:shader_type=“linear_gradient”
/》《/shape》
然后给目标组件设置前景色即可:
ohos:foreground_element=“$graphic:foreground_gradient”


新唐科技M451VE6AE控制器简介
汽车芯片短缺什么原因
粤芯半导体12英寸集成电路生产线将在年内投产
宽禁带半导体前景乐观
一文浅谈电能管理系统
在鸿蒙中如何实现一屏多页
北斗星通变更部分高精度芯片募投项目募集资金用途的原因
支持无人机快速发展的7大核心技术
口罩模拟穿戴试验机的技术参数是怎样的
机器人Iroi和Patrovor
奥地利微电子获誉台湾光电“最佳供应商”
双脉冲测试的原理及测试技巧
笔记本的铁布衫
Wi-Fi运动检测逐渐成熟 并逐渐向其他领域扩展
深度解读:华为与360特供机彻底决裂
光和电的碰撞,硅光子技术如何引领创新?
Vishay 新款厚膜片式电阻可在减少系统元件数量的同时,提高测量精度并节省空间
对放大器补偿的实际考虑
降压型μModule稳压器LTM8053
R&S CMW WLAN和蓝牙芯片测试平台通过博通认证