HarmonyOS基础之PageSlider和PageFlipper

系统 OpenHarmony
PageSlider可以说是鸿蒙中最常用的视图切换组件了,使用方法不用多做介绍,官方文档有详细的说明,这里主要说一下一个特殊的效果。

[[425826]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

众所周知,PageSlider是用于页面之间切换的组件,它通过响应滑动事件完成页面间的切换,而PageFlipper可能知道的人就比较少了,其实PageFlipper和PageSlider类似,都是视图切换组件,它们都继承自StackLayout,因此可以将多个component层叠在一起,每次只显示一个组件,当视图从一个component切换到另一个component时,PageFlipper支持指定动画效果。

区别:PageFlipper通过addComponent()添加component,可使用动画控制多个component之间的切换效果,是个轻量级的组件,适合展示少量静态数据;而PageSlide是由provider来提供component的,更适用复杂的视图切换,实现数据的动态加载。

下面是一个PageSlider和PageFlipper结合起来的使用效果,页面中间的卡片使用的是PageSlider,背景图片和底部的数字指示器用的是PageFlipper,通过回调将三个组件联动起来就实现了这样的效果:

HarmonyOS 基础之PageSlider和PageFlipper-鸿蒙HarmonyOS技术社区

正文

1.pageSlider

PageSlider可以说是鸿蒙中最常用的视图切换组件了,使用方法不用多做介绍,官方文档有详细的说明,这里主要说一下一个特殊的效果。

一屏多页效果

其实鸿蒙本身有提供一个setClipEnabled()的方法,作用是设置是否允许在组件超出其父布局时自动裁剪组件,理论上通过给pageSlider父布局设置setClipEnabled(false),加上给子组件设置合适的宽度可以实现一屏多页效果,但是经过测试并没达到效果,这个方法我也单独拿出来在其他场景验证过确实无效,下面是验证的效果。

HarmonyOS 基础之PageSlider和PageFlipper-鸿蒙HarmonyOS技术社区

但是鸿蒙却提供了另外一个方法setPageMargin(),它的作用是设置PageSlider中子组件边距的,当传入一个合适的负数时(必须是负数),就能实现一屏同时显示多个子组件的效果:

HarmonyOS 基础之PageSlider和PageFlipper-鸿蒙HarmonyOS技术社区

动态设置缩放透明度变化

设置透明度和缩放比例就不细说了,主要就是在PageSlider子组件加载完成后和页面切换中的回调方法中改变alpha值和scale值,直接上代码:

  1. public final class AlphaScalePageTransformer { 
  2.     /** 
  3.      * 缩放 
  4.      */ 
  5.     public static final float INACTIVE_SCALE = 0.8f; 
  6.     /** 
  7.      * 透明度 
  8.      */ 
  9.     public static final float INACTIVE_ALPHA = 0.5f; 
  10.  
  11.     /** 
  12.      * 设置初始状态的缩放和透明度 
  13.      * 
  14.      * @param child 
  15.      * @param position 
  16.      * @param current 
  17.      */ 
  18.     public static void defaultPage(ListContainer child, int position, float current) { 
  19.         if (position != current) { 
  20.             child.setAlpha(INACTIVE_ALPHA); 
  21.             child.setScaleX(INACTIVE_SCALE); 
  22.             child.setScaleY(INACTIVE_SCALE); 
  23.         } 
  24.     } 
  25.  
  26.     /** 
  27.      * 设置滑动中的缩放和透明度 
  28.      * 
  29.      * @param childList 
  30.      * @param position 
  31.      * @param offset 
  32.      * @param direction 
  33.      */ 
  34.     public static void transformPage(List<ListContainer> childList, int position, float offset, float direction) { 
  35.         Component child = childList.get(position); 
  36.         float scale = INACTIVE_SCALE + (1 - INACTIVE_SCALE) * (1 - Math.abs(offset)); 
  37.         float alpha = INACTIVE_ALPHA + (1 - INACTIVE_ALPHA) * (1 - Math.abs(offset)); 
  38.         child.setScaleX(scale); 
  39.         child.setScaleY(scale); 
  40.         child.setAlpha(alpha); 
  41.         if (direction > 0) { 
  42.             if (position < childList.size() - 1) { 
  43.                 child = childList.get(position + 1); 
  44.             } 
  45.         } else { 
  46.             if (position >= 1) { 
  47.                 child = childList.get(position - 1); 
  48.             } 
  49.         } 
  50.         scale = INACTIVE_SCALE + (1 - INACTIVE_SCALE) * Math.abs(offset); 
  51.         alpha = INACTIVE_ALPHA + (1 - INACTIVE_ALPHA) * Math.abs(offset); 
  52.         child.setScaleX(scale); 
  53.         child.setScaleY(scale); 
  54.         child.setAlpha(alpha); 
  55.     } 

 设置两边的component透明度和缩放效果:

  1. //设置初始状态缩放和透明度 
  2. AlphaScalePageTransformer.defaultPage(image, i, pageSlider.getCurrentPage()); 
  3.  
  4. //设置页面切换中缩放和透明度 
  5. pageSlider.addPageChangedListener(new PageChangedListener() { 
  6.             @Override 
  7.             public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) { 
  8.                 AlphaScalePageTransformer.transformPage(listContainers, position,  
  9.                 positionOffset, positionOffsetPixels); 
  10.             } 
  11.         }); 

2.PageFlipper(翻页器)

PageFlipper是一个翻页器,当它有两个或多个子组件时,切换过程中可以轻松设置入场动画和出场动画,以达到意想不到的效果。虽然PageFlipper的使用率远不及PageSlider,但这并不意味着PageFlipper就不强大,他能通过简单的代码实现许多动画效果,比如淘宝头条的效果,日历翻页效果,背景图淡入淡出效果等等。

常用方法:

  1. getCurrentComponent()//获取当前组件 
  2.  
  3. showNext():显示下一个组件(如果当前子组件是最后一个,则显示第一个子组件) 
  4.  
  5. showPrevious():显示上一个组件(如果当前子组件是第一个,则显示最后一个子组件) 
  6.  
  7. getFlipInterval() :获取自动翻转时间 
  8.  
  9. setFlipPeriod(int period) :设置翻转周期 
  10.  
  11. startFlipping() :开启自动翻转 
  12.  
  13. stopFlipping() :停止自动翻转 
  14.  
  15. addComponent() :添加组件 
  16.  
  17. setIncomingAnimationA() :设置转入动画 
  18.  
  19. setOutgoingAnimation() :设置转出动画 

 下面通过设置文字翻页效果来了解下它的使用方法:

HarmonyOS 基础之PageSlider和PageFlipper-鸿蒙HarmonyOS技术社区
  1. public class IndicatorComponent extends DirectionalLayout { 
  2.     /** 
  3.      * 文字大小 
  4.      */ 
  5.     private static final int TEXT_SIZE = 130; 
  6.     /** 
  7.      * 动画时长 
  8.      */ 
  9.     private static final int DURATION = 600; 
  10.     private PageFlipper textSwitcher; 
  11.     private Text textcomponent; 
  12.  
  13.     /** 
  14.      * ItemsCountcomponent 
  15.      * 
  16.      * @param context 
  17.      * @param attrSet 
  18.      */ 
  19.     public IndicatorComponent(Context context, AttrSet attrSet) { 
  20.         super(context, attrSet); 
  21.         init(context); 
  22.     } 
  23.  
  24.     private void init(Context context) { 
  25.         setOrientation(ComponentContainer.HORIZONTAL); 
  26.         textSwitcher = new PageFlipper(context); 
  27.         //理论上PageFlipper只需要添加两个子component就能实现动画效果,但是实际测试发现如果切换速度太快就导致子组件衔接不上出现组件消失的额情况, 
  28.         //因此这里通过实践多添加了几个子component,防止滑动过快出现bug 
  29.         textSwitcher.addComponent(createcomponentForTextSwitcher(context)); 
  30.         textSwitcher.addComponent(createcomponentForTextSwitcher(context)); 
  31.         textSwitcher.addComponent(createcomponentForTextSwitcher(context)); 
  32.         textSwitcher.addComponent(createcomponentForTextSwitcher(context)); 
  33.         addComponent(textSwitcher, new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, 
  34.                 ComponentContainer.LayoutConfig.MATCH_CONTENT)); 
  35.         textcomponent = new Text(context); 
  36.         textcomponent.setTextSize(TEXT_SIZE); 
  37.         textcomponent.setFont(Font.DEFAULT_BOLD); 
  38.         textcomponent.setTextColor(new Color(Color.getIntColor("#8cffffff"))); 
  39.         addComponent(textcomponent, new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, 
  40.                 ComponentContainer.LayoutConfig.MATCH_CONTENT)); 
  41.     } 
  42.  
  43.     /** 
  44.      * 创建组件 
  45.      * 
  46.      * @param context 上下文 
  47.      * @return text 
  48.      */ 
  49.     private Text createcomponentForTextSwitcher(Context context) { 
  50.         Text text = new Text(context); 
  51.         text.setTextSize(TEXT_SIZE); 
  52.         text.setFont(Font.DEFAULT_BOLD); 
  53.         text.setTextColor(Color.WHITE); 
  54.         text.setLayoutConfig(new PageFlipper.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, 
  55.                 PageFlipper.LayoutConfig.MATCH_CONTENT)); 
  56.         return text; 
  57.     } 
  58.  
  59.     /** 
  60.      * update 
  61.      * 
  62.      * @param newPosition   新位置 
  63.      * @param oldPosition   旧位置 
  64.      * @param totalElements 总数 
  65.      */ 
  66.     public void update(int newPosition, int oldPosition, int totalElements) { 
  67.         textcomponent.setText(" / " + totalElements); 
  68.         int offset = textSwitcher.getHeight(); 
  69.         if (newPosition > oldPosition) { 
  70.             //设置组件进入和退出的动画 
  71.             textSwitcher.setIncomingAnimation(createPositionAnimation(-offset, 0, 0f, 1f, DURATION)); 
  72.             textSwitcher.setOutgoingAnimation(createPositionAnimation(0, offset, 1f, 0f, DURATION)); 
  73.         } else if (oldPosition > newPosition) { 
  74.             textSwitcher.setIncomingAnimation(createPositionAnimation(offset, 0, 0f, 1f, DURATION)); 
  75.             textSwitcher.setOutgoingAnimation(createPositionAnimation(0, -offset, 1f, 0f, DURATION)); 
  76.         } 
  77.         //显示下一个组件并执行动画 
  78.         textSwitcher.showNext(); 
  79.         Text text = (Text) textSwitcher.getCurrentComponent(); 
  80.         text.setText(String.valueOf(newPosition + 1)); 
  81.     } 
  82.  
  83.     /** 
  84.      * 创建属性动画 
  85.      * 
  86.      * @param fromY 
  87.      * @param toY 
  88.      * @param fromAlpha 
  89.      * @param toAlpha 
  90.      * @param duration 
  91.      * @return 
  92.      */ 
  93.     private AnimatorProperty createPositionAnimation(int fromY, int toY, float fromAlpha, float toAlpha, int duration) { 
  94.         AnimatorProperty animatorProperty = new AnimatorProperty(); 
  95.         animatorProperty.setCurveType(Animator.CurveType.DECELERATE); 
  96.         animatorProperty.alphaFrom(fromAlpha); 
  97.         animatorProperty.alpha(toAlpha); 
  98.         animatorProperty.moveFromY(fromY); 
  99.         animatorProperty.moveToY(toY); 
  100.         animatorProperty.setDuration(duration); 
  101.         return animatorProperty; 
  102.     } 

