文档开发者中心
我的
|

HarmonyOS

云备份 CBR

在智慧屏上抢红包的Demo和代码(基于鸿蒙模拟器)



效果演示


说下遇到的问题

1 在XML中使用PositionLayout布局增加子组件后,子组件使用setContentPosition(x,y)造成定位失败。

2 无法父组件无法删除子组件,removeComponent()无效。

3 无法隐藏组件setVisibility();值是4或者8都无效。demo需要删除元素,无奈只能设置元素的width,height为0


以下是代码片段


<?xml version="1.0" encoding="utf-8"?>
<PositionLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
                ohos:width="match_parent"
                ohos:height="match_parent"
                ohos:id="$+id:p">
    <Text   ohos:id="$+id:fenshu"
            ohos:text="分数:0"
            ohos:background_element="#66FF00"
            ohos:padding="10vp">
    </Text>
</PositionLayout>


 


--------------------------------------------


package com.example.tv.slice;


import com.example.tv.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;


import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


//1920*1080
public class MainAbilitySlice extends AbilitySlice {
    int count = 0;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_layout1);
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onStart");
        Context context = this;
        PositionLayout positionLayout = (PositionLayout) findComponentById(ResourceTable.Id_p);
        Text text = (Text) findComponentById(ResourceTable.Id_fenshu);
        text.setContentPosition(1750, 0);
        text.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        text.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                Random random = new Random();
                int rFenshu = random.nextInt(100);
                Button button = new Button(context);
                button.setWidth(100);
                button.setHeight(100);
                button.setText("红包:" + rFenshu);
                button.setClickedListener((c) -> {
                    button.setWidth(0);
                    button.setHeight(0);
                    count += rFenshu;
                    text.setText("分数:" + count);
                });
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(new RgbColor(255, 0, 0));
                button.setBackground(shapeElement);
                int i1 = random.nextInt(1720);
                button.setContentPosition(i1, 0);
                positionLayout.addComponent(button);
                AnimatorProperty animatorProperty = button.createAnimatorProperty();
                animatorProperty.moveToY(700).setDuration(4000);
                animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {


                    }


                    @Override
                    public void onStop(Animator animator) {


                    }


                    @Override
                    public void onCancel(Animator animator) {


                    }


                    @Override
                    public void onEnd(Animator animator) {
                        button.setWidth(0);
                        button.setHeight(0);
                    }


                    @Override
                    public void onPause(Animator animator) {


                    }


                    @Override
                    public void onResume(Animator animator) {


                    }
                });
                animatorProperty.start();
            }
        }, 1000,1000);
    }


    @Override
    protected void onActive() {
        super.onActive();
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onActive");
    }


    @Override
    protected void onInactive() {
        super.onInactive();
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onInactive");
    }


    @Override
    protected void onBackground() {
        super.onBackground();
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onBackground");
    }


    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onForeground");
    }


    @Override
    protected void onStop() {
        super.onStop();
          System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onStop");
    }
}


作者:顶风少年


点赞
8
收藏
8
回复
10
分享
举报
浏览1585 编辑于2020-11-24 08:58未知归属地
全部评论
最多点赞
最新发布
最早发布

  好专业呀,看不懂

5楼回复于2020-11-08 02:07 未知归属地

试了一下   removeComponent() 有效果 setVisibility(2)是消失

10楼回复于2020-11-28 14:27 未知归属地

风采依旧,看好你  

9楼回复于2020-11-12 02:17 未知归属地

  写的挺好呀   

8楼回复于2020-11-11 02:15 未知归属地

要的就是这个,很棒 

7楼回复于2020-11-10 01:55 未知归属地

  好棒,学起来  

6楼回复于2020-11-09 02:27 未知归属地

一起顶,让更多的人看到

4楼回复于2020-11-07 02:02 未知归属地

有你的日子更精彩  

3楼回复于2020-11-06 02:37 未知归属地

必须顶起来,好内容 

2楼回复于2020-11-05 03:40 未知归属地

今天晚上八点的直播课已经报名了,看看能不能跟着老师做出来一个小游戏

https://harmonyos.51cto.com/activity/17#hw

1楼编辑于2020-11-04 06:52 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。