TouchGFX之Button

2024-03-29 02:44
文章标签 touchgfx button

本文主要是介绍TouchGFX之Button,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

TouchGFX中的按钮是一种感应触控事件的控件,能够在按钮被按下/释放时发送回调

代码

#ifndef TOUCHGFX_ABSTRACTBUTTON_HPP
#define TOUCHGFX_ABSTRACTBUTTON_HPP
#include <touchgfx/Callback.hpp>
#include <touchgfx/events/ClickEvent.hpp>
#include <touchgfx/widgets/Widget.hpp>namespace touchgfx
{
/* 按键抽象接口类 */
class AbstractButton : public Widget
{
public:/* 构造函数 */AbstractButton() : Widget(), action(), pressed(false){setTouchable(true);	//打开触摸功能}/* 点击事件处理函数 */virtual void handleClickEvent(const ClickEvent& event);/* 设置点击事件回调函数 */void setAction(GenericCallback<const AbstractButton&>& callback){action = &callback;}/* 执行点击事件回调函数 */virtual void executeAction(){if (action && action->isValid()){action->execute(*this);}}/* 获取按钮状态 */virtual bool getPressedState() const{return pressed;}protected:GenericCallback<const AbstractButton&>* action; //点击事件回调函数bool pressed; //按键状态(按下/松开)
};}#endif
#include <touchgfx/widgets/AbstractButton.hpp>namespace touchgfx
{
/* 点击事件回调函数 */
void AbstractButton::handleClickEvent(const ClickEvent& event)
{const bool wasPressed = pressed;pressed = (event.getType() == ClickEvent::PRESSED);/* 按钮状态改变,重绘 */if ((pressed && !wasPressed) || (!pressed && wasPressed)){invalidate();}/* 按下触发回调 */if (wasPressed && (event.getType() == ClickEvent::RELEASED)){executeAction();}
}
}

自定义按键

#ifndef BUTTONWITHCOLOR
#define BUTTONWITHCOLOR
#include <touchgfx/widgets/AbstractButton.hpp>using namespace touchgfx;class ButtonWithColor : public AbstractButton
{
public:ButtonWithColor(): AbstractButton(), upColor(0xFFFFFF), downColor(0xFFFFFF), alpha(255){}	virtual void draw(const Rect& invalidatedArea) const;virtual void setColor(const colortype colorReleased, const colortype colorPressed){upColor = colorReleased;downColor = colorPressed;}virtual Rect getSolidRect() const;void setAlpha(uint8_t newAlpha){alpha = newAlpha;}uint8_t getAlpha() const{return alpha;}colortype getCurrentlyDisplayedColor() const{return (pressed ? downColor : upColor);}virtual void invalidateContent() const{if (alpha > 0){Widget::invalidateContent();}}private:colortype upColor;colortype downColor;uint8_t alpha;
};#endif
#include <touchgfx/Drawable.hpp>
#include <touchgfx/hal/HAL.hpp>
#include <touchgfx/lcd/LCD.hpp>
#include <gui/common/ButtonWithColor.hpp>void ButtonWithColor::draw(const Rect& invalidatedArea) const
{const colortype color = pressed ? downColor : upColor;Rect dirty = invalidatedArea;translateRectToAbsolute(dirty);HAL::lcd().fillRect(dirty, color, alpha);
}Rect ButtonWithColor::getSolidRect() const
{Rect solidRect;if(alpha == 255){solidRect.width = rect.width;solidRect.height = rect.height;}return solidRect;
}
#ifndef SCREENVIEW_HPP
#define SCREENVIEW_HPP#include <gui_generated/screen_screen/screenViewBase.hpp>
#include <gui/screen_screen/screenPresenter.hpp>
#include <gui/common/ButtonWithColor.hpp>class screenView : public screenViewBase
{
public:screenView();virtual ~screenView() {}virtual void setupScreen();virtual void tearDownScreen();
protected:private:ButtonWithColor button;
};#endif // SCREENVIEW_HPP
#include <gui/screen_screen/screenView.hpp>
#include <touchgfx/Color.hpp>screenView::screenView()
{}void screenView::setupScreen()
{screenViewBase::setupScreen();button.setColor(Color::getColorFromRGB(0x00, 0xFF, 0x00), Color::getColorFromRGB(0x00, 0xFF, 0xFF));button.setPosition(100,100,100,100);add(button);
}void screenView::tearDownScreen()
{screenViewBase::tearDownScreen();
}

运行模拟器

左边松开 -> 右边按下

这篇关于TouchGFX之Button的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Flutter Button使用

Material 组件库中有多种按钮组件如ElevatedButton、TextButton、OutlineButton等,它们的父类是于ButtonStyleButton。         基本的按钮特点:         1.按下时都会有“水波文动画”。         2.onPressed属性设置点击回调,如果不提供该回调则按钮会处于禁用状态,禁用状态不响应用户点击。

小程序button控件上下边框的显示和隐藏

问题 想使用button自带的loading图标功能,但又不需要button显示边框线 button控件有一条淡灰色的边框,在控件上了样式 border:none; 无法让button边框隐藏 代码如下: <button class="btn">.btn{border:none; /*一般使用这个就是可以去掉边框了*/} 解决方案 发现button控件有一个伪元素(::after

Android中圆角Button实现

在android开发中,Button是使用很频繁的一种控件,而android提供的原生Button是很规矩的矩形外观,有时候缺乏美感,而相反,圆角按钮则可以提升美感。那么,我们如何设计实现出圆形按钮呢?     话不多说,请看实现! 在drawable目录下新建名称如“shape.xml”的文件 <pre class="html" name="code"><pr

【Puppeteer】‘left‘ is already pressed, ‘${button}‘ is already pressed 的解决办法

解决过程如下 这是我原来的代码,不管我怎么修改,都一直会出现 'left' is already pressed 这个错误 找了很多资料 搜了 很多网站都 找不到解决办法 async function dragAndDrop(page, canvasSelector, startX, startY, endX, endY) {const startCoordinates = await ge

button 提交后再次刷新页面

button,input type=button按钮在IE和w3c,firefox浏览器区别 当在IE浏览器下面时,button标签按钮,input标签type属性为button的按钮是一样的功能,不会对表单进行任何操作。 但是在W3C浏览器,如Firefox下就需要注意了,button标签按钮会提交表单,而input标签type属性为button不会对表单进行任何操作。 解决方案:

猫猫学iOS(四十四)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配

猫猫分享,必须精品 原创文章,欢迎转载。转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 效果: 注意图里面了吗,其实那个效果做起来真的很简单,在iOS中苹果给我们封装的很好,关键是那个按钮 系统的按钮的图片是在左边的,这里我们需要把他调整到右边,然后呢需要我们自己做一下操作。 代码: 话不多说,先

ABAP Dialog Radio Button

额.妈了个巴子,整了一天,才发现,原来Dialog 的Radio Button 是要右键去设置组的 我就说为什么不行咧 误区:我以为是属性那里的组去设置的

安卓实战中防止Button的频繁点击多次执行点击事件和点击切换文字

哈哈,今天来写两种button在安卓开发实战的对应效果处理,也是对button的点击事件触发过程的处理。 开发实战中button事件处理3种情况: 情况一:button点击触发倒计时情况二:button频繁点击(特别是触发网络请求),本人公司是金融公司很容易产生两笔交易的情况。情况三:button点击时不断轮流切换文字 看效果: 情况一:button触发倒计时 请移步我的:频繁点击

Unity(2022.3.41LTS) - UI详细介绍- Button(按钮)TMP

目录 零.简介 一、基本功能与重要性 二、属性和设置详解 三、使用方法深入探讨 四、优化和注意事项 零.简介 在 Unity 中,按钮(Button)是用户界面中非常重要的交互元素之一。以下是对 Unity 中按钮的更详细介绍: 一、基本功能与重要性 触发特定操作:按钮的核心作用是当用户点击时,触发预先设定的一系列操作或事件。这可以是切换场景、执行游戏逻辑、显示信息等

uni-app小程序button按钮的open-type有哪些好用的API

在uni-app小程序中,button组件的open-type属性用于指定按钮的开放能力,即按钮点击时触发的微信小程序特定功能。以下是一些常用的open-type值及其对应的API功能: contact:打开客服会话,如果用户在会话中点击消息卡片后返回小程序,可以从bindcontact回调中获得具体信息。 share:触发用户转发,使用前建议先阅读《微信小程序用户转发》。 getPhon