OpenHarmony 实战开发——CircleIndicator组件,使指示器风格更加多样化

751 篇文章 5 订阅
606 篇文章 10 订阅

UI界面是应用程序可视化必不可少的部分。设计精致的UI界面可以使得整个可视化应用程序给用户留下深刻的印象,是改善用户界面体验最直接的方式。

ArkUI开发框架为开发者提供了丰富的UI原生组件,如Navigation(Page页面的根容器)、ScrollBar(滚动条组件)、Swiper(滑动容器)及Tabs(通过页签进行内容视图切换的容器组件)等。其中,Swiper组件和Tabs组件在应用程序开发中对于指示器的使用引入较多,但是直接使用原生的Swiper组件和Tabs组件不适用于表现复杂的UI指示器交互变化。因此,我们针对Swiper组件和Tabs组件在指示器应用方向做了一个简单的封装,以CiecleIndicator三方组件的形式提供应用程序依赖使用,从而提升了ArkUI开发框架的UI界面之指示器风格多样化的能力。

CircleIndicator介绍

CircleIndicator组件UI效果展示

圆形指示器:


长条指示器:


横幅指示器:

三角指示器:


图标指示器:


携带中央视图的Tabs指示器:


固定位置Tabs指示器:


固定位置Tabs指示器(胶囊风格):


固定位置Tabs指示器(携带角标):

可滑动Tabs指示器:

可滑动Tabs指示器(水滴滑块):

可滑动Tabs指示器(首列固定):

titles指示器:

什么是CircleIndicator?

CircleIndicator顾名思义,它指的是圆形指示器。不过在我们OpenHarmony三方组件中的CircleIndicator组件不再是狭义的圆形指示器,而是将多种表现形式的指示器汇集为一体的归一化指示器合集组件。

CircleIndicator的源码实现

这里我们以CircleIndicator组件源码中的TriangularIndicator.ets文件为源码解析样例对象展开分析。首先创建TriangularIndicator.ets文件,使用命名空间建立TriangularIndicator初始化模型:

namespace TriangularIndicator {
  export class Model {
//设置指示器高度
    mHeight: number = 18
//设置指示器宽度
    mWidth: number = lpx2px(720)
//设置指示器背景色
    mBackgroundColor: string
//字段过多,此处进行省略
//各字段set与get方法,此处只以height字段为例
    public getHeight(): number {
      return this.mHeight
    }
    public setHeight(height: number): Model {
      this.mHeight = height
      return this
    }
    //触摸事件拦截
    onPageTouch: (event: TouchEvent, currentIndicator: number) => void
    public notifyTouch(event: TouchEvent, currentIndex: number) {
      this.onPageTouch(event, currentIndex)
    }
    //设置构造器
    private tabsController: TabsController
        (tabsController: TabsController) {
      this.tabsController = tabsController
    }
    //页面切换监听
    indexChange: (itemIndex: number) => void
    public setChangeListener(callback: (index: number) => void): Model{
      this.indexChange = callback
      return this
    }
}

将TriangularIndicator应用组件化:

@Component
struct TriangularIndicator {
//获取TriangularIndicator实例
  @State model: TriangularIndicator.Model = new TriangularIndicator.Model(null)
//初始化指示器当前index
  @Link @Watch("itemIndexChange") itemIndex: number
//设置指示器总条数
  @State count: number = 0
//再分别实现itemIndexChange、aboutToAppear、onTouch、getOffset方法,此处实现不做展示
//再在build方法里面描述UI结构
    build() {
      Column() {
        Rect({ width: this.model.mWidth, height:     this.model.mLineHeight }).fill(this.model.mLineColor)
        Polygon()
          .points(this.model.mReverse ?
        [[px2vp(this.model.mWidth) / (this.count * 2) - this.model.mTriangleWidth / 2, this.model.mLineHeight - this.model.mYOffset],
        [px2vp(this.model.mWidth) / (this.count * 2), this.model.mLineHeight + this.model.mTriangleHeight - this.model.mYOffset],
        [px2vp(this.model.mWidth) / (this.count * 2) + this.model.mTriangleWidth / 2, this.model.mLineHeight - this.model.mYOffset]] :
        [[px2vp(this.model.mWidth) / (this.count * 2) - this.model.mTriangleWidth / 2, -this.model.mYOffset],
  [px2vp(this.model.mWidth) / (this.count * 2), -this.model.mTriangleHeight - this.model.mYOffset],
        [px2vp(this.model.mWidth) / (this.count * 2) + this.model.mTriangleWidth / 2, -this.model.mYOffset]])
          .offset(this.model.mStartInterpolator ?
            { x: px2vp(this.model.mWidth) / this.count * (this.itemIndex -     this.model.mStartInterpolator.interpolate(Math.abs(this.model.offs    et / this.model.mWidth)) * Math.sign(this.model.offset)),
              y: 0 } :
            { x: px2vp(this.model.mWidth) / this.count * (this.itemIndex - this.model.offset / this.model.mWidth),
              y: 0 })
          .fill(this.model.mLineColor)
          .height(this.model.mHeight)
          .width(this.model.mWidth)
      }.width('100%').height(this.model.mHeight)
      .backgroundColor(this.model.mBackgroundColor)
    }
}

最后将TriangularIndicator暴露出来供外部引用:

export default TriangularIndicator

CircleIndicator实战

创建项目

如图所示,在DevEco Studio中新建CircleIndicator_Test项目,项目类型选择Application,语言选择eTS,点击Finish完成创建。

创建工程

添加依赖

成功创建项目后,接下来就是将CircleIndicator组件下载至项目中。请在添加依赖之前参考如何安装OpenHarmony npm包(https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md)完成OpenHarmony npm环境配置。完成OpenHarmony npm环境配置之后,在DevEco Studio的底部导航栏,点击“Terminal”(快捷键Alt+F12), 键入命令:npm install @ohos/circle-indicator --save并回车,此时CircleIndicator组件会自动下载至项目中。下载完成后工程根目录下会生成node_modules/@ohos/CircleIndicator目录。

编写逻辑代码

提供多种Indicator,使用方法类似,以TriangularIndicator为例

1. 初始化:实例化TabsController和对应的Indicator.Model 对象, 并添加number类型成员以记录当前页下标

private controller: TabsController = new TabsController()
@State model: TriangularIndicator.Model = new TriangularIndicator.Model(this.controller)
@State itemIndex: number = 0

2. 属性设置:通过Model类对象设置UI属性来自定义所需风格,也可以添加所需的回调

aboutToAppear() {
  this.model
    .setReverse(true)
    .setLineHeight(4)
    .setTriangleHeight(10)
    .setLineColor("#e94220")
    .setBackgroundColor("#eeeeee")
    .setChangeListener((itemIndex) => {
      console.info("change page to " + this.data[itemIndex])
    })
}

3. 界面绘制
:在Tabs组件旁放置Indicator组件,注意需要关闭原生的bar。并监听Tabs组件的touch事件,通知给Model类,以统一处理滑动逻辑

build() {
  Column() {
    TriangularIndicator({ itemIndex: $itemIndex, count: this.data.length, model: this.model })
    Tabs({ index: this.itemIndex, controller: this.controller }) {
      ……
    }
    .barWidth(0)
    .onTouch((event: TouchEvent) => {
      this.model.notifyTouch(event, this.itemIndex)
    })
  }.padding({ top: 40 })
  .backgroundColor("#eeeeee")
}

为了帮助到大家能够更有效的学习OpenHarmony 开发的内容,下面特别准备了一些相关的参考学习资料:

OpenHarmony 开发环境搭建:https://qr18.cn/CgxrRy

《OpenHarmony源码解析》:https://qr18.cn/CgxrRy

  • 搭建开发环境
  • Windows 开发环境的搭建
  • Ubuntu 开发环境搭建
  • Linux 与 Windows 之间的文件共享
  • ……

系统架构分析:https://qr18.cn/CgxrRy

