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

HarmonyOS基礎(chǔ)之PageSlider和PageFlipper

系統(tǒng) OpenHarmony
PageSlider可以說是鴻蒙中最常用的視圖切換組件了,使用方法不用多做介紹,官方文檔有詳細(xì)的說明,這里主要說一下一個特殊的效果。

[[425826]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

眾所周知,PageSlider是用于頁面之間切換的組件,它通過響應(yīng)滑動事件完成頁面間的切換,而PageFlipper可能知道的人就比較少了,其實PageFlipper和PageSlider類似,都是視圖切換組件,它們都繼承自StackLayout,因此可以將多個component層疊在一起,每次只顯示一個組件,當(dāng)視圖從一個component切換到另一個component時,PageFlipper支持指定動畫效果。

區(qū)別:PageFlipper通過addComponent()添加component,可使用動畫控制多個component之間的切換效果,是個輕量級的組件,適合展示少量靜態(tài)數(shù)據(jù);而PageSlide是由provider來提供component的,更適用復(fù)雜的視圖切換,實現(xiàn)數(shù)據(jù)的動態(tài)加載。

下面是一個PageSlider和PageFlipper結(jié)合起來的使用效果,頁面中間的卡片使用的是PageSlider,背景圖片和底部的數(shù)字指示器用的是PageFlipper,通過回調(diào)將三個組件聯(lián)動起來就實現(xiàn)了這樣的效果:

HarmonyOS 基礎(chǔ)之PageSlider和PageFlipper-鴻蒙HarmonyOS技術(shù)社區(qū)

正文

1.pageSlider

PageSlider可以說是鴻蒙中最常用的視圖切換組件了,使用方法不用多做介紹,官方文檔有詳細(xì)的說明,這里主要說一下一個特殊的效果。

一屏多頁效果

其實鴻蒙本身有提供一個setClipEnabled()的方法,作用是設(shè)置是否允許在組件超出其父布局時自動裁剪組件,理論上通過給pageSlider父布局設(shè)置setClipEnabled(false),加上給子組件設(shè)置合適的寬度可以實現(xiàn)一屏多頁效果,但是經(jīng)過測試并沒達(dá)到效果,這個方法我也單獨拿出來在其他場景驗證過確實無效,下面是驗證的效果。

HarmonyOS 基礎(chǔ)之PageSlider和PageFlipper-鴻蒙HarmonyOS技術(shù)社區(qū)

但是鴻蒙卻提供了另外一個方法setPageMargin(),它的作用是設(shè)置PageSlider中子組件邊距的,當(dāng)傳入一個合適的負(fù)數(shù)時(必須是負(fù)數(shù)),就能實現(xiàn)一屏同時顯示多個子組件的效果:

HarmonyOS 基礎(chǔ)之PageSlider和PageFlipper-鴻蒙HarmonyOS技術(shù)社區(qū)

動態(tài)設(shè)置縮放透明度變化

設(shè)置透明度和縮放比例就不細(xì)說了,主要就是在PageSlider子組件加載完成后和頁面切換中的回調(diào)方法中改變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.      * 設(shè)置初始狀態(tài)的縮放和透明度 
  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.      * 設(shè)置滑動中的縮放和透明度 
  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.     } 

 設(shè)置兩邊的component透明度和縮放效果:

  1. //設(shè)置初始狀態(tài)縮放和透明度 
  2. AlphaScalePageTransformer.defaultPage(image, i, pageSlider.getCurrentPage()); 
  3.  
  4. //設(shè)置頁面切換中縮放和透明度 
  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是一個翻頁器,當(dāng)它有兩個或多個子組件時,切換過程中可以輕松設(shè)置入場動畫和出場動畫,以達(dá)到意想不到的效果。雖然PageFlipper的使用率遠(yuǎn)不及PageSlider,但這并不意味著PageFlipper就不強(qiáng)大,他能通過簡單的代碼實現(xiàn)許多動畫效果,比如淘寶頭條的效果,日歷翻頁效果,背景圖淡入淡出效果等等。

常用方法:

  1. getCurrentComponent()//獲取當(dāng)前組件 
  2.  
  3. showNext():顯示下一個組件(如果當(dāng)前子組件是最后一個,則顯示第一個子組件) 
  4.  
  5. showPrevious():顯示上一個組件(如果當(dāng)前子組件是第一個,則顯示最后一個子組件) 
  6.  
  7. getFlipInterval() :獲取自動翻轉(zhuǎn)時間 
  8.  
  9. setFlipPeriod(int period) :設(shè)置翻轉(zhuǎn)周期 
  10.  
  11. startFlipping() :開啟自動翻轉(zhuǎn) 
  12.  
  13. stopFlipping() :停止自動翻轉(zhuǎn) 
  14.  
  15. addComponent() :添加組件 
  16.  
  17. setIncomingAnimationA() :設(shè)置轉(zhuǎn)入動畫 
  18.  
  19. setOutgoingAnimation() :設(shè)置轉(zhuǎn)出動畫 

 下面通過設(shè)置文字翻頁效果來了解下它的使用方法:

