Button的抖动效果

实现Button的抖动其实很简单,需要写个动画,将Button抖动的坐标标示出来。 然后放在res/anim包 。

至于Activity的代码就很简单了。给Button设置点击事件,将动画加载进来。

Animation anim = AnimationUtils.loadAnimation(MyCollectionActivity.this, R.anim.myanim);

anim动画的代码:

 empty.startAnimation(anim);   //empty 是Button<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/cycle"
>
<translate
android:fromXDelta="0"
android:toXDelta="-10"
android:fromYDelta="0"
android:toYDelta="-10"
android:duration="300"/>
<translate
android:fromXDelta="0"
android:toXDelta="10"
android:fromYDelta="0"
android:toYDelta="-10"
android:startOffset="300"
android:duration="300"/>
<translate
android:fromXDelta="0"
android:toXDelta="-10"
android:fromYDelta="0"
android:toYDelta="10"
android:startOffset="600"
android:duration="300"/>
<translate
android:fromXDelta="0"
android:toXDelta="10"
android:fromYDelta="0"
android:toYDelta="10"
android:startOffset="900"
android:duration="300"/>
</set>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现按钮点击后的抖动效果,可以使用QPropertyAnimation类和QParallelAnimationGroup类。 以下是一个简单的示例代码: ```python from PyQt5.QtCore import QPropertyAnimation, QEasingCurve, QRect from PyQt5.QtGui import QPalette, QColor from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QParallelAnimationGroup class MyButton(QPushButton): def __init__(self, *args, **kwargs): super(MyButton, self).__init__(*args, **kwargs) self.animation_group = QParallelAnimationGroup(self) self.hover_animation = QPropertyAnimation(self, b"color") self.click_animation = QPropertyAnimation(self, b"geometry") self.hover_animation.setStartValue(QColor(0, 0, 0)) self.hover_animation.setEndValue(QColor(255, 255, 255)) self.hover_animation.setDuration(300) self.hover_animation.setEasingCurve(QEasingCurve.InOutCirc) self.click_animation.setDuration(50) self.click_animation.setEasingCurve(QEasingCurve.InOutCirc) self.animation_group.addAnimation(self.hover_animation) self.animation_group.addAnimation(self.click_animation) self.clicked.connect(self.animate) def enterEvent(self, event): self.hover_animation.setDirection(QPropertyAnimation.Forward) self.animation_group.start() def leaveEvent(self, event): self.hover_animation.setDirection(QPropertyAnimation.Backward) self.animation_group.start() def animate(self): self.click_animation.setStartValue(self.geometry()) self.click_animation.setEndValue(self.geometry().adjusted(2, 2, -2, -2)) self.click_animation.start() class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.button = MyButton("Click me", self) self.button.setGeometry(50, 50, 100, 50) self.show() if __name__ == "__main__": app = QApplication([]) palette = QPalette() palette.setColor(QPalette.Button, QColor(255, 0, 0)) app.setPalette(palette) window = MainWindow() app.exec_() ``` 在这个示例中,我们自定义了一个MyButton类,它继承自QPushButton类,并实现了enterEvent、leaveEvent和animate方法。当鼠标移入按钮时,我们会启动一个颜色渐变动画;当鼠标移出按钮时,我们会再次启动这个动画,但是方向相反。当按钮被点击时,我们会启动一个位置动画,让按钮的位置稍微抖动一下,以达到抖动效果。 注意,这个示例代码使用了QPalette来设置应用程序的主题色。如果你想使用不同的颜色,可以自行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值