  • 构建子系统
  • 启动流程
  • 子系统
  • 分布式任务调度子系统
  • 分布式通信子系统
  • 驱动子系统
  • ……

OpenHarmony 设备开发学习手册:https://qr18.cn/CgxrRy

在这里插入图片描述

OpenHarmony面试题(内含参考答案):https://qr18.cn/CgxrRy

  • 22
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果。它可以自定义指示器上小圆点的样式和动画效果。可以用于引导页。项目地址:https://github.com/ongakuer/CircleIndicator 效果图:如何使用1. xml布局中创建CircleIndicator是配合ViewPager使用的,一般如下布局:<RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>2. java代码中// DEFAULT ViewPager defaultViewpager = (ViewPager) findViewById(R.id.viewpager_default); CircleIndicator defaultIndicator = (CircleIndicator) findViewById(R.id.indicator_default); DemoPagerAdapter defaultPagerAdapter = new DemoPagerAdapter(getSupportFragmentManager()); defaultViewpager.setAdapter(defaultPagerAdapter);//为ViewPager设置适配器 defaultIndicator.setViewPager(defaultViewpager);//将ViewPager添加到CircleIndicator中,以便监听ViewPager的滚动等事件DemoPagerAdapter是ViewPager的适配器,这个需要你去实现。属性说明属性名称类型说明ci_widthdimension小圆点的宽度ci_heightdimension小圆点的高度ci_margindimension小圆点间的间距ci_animatorreferenceViewPager页面切换时,给小圆点设置动画。设置当前的小圆点恢复到未选中时的状态的动画。例如:当前正从a切换到b,那么该属性设置的是a的动画。ci_animator_reversereference和"ci_animator"属性一样,只不过该属性设置的是b的动画。ci_drawablereference设置当前或选中的小圆点的样式,如颜色、圆角等。ci_drawable_unselectedreference和"ci_drawable"一样,只是该属性设置的是未选中的小圆点的样式。点击上面的"下载源码"下载完整的demo工程。demo简要说明xml布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_custom"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_custom"             app:ci_width="10dp"             app:ci_height="4dp"             app:ci_margin="6dp"             app:ci_animator="@animator/indicator_animator"             app:ci_animator_reverse="@animator/indicator_animator_reverse"             app:ci_drawable="@drawable/black_radius_square"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_unselected_background"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_unselected_background"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             app:ci_width="6dp"             app:ci_height="6dp"             app:ci_animator="@animator/indicator_no_animator"             app:ci_drawable="@drawable/white_radius"             app:ci_drawable_unselected="@drawable/black_radius"             android:layout_height="40dp"             />     </RelativeLayout> </LinearLayout>定义了3组ViewPager和指示器。第一组是采用默认样式的,被选中的小圆点带有放大的动画。第2组比较全,既定义了动画又修改了样式。第3组是取消动画的同时使用了ci_drawable_unselected属性。适配器Adapter:public class DemoPagerAdapter extends FragmentPagerAdapter {         private int pagerCount = 5;         private Random random = new Random();         public DemoPagerAdapter(FragmentManager fm) {             super(fm);         }         @Override public Fragment getItem(int i) {             return ColorFragment.newInstance(0xff000000 | random.nextInt(0x00ffffff));         }         @Override public int getCount() {             return pagerCount;         }     }定义了5个页面或Fragment,通过ColorFragment生成Fragment。Fragment:public class ColorFragment extends Fragment {     private static final String ARG_COLOR = "color";     private int mColor;     public static ColorFragment newInstance(int param1) {         ColorFragment fragment = new ColorFragment();         Bundle args = new Bundle();         args.putInt(ARG_COLOR, param1);         fragment.setArguments(args);         return fragment;     }     public ColorFragment() {     }     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         if (getArguments() != null) {             mColor = getArguments().getInt(ARG_COLOR);         }     }     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState) {         View v = inflater.inflate(R.layout.color_fragment, container, false);         v.setBackgroundColor(mColor);         return v;     } }生成空的页面,没有任何控件,只是设置了页面的背景色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值