成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-08-12 15:01:09

鴻蒙HarmonyOS應用

2021-09-16 10:05:09

鴻蒙HarmonyOS應用

2021-10-14 15:14:36

鴻蒙HarmonyOS應用

2021-03-18 10:01:06

Java編譯異常運行異常

2021-03-22 09:56:01

Java基礎System類Static

2011-07-13 16:14:53

C++引用指針

2021-12-03 09:49:59

鴻蒙HarmonyOS應用

2011-06-03 09:25:00

IPXWINS

2021-04-05 08:11:04

Java基礎Calendar類DateFormat類

2021-04-08 10:10:46

JavaSimpleDateFList接口

2023-11-02 18:45:00

Rust編程表達式

2021-03-29 10:00:32

Java基礎Random類Random

2009-03-12 10:52:43

Java線程多線程

2024-01-05 17:41:36

Rust編程循環

2012-10-17 14:20:57

架構算法PHP

2011-07-04 16:04:20

Applet

2021-09-14 09:34:05

鴻蒙HarmonyOS應用

2011-07-14 22:36:37

C++
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91中文字幕 | 欧美自拍视频 | 欧美日韩国产免费 | 精品1区2区 | 国产伦精品一区二区 | 精品免费国产一区二区三区四区 | 91精品国产综合久久福利软件 | 日韩精品一区二区三区在线播放 | 91麻豆精品一区二区三区 | 男人天堂免费在线 | 久草免费在线视频 | 一区二区三区四区不卡视频 | 色综合99 | 亚洲另类自拍 | 午夜免费福利片 | 亚洲国产乱码 | 国产91丝袜在线播放 | 一区二区三区四区免费视频 | 欧美精品福利视频 | 在线欧美亚洲 | 国产美女黄色片 | 成人在线免费观看 | 麻豆精品久久 | 国产a区 | 国产精品视频二区三区 | 国产精品色 | 婷婷不卡| 色资源在线观看 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 国产中文一区二区三区 | 在线一区 | 97精品超碰一区二区三区 | 国产精品欧美一区二区三区不卡 | 91av在线看| 成人免费淫片aa视频免费 | 91精品久久久久久久久久 | 免费视频久久 | a国产视频 | 成人精品在线视频 | 亚洲国产成人精品一区二区 | 精品视频一区二区 |