鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件 原创

六合李欣
发布于 2021-2-14 23:15
浏览
1收藏

1. 首先谢谢https://harmonyos.51cto.com/posts/2375  51CTO这位仁兄的帖子 标题为:HarmonyOS UltimateProvider简单快速的使用ListContainer,UltimateProvider组件非常好用。再昨天的动画基础上,自定义了文字动画,只要掌握多线程的技能的结合华为的动画组件,就能够做成很好的动画应用.效果如下:

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

 

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

 

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

 

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

通过多线程控制每个字出现的频率。同时获取UI线程,处理每次获取的文字(这个做过android的同学都知道UI线程的使用),处理的代码如下:

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

2. 跳转到主界面,这次通过HarmonyOS UltimateProvider数据绑定组件(真的好用,数据和视图分离处理),通过OKHttp请求网络对JSON数据解析,获取了笑话大全,哈哈,过年了,笑一笑。

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

3.Java代码实现如下:

package com.example.javahm8.slice;

import com.example.javahm8.ResourceTable;
import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.indicators.BallGridPulseIndicator;
import com.wang.avi.indicators.BallPulseIndicator;
import com.wang.avi.indicators.BallPulseRiseIndicator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.bundle.AbilityInfo;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {

    private DependentLayout myLayout = new DependentLayout(this);
    private ArrayList<AVLoadingIndicatorView> animatorList=new ArrayList<>();

    int  i=0;

    private  String[]  strs={"牛","气","冲","天"};

    String  msg="";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //super.setUIContent(ResourceTable.Layout_ability_main);
        setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);

        LayoutConfig config = new
                LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);


        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255,106,106));
        myLayout.setBackground(element);

        ShapeElement commonElement = new ShapeElement();
        commonElement.setRgbColor(new RgbColor(255,106,106));

        LayoutConfig ballGridPulseIndicatorConfig = new LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        ballGridPulseIndicatorConfig.setMargins(300,700,0,0);
        BallGridPulseIndicator ballGridPulseIndicator=new BallGridPulseIndicator(this);
        ballGridPulseIndicator.setLayoutConfig(ballGridPulseIndicatorConfig);
        ballGridPulseIndicator.setHeight(500);
        ballGridPulseIndicator.setWidth(500);
        ballGridPulseIndicator.setBackground(commonElement);
        myLayout.addComponent(ballGridPulseIndicator);
        animatorList.add(ballGridPulseIndicator);

        Text text = new Text(getContext());
        text.setText(msg);
        text.setTextSize(150);
        text.setTextColor(Color.WHITE);

        text.setId(100);
        // 为组件添加对应布局的布局属性
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layoutConfig.setMargins(250,1200,0,0);
        text.setLayoutConfig(layoutConfig);
        // 将Text添加到布局中
        myLayout.addComponent(text);

        new  Thread(new Runnable() {
            @Override
            public void run() {

                while(i<=6)
                {

                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    getUITaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {

                            if(i<=3)
                            {
                                msg+=strs[i];
                                text.setText(msg);
                            }
                            else    if(i==6)
                            {
                                present(new ContentAbilitySlice(), new Intent());
                            }

                            i++;

                        }
                    });


                  }


            }
        }).start();

        super.setUIContent(myLayout);
    }
    private void startAllAnimator(ArrayList<AVLoadingIndicatorView> avLoadingIndicatorViews){
        for (int i = 0; i < avLoadingIndicatorViews.size(); i++) {
            avLoadingIndicatorViews.get(i).start();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
        startAllAnimator(animatorList);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

 

3.跳转页面的布局文件代码:

<?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="#CD5C5C"
    ohos:orientation="vertical">


    <ListContainer
        ohos:id="$+id:list_container"
        ohos:layout_alignment="horizontal_center"
        ohos:height="match_parent"
        ohos:width="match_parent"


        />

</DirectionalLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:left_margin="16vp"
    ohos:right_margin="16vp"
    ohos:orientation="vertical">

    <!-- 列表项 -->
    <Text
        ohos:id="$+id:item_index"
        ohos:height="match_content"
        ohos:multiple_lines="true"
        ohos:width="match_content"
        ohos:bottom_margin="20vp"
        ohos:padding="4vp"
        ohos:text="Item0"
        ohos:text_size="20fp"
        ohos:layout_alignment="center"/>


</DirectionalLayout>

 

4.主界面的Java代码:

package com.example.javahm8.slice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chg.ultimateprovider.UltimateProvider;
import com.example.javahm8.ResourceTable;
import com.example.javahm8.model.SampleItem;
import com.example.javahm8.net.HttpUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;


public class ContentAbilitySlice  extends AbilitySlice {

    private ListContainer listContainer;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initview();

        initData();
    }
    private void initview()
    {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
    }

    private  void  initData()
    {
        List<SampleItem> list = new ArrayList<>();
        new  Thread(new Runnable() {
            @Override
            public void run() {
                try
                {
                    String response = new HttpUtils().run("http://v.juhe.cn/joke/randJoke.php?key=4e01baabf2430f62a857112f331a52d5");
                   // System.out.println(response);

                    JSONObject  jobj=JSONObject.parseObject(response);

                   JSONArray  arrays=jobj.getJSONArray("result");

                    getUITaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {

                            for(int i=0;i<arrays.size();i++)
                            {
                                String content=arrays.getJSONObject(i).getString("content");
                                // System.out.println(content);
                                list.add(new SampleItem(content));

                            }

                            listContainer.setItemProvider(new UltimateProvider(list, getContext()));

                        }
                    });





                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

            }
        }).start();

    }





}

 

5. 网络请求Java代码:

package com.example.javahm8.net;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class HttpUtils {

    private OkHttpClient client = new OkHttpClient();

    public String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}

 

6.数据模型和视图java代码:

package com.example.javahm8.model;

import com.chg.ultimateprovider.Model;
import com.example.javahm8.ResourceTable;
import com.example.javahm8.holder.SampleItemViewHolder;

/**
 *  列表项
 */
public class SampleItem implements Model {

    private String name;

    public SampleItem(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int getResource(int position) {
        return ResourceTable.Layout_item_sample;
    }

    @Override
    public Class getHolderClass(int position) {
        return SampleItemViewHolder.class;
    }
}

 

package com.example.javahm8.holder;

import com.chg.ultimateprovider.EventTransmissionListener;
import com.chg.ultimateprovider.UltimateProvider;
import com.chg.ultimateprovider.ViewHolder;
import com.example.javahm8.ResourceTable;
import com.example.javahm8.model.SampleItem;

import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class SampleItemViewHolder extends ViewHolder<SampleItem> {

    private Text content;

    /**
     * 构造方法
     *
     * @param eventTransmissionListener 事件传输对象
     * @param component                 component
     * @param provider                  provider
     */
    public SampleItemViewHolder(EventTransmissionListener eventTransmissionListener, Component component, UltimateProvider provider) {
        super(eventTransmissionListener, component, provider);
        content = (Text) findComponentById(ResourceTable.Id_item_index);
    }


    @Override
    public void onDataBound() {
        content.setText(getModel().getName());
    }
}

 

 

7.第三方模块依赖和配置截图:

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
3
收藏 1
回复
举报
2条回复
按时间正序
/
按时间倒序
张荣超_九丘教育
张荣超_九丘教育

👍👍👍

回复
2021-2-15 19:39:10
AnBetter2021
AnBetter2021

学习了

回复
2021-3-10 09:40:59
回复
    相关推荐