我试图让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
,但是从未在 longpress()
中获得 ACTION_UP
部分 .
这是因为longpress不应分为action_up,down?
长按手势本身只包括按(又名ACTION_DOWN)?为什么longPress中的action_up永远不会被执行?
2 回答
这确实是因为longpress不分为向下和向上,而是仅具有“触发”动作 .
实际上,ACTION_DOWN甚至是一个不正确的术语 . longpress与ACTION_DOWN没有任何关系,因为当用户按下按钮时,ACTION_DOWN不会被触发 . 它只会在特定延迟下降后触发 . DELAY_PASSED左右因此是一个更合适的名称 .
请注意,正常按下仍然继续,并且其ACTION_UP仍将触发 .
这是使用
Holder
类和onTouch()
方法提供的解决方案 .