HarmonyOS 基礎(chǔ)之PageSlider和PageFlipper-鴻蒙HarmonyOS技術(shù)社區(qū)
  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就能實現(xiàn)動畫效果,但是實際測試發(fā)現(xiàn)如果切換速度太快就導(dǎo)致子組件銜接不上出現(xiàn)組件消失的額情況, 
  28.         //因此這里通過實踐多添加了幾個子component,防止滑動過快出現(xiàn)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.      * 創(chuàng)建組件 
  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 總數(shù) 
  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.             //設(shè)置組件進(jìn)入和退出的動畫 
  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.         //顯示下一個組件并執(zhí)行動畫 
  78.         textSwitcher.showNext(); 
  79.         Text text = (Text) textSwitcher.getCurrentComponent(); 
  80.         text.setText(String.valueOf(newPosition + 1)); 
  81.     } 
  82.  
  83.     /** 
  84.      * 創(chuàng)建屬性動畫 
  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.     } 

結(jié)束

以上主要介紹了PageSlider和PageFlipper的一些簡單使用,最后補(bǔ)充一個小功能,設(shè)置漸變效果,這個簡單的效果可能很多人還不知道如何設(shè)置:

首先生成一個foreground_gradient.xml

  1. <shape 
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:shape="rectangle"
  4.  
  5.     //設(shè)置填充的顏色,可以根據(jù)實際需要設(shè)置多個 
  6.     <solid 
  7.         ohos:colors="#000000,#00ffffff,#d8000000"/> 
  8.     //設(shè)置漸變方向,有三個值可供選擇:linear_gradient,radial_gradient,sweep_gradient 
  9.     <gradient 
  10.         ohos:shader_type="linear_gradient" 
  11.         /> 
  12. </shape> 

 然后給目標(biāo)組件設(shè)置前景色,即:

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

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-09-07 09:53:45

鴻蒙HarmonyOS應(yīng)用

2021-09-09 14:49:26

鴻蒙HarmonyOS應(yīng)用

2021-09-16 10:05:09

鴻蒙HarmonyOS應(yīng)用

2021-10-14 15:14:36

鴻蒙HarmonyOS應(yīng)用

2021-08-12 15:01:09

鴻蒙HarmonyOS應(yīng)用

2021-03-18 10:01:06

Java編譯異常運(yùn)行異常

2021-12-03 09:49:59

鴻蒙HarmonyOS應(yīng)用

2021-03-22 09:56:01

Java基礎(chǔ)System類Static

2011-07-13 16:14:53

C++引用指針

2021-09-14 09:34:05

鴻蒙HarmonyOS應(yīng)用

2011-06-03 09:25:00

IPXWINS

2021-04-05 08:11:04

Java基礎(chǔ)Calendar類DateFormat類

2021-04-08 10:10:46

JavaSimpleDateFList接口

2023-11-02 18:45:00

Rust編程表達(dá)式

2021-03-29 10:00:32

Java基礎(chǔ)Random類Random

2021-09-23 10:00:57

鴻蒙HarmonyOS應(yīng)用

2009-03-12 10:52:43

Java線程多線程

2024-01-05 17:41:36

Rust編程循環(huán)

2012-10-17 14:20:57

架構(gòu)算法PHP

2011-07-04 16:04:20

Applet
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 看a网站| 日韩人体在线 | 亚洲av一级毛片 | 久久精品成人 | 99成人精品 | 久久大陆| h在线| 亚洲色综合 | 五月天激情电影 | 久久亚洲欧美日韩精品专区 | 欧美精品1区2区3区 精品国产欧美一区二区 | 在线视频a | 7777奇米影视 | 91精品福利 | 久久久久久免费毛片精品 | 成人一区av| www.97国产| 欧美日韩一区二区三区四区 | 久久久精品一区 | 亚洲成a人片 | 国产亚洲二区 | 亚洲欧美日韩一区二区 | 91精品国产综合久久久久久蜜臀 | 亚洲成人一区二区 | 国产伦一区二区三区 | 精品91| 亚洲精品日日夜夜 | 中文字幕日韩一区 | 中文字幕在线电影观看 | 国产中文区二幕区2012 | 久久看精品 | 精品国产18久久久久久二百 | 成人自拍视频 | 一级毛片在线播放 | 亚洲国产精品一区二区第一页 | www.国产一区 | 国产片网站 | 精品中文视频 | 免费日本视频 | 欧美一级在线观看 | 成人三级影院 |