我试图在longpress上使imageView增大尺寸,并在我取消它后使其恢复正常。
public class MainActivity extends Activity {
private class Erjan_gestures extends SimpleOnGestureListener{
@Override
public void onLongPress(MotionEvent event) {
Log.wtf("x", "long press occurring");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.wtf("x", "LONG PRESS - action down");
image.getLayoutParams().height = 400;
image.getLayoutParams().width = 400;
RelativeLayout.LayoutParams for_answer1 = new RelativeLayout.LayoutParams(300, 600);
image.requestLayout();
break ;
case MotionEvent.ACTION_UP:
//THIS CASE IS NEVER REACHED
Log.wtf("x", "LONG PRESS - action up");
image.getLayoutParams().height = oldH;
image.getLayoutParams().width = oldW;
image.requestLayout();
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image= (ImageView)findViewById(R.id.card);
button=(Button)findViewById(R.id.button);
oldW = 500;
oldH = 600;
gestureDetector = new GestureDetector(new Erjan_gestures());
gestureDetector.setIsLongpressEnabled(true);
image.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
Log.wtf("x", "action up is detected");
}
Log.wtf("x", "I m a card, and i know you click on me!");
if(gestureDetector.onTouchEvent(event)) {
Log.wtf("x", "this is onTouch(View v, MotionEvent event)");
return true;
}
else return false;
}
});
}
但是,我的imageView确实检测到longpress并执行了
ACTION_DOWN
,但从未进入ACTION_UP
的longpress()
部分。这是因为不应该将longpress分为action_up,down吗?
长按手势本身仅包含按(又称为ACTION_DOWN)?
为什么longPress中的action_up永远不会执行?
最佳答案
这确实是因为longpress并没有分为上下两部分,而是只具有“触发”动作。
实际上,ACTION_DOWN甚至是错误的术语。 longpress与ACTION_DOWN无关,因为当用户按下按钮时ACTION_DOWN不会立即触发。只有在特定的延迟按下后才触发。因此DELAY_PASSED左右将是一个更合适的名称。
请注意,正常按下仍会继续,并且其ACTION_UP仍会触发。
关于android - longPress不回复ACTION_UP-简单的手势监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32710445/