HarmonyOS实战—实现抖音点赞和取消点赞效果

系统 OpenHarmony
鸿蒙HarmonyOS是面向多智能终端、全场景的分布式操作系统,为消费者提供跨终端、全场景智慧时代的无缝体验。

[[416604]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

一、双击点赞 和 双击取消点赞

如:在抖音中双击屏幕之后就可以点赞,小红心就会变亮。

把白色和红色的心形图片复制到 media 下

需要图片的可以自取,下面白色图片由于没有背景,所以显示的是白色的,下载后鼠标点击就能看见了

因为要双击屏幕才能点赞,所以还要给布局组件id

代码实现:

ability_main

<?xml version="1.0" encoding="utf-8"?> 
<DirectionalLayout 
    ohos:id="$+id:dl" 
    xmlns:ohos="http://schemas.huawei.com/res/ohos" 
    ohos:height="match_parent" 
    ohos:width="match_parent" 
    ohos:alignment="center" 
    ohos:orientation="vertical"
 
    <Image 
        ohos:id="$+id:img" 
        ohos:height="match_content" 
        ohos:width="match_content" 
        ohos:image_src="$media:white" 
        ohos:background_element="cyan" 
        > 
 
    </Image> 
 
 
 
</DirectionalLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

MainAbilitySlice

package com.xdr630.listenerapplication6.slice; 
 
import com.xdr630.listenerapplication6.ResourceTable; 
import ohos.aafwk.ability.AbilitySlice; 
import ohos.aafwk.content.Intent; 
import ohos.agp.components.Component; 
import ohos.agp.components.DirectionalLayout; 
import ohos.agp.components.Image; 
 
public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener { 
 
    Image image; 
 
    @Override 
    public void onStart(Intent intent) { 
        super.onStart(intent); 
        super.setUIContent(ResourceTable.Layout_ability_main); 
 
        //1.找到图片组件 
        image = (Image) findComponentById(ResourceTable.Id_img); 
        //找到铺满屏幕布局的对象 
        DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl); 
        //2.给布局添加双击事件 
        dl.setDoubleClickedListener(this); 
    } 
 
    @Override 
    public void onActive() { 
        super.onActive(); 
    } 
 
    @Override 
    public void onForeground(Intent intent) { 
        super.onForeground(intent); 
    } 
 
    //如果标记为false,表示没有点赞,此时把白色变为红色 
    //如果标记为true,表示已经点赞,再次双击后,会把红色变回白色 
 
    boolean flag = false
 
    @Override 
    public void onDoubleClick(Component component) { 
        //修改图片的红星就可以了,只需要用到image就行了,所以把image定为成员变量 
 
        if (flag){ 
            image.setImageAndDecodeBounds(ResourceTable.Media_white); 
            //取消点赞变成白色,也要把flag设置为false 
            flag = false
        }else
            image.setImageAndDecodeBounds(ResourceTable.Media_red); 
            //当启动项目的时候,flag初始值是false,就会走下面的else的代码,变成红色后就要把flag变成true了 
            flag = true
        } 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

运行:

双击屏幕点赞:

双击屏幕后取消点赞:

二、能否按照抖音的业务去实现呢?

业务分析:

  • 双击屏幕之后点赞。(上面已实现),再次双击屏幕之后,不会取消点赞,只有点击后红心之后才能取消点赞。
  • 单击红心也可以点赞,再次单击红心就会取消点赞

实现思路:

1.给最外层的布局添加双击事件,双击之后点赞,变成红色心。

如果已经被点赞,那么还是修改为红色心,相当于不做任何处理。

2.给图片添加单击事件。

如果没有点赞,单击之后,白色心变成红色心。

如果已经点赞了,单击之后,红色心变成白色心。

代码实现:

上面布局文件不变,MainAbilitySlice 如下:

  • 给布局添加双击事件,因为再次双击不会取消点赞,所以把else代码里设置为红色后就把 flag 取反去掉,就不会出现再次双击取消点赞了。
  • 给图片添加单击事件,因为涉及到点赞后为红色,再取消就变为白色,所以要把 flag 变为相反的操作
package com.xdr630.listenerapplication6.slice; 
 
import com.xdr630.listenerapplication6.ResourceTable; 
import ohos.aafwk.ability.AbilitySlice; 
import ohos.aafwk.content.Intent; 
import ohos.agp.components.Component; 
import ohos.agp.components.DirectionalLayout; 
import ohos.agp.components.Image; 
 
public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener, Component.ClickedListener { 
 
    Image image; 
 
    @Override 
    public void onStart(Intent intent) { 
        super.onStart(intent); 
        super.setUIContent(ResourceTable.Layout_ability_main); 
 
        //1.找到图片组件 
        image = (Image) findComponentById(ResourceTable.Id_img); 
        //找到铺满屏幕布局的对象 
        DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl); 
        //2.给布局添加双击事件 
        dl.setDoubleClickedListener(this); 
        //3.给图片添加单击事件 
        image.setClickedListener(this); 
    } 
 
    @Override 
    public void onActive() { 
        super.onActive(); 
    } 
 
    @Override 
    public void onForeground(Intent intent) { 
        super.onForeground(intent); 
    } 
 
    //如果标记为false,表示没有点赞,此时把白色变为红色 
    //如果标记为true,表示已经点赞,再次双击后,会把红色变回白色 
 
    boolean flag = false
 
 
    @Override 
    public void onDoubleClick(Component component) { 
        //修改图片的红星就可以了,只需要用到image就行了,所以把image定为成员变量 
 
        if (flag){ 
            image.setImageAndDecodeBounds(ResourceTable.Media_white); 
            //取消点赞变成白色,也要把flag设置为false 
            flag = false
        }else
            image.setImageAndDecodeBounds(ResourceTable.Media_red); 
            //当启动项目的时候,flag初始值是false,就会走下面的else的代码,此时设置为红色,把flag去掉,再次双击后就还是红色了 
        } 
    } 
 
    @Override 
    public void onClick(Component component) { 
        if (flag){ 
            image.setImageAndDecodeBounds(ResourceTable.Media_white); 
            flag = false
        }else
            image.setImageAndDecodeBounds(ResourceTable.Media_red); 
            flag = true
        } 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.

运行:

单击红心后:

再次单击红心:

双击屏幕后效果如下,再次双击屏幕就不会取消点赞了,只有点击小红心才能取消点赞

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-01-10 15:15:53

Redis点赞数据库

2015-07-17 10:41:59

点赞按钮

2020-12-03 11:00:29

Spring ClouRedis数据库

2021-04-14 14:28:14

Python点攒抖音

2022-08-11 09:30:52

transitionCSS

2020-05-11 17:00:30

点赞MySQLRedis

2020-02-10 16:02:29

东华软件

2015-07-21 15:22:20

点赞仿知乎按钮动画

2018-05-10 16:47:10

戴尔

2018-03-06 11:25:04

漫游流量运营商

2024-10-29 11:19:23

点赞系统同步

2015-06-02 10:22:43

CDN/Webluke

2019-08-27 08:51:36

计数数据库并发

2018-03-09 10:20:45

安卓Android P系统

2024-02-01 13:02:00

AI模型

2022-08-22 20:10:59

自定义计数器CSS

2020-09-17 18:31:54

戴尔

2017-11-13 10:56:46

2021-01-13 14:42:36

GitHub代码Java
点赞
收藏

51CTO技术栈公众号