当前位置:   article > 正文

鸿蒙支持java,鸿蒙Java开发模式9:鸿蒙牛气冲天动画及鸿蒙UltimateProvider组件

鸿蒙 请求java

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

6617b5804a290e8b9784e88cb59e2174.png

82a7744e435e1753dd7bb6a9fbca5ff1.png

cea678e40859bc06593784015913f906.png

a6d466ad596a4e9a9855e46d1a81dd2f.png

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

a0cb6db1212490b5307a28cb61c6dc62.png

9f8efddf0b428e8baa60075deb03e427.png

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

9117b9361eac9d320ead4f1e4651e4c3.png

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 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 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.跳转页面的布局文件代码:

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="#CD5C5C"

ohos:orientation="vertical">

ohos:id="$+id:list_container"

ohos:layout_alignment="horizontal_center"

ohos:height="match_parent"

ohos:width="match_parent"

/>

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">

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"/>

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 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

{

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 {

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.第三方模块依赖和配置截图:

fe946411f7d6370444a995f5937a75ad.png

20084ef0fcbc21ece683582e23f65a8a.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/259915
推荐阅读
相关标签
  

闽ICP备14008679号