【问题标题】:Touch event: when finger is removed from the item触摸事件:当手指从项目上移开时
【发布时间】:2013-01-04 10:43:55
【问题描述】:

当我从物品上移开手指时,我需要实现功能。所以我需要一些活动。

***Scenario:*** 

1. Touch the image view using finger.
2. Remove the finger.
3. Now implement the functionality.

我想要第 2 步的事件回调。

如果存在一些预定义的事件,请建议名称。

【问题讨论】:

    标签: android events touch


    【解决方案1】:

    好的。当您触摸屏幕并移除手指事件时调用:

    1. ACTION_DOWN - 当你第一次触摸时
    2. ACTION_MOVE - 当您在屏幕上移动手指时
    3. ACTION_UP - 当您将手指从屏幕上移开时

    祝你好运!

    【讨论】:

      【解决方案2】:

      对于这种情况,您可以为 ImageView 实现OnTouchListener

      yourImageView.setOnTouchListener(new OnTouchListener () {
        public boolean onTouch(View view, MotionEvent event) {
      
          if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
            Log.d("TouchTest", "Touch down");
          } 
          else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
            Log.d("TouchTest", "Touch up");
          }
        }
      }
      

      【讨论】:

      • 注意,如果要在MotionEvent.ACTION_DOWN之后获取MotionEvent.ACTION_UP,则应该在ACTION_DOWN时返回true
      【解决方案3】:
      public class TestTouchEvents extends Activity implements OnTouchListener {
        ImageView imageView;
        Bitmap bitmap;
        Canvas canvas;
        Paint paint;
      
      
        @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      
          imageView = (ImageView) this.findViewById(R.id.ImageView);
      
          Display currentDisplay = getWindowManager().getDefaultDisplay();
          float dw = currentDisplay.getWidth();
          float dh = currentDisplay.getHeight();
      
          bitmap = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
          canvas = new Canvas(bitmap);
          paint = new Paint();
          paint.setColor(Color.GREEN);
          imageView.setImageBitmap(bitmap);
          imageView.setOnTouchListener(this);
        }
      
        public boolean onTouch(View v, MotionEvent event) {
          int action = event.getAction();
      
          switch (action) {
          case MotionEvent.ACTION_DOWN:
            // Do Something
            Log.d("Touch", "Touch down");
            break;
      
          case MotionEvent.ACTION_MOVE:
      
            // Do Something
            Log.d("Touch", "Touch move");
            //imageView.invalidate();
      
            break;
      
          case MotionEvent.ACTION_UP:
      
            // Do Something
            Log.d("Touch", "Touch up");
            //imageView.invalidate();
            break;
      
          case MotionEvent.ACTION_CANCEL:
      
            Log.d("Touch", "Touch cancel");
            break;
      
          default:
            break;
          }
          return true;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        相关资源
        最近更新 更多