鸿蒙HarmonyOS自定义旋转进度对话框开发

        Harmony中文翻译过来,和谐,融洽,HarmonyOS表面华为希望构建和谐共赢的操作系统,华为动了美国等西方国家顶层技术劳工的饭碗,导致被打压,可以说继续往前发展,中国最终会成为技术的引领者,西方国家技术垄断终会被打破,过去我们工作10几个小时才能挣一两百,而他们工作几个小时就能挣一千的格局终会被打破。中国加油!程序猿加油!

         说多了,还是说说我们今天要完成的自定义旋转进度对话框吧,和android一样,android也没有现成的组件,需要通过Dialog+Animation来实现,dialog弹出对话框,在对话框中实现图片的旋转动画来实现。

         自定义对话框代码ProgressDialog.java:

package com.figo.demo.utils;

import com.figo.demo.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

public class ProgressDialog extends CommonDialog {
    /**
     * 进度图片  .
     */
    private Component propertyAnimationImage;
    /**
     * 动画 .
     */
    AnimatorProperty animator;

    /**
     * 构造函数 .
     * @param context
     */
    public ProgressDialog(Ability context) {
        super(context);
        initComponent(context);
    }

    /**
     * 初始化组件 .
     * @param context
     */
    private void initComponent(Ability context)
    {
        Component customComponent = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_progress_dialog, null, true);
        super.setContentCustomComponent(customComponent);
        propertyAnimationImage = customComponent.findComponentById(ResourceTable.Id_img_loading);
        //不知为啥,鸿蒙OS的ability及dialog中,不能直接启动动画,需要加载完成后,单独给个方法启动动画
        //startRotateAnimation(propertyAnimationImage);

    }

    /**
     * 开始动画 .
     */
    public void startRotateAnimation() {
        if(propertyAnimationImage!=null) {
            AnimatorProperty animator = propertyAnimationImage.createAnimatorProperty();
            animator.setCurveType(Animator.CurveType.LINEAR);//曲线类型,大家可以试试看选择合适的
            animator.rotate(360);//顺时针旋转360度,负值是反向
            animator.setDuration(2000);//旋转一圈的时间毫秒
            animator.setLoopedCount(-1);//-1表示无限次
            animator.start();
        }
    }

    /**
     * 结束动画 .
     */
    public void stopRotateAnimation()
    {
        if(animator!=null&&animator.isRunning())
        {
            animator.stop();
        }
    }
}

          对话框页面layout_progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#00000000"
    ohos:alignment="center"
    ohos:orientation="vertical">
    <Text
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="$string:dialog_title"
        ohos:text_size="20vp">

    </Text>
    <Image
        ohos:id="$+id:img_loading"
        ohos:height="70vp"
        ohos:width="70vp"
        ohos:scale_mode="stretch"
        ohos:image_src="$media:ic_loading">
    </Image>
</DirectionalLayout>

        比如在OKHTTP请求的时候调用:

ProgressDialog progressDialog = new ProgressDialog(getAbility());
progressDialog.setTransparent(true);
progressDialog.show();
progressDialog.startRotateAnimation();

请求地址给出返回结果的时候关闭:

//必须这么写,否则会出现Looper.don't prepare的错误,getUITaskDispatcher应该就像
//android的runOnUiThread,toast,dialog需要运行在主线程

getUITaskDispatcher().asyncDispatch(new Runnable() {
    @Override
    public void run() {
        progressDialog.stopRotateAnimation();
        progressDialog.destroy();
    }
});
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值