【发布时间】:2015-08-12 11:15:38
【问题描述】:
当我在我的 LG G6 上接听电话时,我必须单击绿色圆圈的中心并向外滚动到伴随颜色变化的外圈才能接听电话。我想在我的应用中实现类似的东西。
我有一个附有 onTouchListener 的按钮 (300*20dp)。从左到右滑动它会触发一个事件并向我的服务器发送一个 POST 请求,否则不会发送任何请求。
我目前拥有的是
Button
android:layout_marginTop="10dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Swipe Right to Purchase!"
android:id="@+id/btnSend"
我在MainActivity中的代码如下
Button btnSend;
btnSend=(Button)findViewById(R.id.btnSend);
btnSend.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
myGestDetector.onTouchEvent(event);
//Log.d(" VIEW : ",v.getLeft()+" "+v.getRight());
return true;
}
});
myGestDetector = new GestureDetector(this, new
GestureDetector.SimpleOnGestureListener()
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if (e2.getX()- e1.getX()>900) {
Log.d(TAG, "Left to Right swipe performed");
btnSend.setText("Order has been placed!");
}else btnSend.setText("Swipe More!");
return true;
}
@Override
public boolean onDown(MotionEvent e1)
{
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
return true;
}
});
这确保用户必须水平滑动大量。现在我将 900 乘以 0.75*(view.getRight-view.getLeft)。如果我滑动较少,文本会按预期更改为“滑动更多”,但如果我滑动超过 900 次,那么在日志中我会得到 p>
08-12 16:09:20.521 25598-25598/com.brandlabs.varun.yocity D/Motion 是:执行了从左到右的滑动
单次滑动会导致多次发送多个 POST 请求。我的问题是
1) 我怎样才能阻止它/将其限制为一个事件?如果我取消注释此 Log.d(" VIEW : ",v.getLeft()+" "+v.getRight());然后,对于每次滑动,我都会在日志中获得多条这些行。
2) 为什么会这样?
3) 使用此功能时,当用户移动手指时,我可以在 action_move 期间跟踪它并更改按钮的颜色吗?比如显示进度。
【问题讨论】:
标签: android performance android-layout swipe android-progressbar