结束

以上主要介绍了PageSlider和PageFlipper的一些简单使用,最后补充一个小功能,设置渐变效果,这个简单的效果可能很多人还不知道如何设置:

首先生成一个foreground_gradient.xml

  1. <shape 
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:shape="rectangle"
  4.  
  5.     //设置填充的颜色,可以根据实际需要设置多个 
  6.     <solid 
  7.         ohos:colors="#000000,#00ffffff,#d8000000"/> 
  8.     //设置渐变方向,有三个值可供选择:linear_gradient,radial_gradient,sweep_gradient 
  9.     <gradient 
  10.         ohos:shader_type="linear_gradient" 
  11.         /> 
  12. </shape> 

 然后给目标组件设置前景色,即:

  1. ohos:foreground_element="$graphic:foreground_gradient" 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-07 09:53:45

鸿蒙HarmonyOS应用

2021-09-09 14:49:26

鸿蒙HarmonyOS应用

2021-10-14 15:14:36

鸿蒙HarmonyOS应用

2021-09-16 10:05:09

鸿蒙HarmonyOS应用

2021-08-12 15:01:09

鸿蒙HarmonyOS应用

2021-12-03 09:49:59

鸿蒙HarmonyOS应用

2021-09-14 09:34:05

鸿蒙HarmonyOS应用

2021-09-23 10:00:57

鸿蒙HarmonyOS应用

2021-08-26 09:50:06

鸿蒙HarmonyOS应用

2021-09-13 15:14:01

鸿蒙HarmonyOS应用

2021-08-31 14:58:52

鸿蒙HarmonyOS应用

2021-11-03 09:51:45

鸿蒙HarmonyOS应用

2022-02-16 16:37:51

HarmonyOSArkUI操作系统

2021-09-03 15:27:17

鸿蒙HarmonyOS应用

2021-03-22 09:56:01

Java基础System类Static

2011-07-13 16:14:53

C++引用指针

2021-03-18 10:01:06

Java编译异常运行异常

2021-08-17 10:20:14

鸿蒙HarmonyOS应用

2021-01-04 11:44:05

鸿蒙HarmonyOSAbilitySlic

2021-09-15 14:55:49

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号