zoukankan      html  css  js  c++  java
  • 05_响应多点触控


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.TextView;

    public class MultiTouchTest extends Activity implements OnTouchListener {
        StringBuilder builder = new StringBuilder();
        TextView textView;
        float[] x = new float[10];
        float[] y = new float[10];
        boolean[] touched = new boolean[10];

        private void updateTextView() {
            builder.setLength(0);
            for(int i = 0; i < 10; i++) {
                builder.append(touched[i]);
                builder.append(", ");
                builder.append(x[i]);
                builder.append(", ");
                builder.append(y[i]);
                builder.append("\n");
            }
            textView.setText(builder.toString());
        }
       
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            textView = new TextView(this);
            textView.setText("Touch and drag (multiple fingers supported)!");
            textView.setOnTouchListener(this);
            setContentView(textView);
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
         int curAction = event.getAction();
            int action = curAction & MotionEvent.ACTION_MASK;
            int pointerIndex = (curAction & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
            int pointerId = event.getPointerId(pointerIndex);

            switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                touched[pointerId] = true;
                x[pointerId] = (int)event.getX(pointerIndex);
                y[pointerId] = (int)event.getY(pointerIndex);
                break;

            case MotionEvent.ACTION_UP:         
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_CANCEL:
                touched[pointerId] = false;
                x[pointerId] = (int)event.getX(pointerIndex);
                y[pointerId] = (int)event.getY(pointerIndex);
                break;

            case MotionEvent.ACTION_MOVE:
                int pointerCount = event.getPointerCount();
                for (int i = 0; i < pointerCount; i++) {
                    pointerIndex = i;
                    pointerId = event.getPointerId(pointerIndex);
                    x[pointerId] = (int)event.getX(pointerIndex);
                    y[pointerId] = (int)event.getY(pointerIndex);
                }
                break;
            }
           
            updateTextView();      
            return true;
        }
    }

  • 相关阅读:
    ASP.NET 缓存技术分析
    asp.net中两款文本编辑器NicEdit和Kindeditor
    VS2005,VS2008,VS2010将ASP.NET网站编译成一个DLL文件
    公共的Json操作C#类
    怎么把100多个EXCEL文件合并成一个
    C# ToString()方法一些特殊用法
    C# 将数据导出到Execl汇总
    ASP.NET MVC中在Action获取提交的表单数据方法总结
    vue实现锚点定位跳转(当前页面跳转,url不变)
    es6 去除小数点后,不四舍五入
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060400.html
Copyright © 2011-2022 走看看