偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

HarmonyOS基礎(chǔ)之PageSlider和PageFlipper

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

[[425826]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com

前言

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

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

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

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

正文

1.pageSlider

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

一屏多頁(yè)效果

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

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

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

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

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

設(shè)置透明度和縮放比例就不細(xì)說(shuō)了,主要就是在PageSlider子組件加載完成后和頁(yè)面切換中的回調(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è)置滑動(dòng)中的縮放和透明度 
  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è)置頁(yè)面切換中縮放和透明度 
  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(翻頁(yè)器)

PageFlipper是一個(gè)翻頁(yè)器,當(dāng)它有兩個(gè)或多個(gè)子組件時(shí),切換過(guò)程中可以輕松設(shè)置入場(chǎng)動(dòng)畫(huà)和出場(chǎng)動(dòng)畫(huà),以達(dá)到意想不到的效果。雖然PageFlipper的使用率遠(yuǎn)不及PageSlider,但這并不意味著PageFlipper就不強(qiáng)大,他能通過(guò)簡(jiǎn)單的代碼實(shí)現(xiàn)許多動(dòng)畫(huà)效果,比如淘寶頭條的效果,日歷翻頁(yè)效果,背景圖淡入淡出效果等等。

常用方法:

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

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

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.      * 動(dòng)畫(huà)時(shí)長(zhǎng) 
  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只需要添加兩個(gè)子component就能實(shí)現(xiàn)動(dòng)畫(huà)效果,但是實(shí)際測(cè)試發(fā)現(xiàn)如果切換速度太快就導(dǎo)致子組件銜接不上出現(xiàn)組件消失的額情況, 
  28.         //因此這里通過(guò)實(shí)踐多添加了幾個(gè)子component,防止滑動(dòng)過(guò)快出現(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)入和退出的動(dòng)畫(huà) 
  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.         //顯示下一個(gè)組件并執(zhí)行動(dòng)畫(huà) 
  78.         textSwitcher.showNext(); 
  79.         Text text = (Text) textSwitcher.getCurrentComponent(); 
  80.         text.setText(String.valueOf(newPosition + 1)); 
  81.     } 
  82.  
  83.     /** 
  84.      * 創(chuàng)建屬性動(dòng)畫(huà) 
  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的一些簡(jiǎn)單使用,最后補(bǔ)充一個(gè)小功能,設(shè)置漸變效果,這個(gè)簡(jiǎn)單的效果可能很多人還不知道如何設(shè)置:

首先生成一個(gè)foreground_gradient.xml

  1. <shape 
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:shape="rectangle"
  4.  
  5.     //設(shè)置填充的顏色,可以根據(jù)實(shí)際需要設(shè)置多個(gè) 
  6.     <solid 
  7.         ohos:colors="#000000,#00ffffff,#d8000000"/> 
  8.     //設(shè)置漸變方向,有三個(gè)值可供選擇: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)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(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
點(diǎn)贊
收藏

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