Launcher3 长按Hotseat图标,显示删除角标(红底白杠杠用于删除图标或者显示应用未读消息数量)

本文主要是介绍Launcher3 长按Hotseat图标,显示删除角标(红底白杠杠用于删除图标或者显示应用未读消息数量),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于Android 13,Launcher3实现需求:

1. 长按Hotseat的图标显示红色删除角标

2. 点击角标,删除图标并保存到Database

3.点击其他地方,取消编辑hotseat图标模式

实现效果:

实现原理:

 

图标是由BubbleTextView来是实现的,是一个TextView,要增加角标应该有几种思路:

1.TextView可以设置 left top bottom right 4个drawable,top已经用作实际的icon,这个应该布局不了

2. 修改背景background,在适当的时候修改background?

3.重写TextView的onDraw,适当的时候在原来的基础上画出额外的角标

查看Android13原生的Launcher代码发现 BubbleTextView的onDraw已经有DotRenderer的实现,显示的应该是应用通知,因此模仿这个实现思路即可。

查看源码发现实际使用的是DoubleShadowBubbleTextView.java,调用到的是ondraw调用的是drawWithoutDot

因此在BubbleTextView.java中添加代码即可实现:

 

protected void drawWithoutDot(Canvas canvas) {super.onDraw(canvas);drawMyDotIfNecessary(canvas);
}protected void drawMyDotIfNecessary(Canvas canvas){if(!mIsDeleteHidden) {Paint paint = new Paint();paint.setColor(Color.RED); // 角标颜色paint.setStyle(Paint.Style.FILL);canvas.drawCircle(getWidth() - 30f, 30f, 30f, paint);}
}

 

接下来应该需要实现在什么时候显示圆点操作,

比方说长按某一个hotseat里图标,则所有的view都添加这个圆点

长按图标事件可以得到,在什么时候取消呢?点击图标非圆点处取消。

接下来实现,点击圆点事件:把点击事件传到mHotseatController里删除并更新hotseat里的view

 step1: 在BubbleTextView.java里实现onDraw画图,捕捉点击圆点事件,实现显示隐藏角标接口

private boolean mIsDeleteHidden = true;//Kevin.Ye added
private boolean mIsDownInDotErea = false;//
private Rect mRectDotBounds = null;//
private OnDotClkListener mOnDotClkListener = null;
//Kevin.Ye added end@Overridepublic boolean onTouchEvent(MotionEvent event) {// ignore events if they happen in padding areaif(cancelDotIfNecessary(event))//added by Kevin.Ye case when Dot is shownreturn true;//endif (event.getAction() == MotionEvent.ACTION_DOWN&& shouldIgnoreTouchDown(event.getX(), event.getY())) {return false;}if (isLongClickable()) {super.onTouchEvent(event);mLongPressHelper.onTouchEvent(event);// Keep receiving the rest of the eventsreturn true;} else {return super.onTouchEvent(event);}}
private boolean cancelDotIfNecessary(MotionEvent event){if(isMyDotHidden())return false;switch (event.getAction()){case MotionEvent.ACTION_DOWN:if(isTouchInDotErea((int)event.getX(),(int)event.getY())) {mIsDownInDotErea = true;return true;}break;case MotionEvent.ACTION_UP:if(mIsDownInDotErea){mIsDownInDotErea = false;if(isTouchInDotErea((int)event.getX(),(int)event.getY())){onClkMyDot();return true;}else{Log.d("dot","touch up elsewhere");}}break;}return false;}private boolean isTouchInDotErea(int x,int y){Log.d("dot","touch x:"+x+" y:"+y);if(mRectDotBounds == null) {mRectDotBounds = new Rect(getWidth() - 60, 0, getWidth(), 60);}return mRectDotBounds.contains(x,y);}private void onClkMyDot(){Log.d("dot","onClkMyDot");if(mOnDotClkListener != null)mOnDotClkListener.onClkIconDot(BubbleTextView.this);}public interface OnDotClkListener{void onClkIconDot(View view);}public void setOnDotClkListener(OnDotClkListener onDotClkListener){mOnDotClkListener = onDotClkListener;}
/**draw my dot for deleting or added icon Kevin.Ye*/private boolean isMyDotHidden(){return mIsDeleteHidden;}public void setDeleteDotHidden(boolean hide){mIsDeleteHidden = hide;invalidate();}protected void drawMyDotIfNecessary(Canvas canvas){if(!mIsDeleteHidden) {Paint paint = new Paint();paint.setColor(Color.RED); // 角标颜色        paint.setStyle(Paint.Style.FILL);canvas.drawCircle(getWidth() - 30f, 30f, 30f, paint);}}

 

step2: HotseatController.java(注意本类是源码没有的),实现显示角标、取消显示角标、响应删除图标三个接口

private boolean mIsInDeletingMode = false;public boolean isInDeletingMode(){return mIsInDeletingMode;}public void showHotseatDeleteDot(){mIsInDeletingMode = true;Hotseat hs = mLauncher.getHotseat();int gridCount = getGridCount(mLauncher);//ArrayList<View> views = new ArrayList<>();for (int i = 0; i < gridCount; i++) {int cx = hs.getCellXFromOrder(i);int cy = hs.getCellYFromOrder(i);View v = hs.getShortcutsAndWidgets().getChildAt(cx, cy);if (hs.isOccupied(cx, cy)) {if (v != null) {if(v instanceof BubbleTextView){Log.d("dot","v instanceof BubbleTextView!!!");((BubbleTextView)v).setDeleteDotHidden(false);((BubbleTextView)v).setOnDotClkListener(mOnDotClkListener);}}}}}public void cancelDeletingMode(){mIsInDeletingMode = false;Hotseat hs = mLauncher.getHotseat();int gridCount = getGridCount(mLauncher);//ArrayList<View> views = new ArrayList<>();for (int i = 0; i < gridCount; i++) {int cx = hs.getCellXFromOrder(i);int cy = hs.getCellYFromOrder(i);View v = hs.getShortcutsAndWidgets().getChildAt(cx, cy);if (hs.isOccupied(cx, cy)) {if (v != null) {if(v instanceof BubbleTextView){Log.d("dot","v instanceof BubbleTextView!!!");((BubbleTextView)v).setDeleteDotHidden(true);}}}}}BubbleTextView.OnDotClkListener mOnDotClkListener = new BubbleTextView.OnDotClkListener() {@Overridepublic void onClkIconDot(View view) {Log.d("dot","HotseatController onClkMyDot");Hotseat hs = mLauncher.getHotseat();hs.removeView(view);//Log.d("dot","view.getTag() :"+view.getTag().toString());Object tag = view.getTag();WorkspaceItemInfo info = tag instanceof WorkspaceItemInfo ? (WorkspaceItemInfo) tag : null;if(info != null) {mLauncher.getModelWriter().deleteItemFromDatabase(info,null);}}};

step3:Launcher.java中增加调用取消编辑hotseat模式(隐藏角标)

private HotseatController mHotseatController = null;//Kevin.YemHotseatController = new HotseatController(this.getApplicationContext(),this);//Kevin.Ye/***Return HotseatController Kevin.Ye added*/
public HotseatController getHotseatController(){return mHotseatController;
}@Overridepublic boolean startActivitySafely(View v, Intent intent, ItemInfo item) {/*Kevin.Ye added for cancel deleting mode*/if(getHotseatController().isInDeletingMode()){getHotseatController().cancelDeletingMode();return true;}}

step4:src/com/android/launcher3/Workspace.java 中的接口DragView beginDragShared

实现长按进入Hotseat图标编辑模式(显示删除角标)

        if (child.getParent() instanceof ShortcutAndWidgetContainer) {mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();}if (child instanceof BubbleTextView && !dragOptions.isAccessibleDrag) {//Kevin.Ye added for showing hotseat edit modeboolean bStartLongPressAction = true;if(child.getParent() instanceof ShortcutAndWidgetContainer)if(child.getParent().getParent() instanceof Hotseat){mLauncher.getHotseatController().showHotseatDeleteDot();bStartLongPressAction = false;}//add endif(bStartLongPressAction)dragOptions.preDragCondition = ((BubbleTextView) child).startLongPressAction();}

后续应该用加载drawable的方式来代替drawCircle画图,删除图标后应该重新排布热座上的图标。

这篇关于Launcher3 长按Hotseat图标,显示删除角标(红底白杠杠用于删除图标或者显示应用未读消息数量)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1116045

相关文章

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima