java - 布局上的 onTouch 事件每次点击只注册一个触摸
问题描述
我有以下问题:我创建了一个扩展 ConstraintLayout 的新类,并且我正在覆盖 onTouchEvent 函数来为布局设置动画。
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(this, "scaleX", 0.85f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(this, "scaleY", 0.85f);
scaleDownX.setDuration(100);
scaleDownY.setDuration(100);
AnimatorSet scaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);
scaleDown.start();
break;
case MotionEvent.ACTION_CANCEL:
Log.d("arne", "CANCEL");
// Beide Cases lösen das Event aus
// ACTION_CANCEL minimiert den Button auch, wenn man den Knopf nicht loslässt, sondern weg wischt
case MotionEvent.ACTION_UP:
ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(this, "scaleX", 1f);
ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(this, "scaleY", 1f);
scaleUpX.setDuration(100);
scaleUpY.setDuration(100);
AnimatorSet scaleUp = new AnimatorSet();
scaleUp.play(scaleUpX).with(scaleUpY);
scaleUp.start();
break;
}
return super.onTouchEvent(event);
}
这对按钮完美无缺,如果我按住按钮,事件会在一秒钟内触发多次。
但是在 ConstraintLayout 上它只触发一次,就像 onClick 事件一样。
这在我的理解中有点奇怪。
你有这个问题的解决方案吗?
解决方案
好的,我终于找到了解决方案。
你只需要设置你的布局可点击。
android:clickable="true"
推荐阅读
- javascript - WooCommerce 加/减数量。产品不更新
- git - Git 子树,子模块 - 使用另一个项目中的项目
- arrays - 月级别和年级别的聚合,也在 MongoDB 中找到平均值
- python - Python Beautifulsoup 查找元素
- vercel - 是否有 Vercel 重写规则来摆脱 Facebook 的 fbclid 参数?
- python - 评估自我内部功能
- java - 使用 WSS4J (SOAP) 在 CXF WebServices 中实现身份验证
- javascript - 如何在 Vue.js 组件中使用 Pixi.js?
- excel - 无法使用正确的日期格式填充 Excel 单元格
- reactjs - 重新加载反应应用程序时显示的Django Rest url页面