有时候需要对屏幕界面进行手势操作,这个时候就需要用到触屏监听接口。这个接口一般由View类及子类进行提供,通过设置监听函数,就可以完成对触屏的监听。代码如下所示:

       mChartView.setOnTouchListener(newOnTouchListener() {

              // 重写触屏监听函数,用作缩放处理

              @Override

              public booleanonTouch(View arg0, MotionEvent arg1) {

                   // TODOAuto-generated method stub

                   int nCount =arg1.getPointerCount();

                   int n =arg1.getAction();

                   if(arg1.getAction() == MotionEvent.ACTION_MOVE&&nCount == 1) {// 1个手指滑动

                       realDataChart.SetPanAble();

                   }

                   if((arg1.getAction()&MotionEvent.ACTION_MASK) ==MotionEvent.ACTION_POINTER_DOWN

                            && 2 ==nCount)// 标示2个手指

                   {

                       for (int i = 0; i< nCount; i++) {

                            float x =arg1.getX(i);

                            float y =arg1.getY(i);

                            Point ptPoint= new Point((int)x,(int)y);

                       }

                       int xlen =Math.abs((int)arg1.getX(0) - (int)arg1.getX(1));

                       int ylen =Math.abs((int)arg1.getX(0) - (int)arg1.getY(1));

                       nLenStart = Math.sqrt(xlen*xlen+ ylen*ylen);  // 计算起始两指之间的间距

                   }

                   else if((arg1.getAction()&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP

                            && 2 ==nCount) {

                       for (int i = 0; i< nCount; i++) {

                            float x =arg1.getX(i);

                            float y =arg1.getY(i);

                            Point ptPoint= new Point((int)x,(int)y);

                       }

                       int xlen =Math.abs((int)arg1.getX(0) - (int)arg1.getX(1));

                       int ylen =Math.abs((int)arg1.getX(0) - (int)arg1.getY(1));

                       nLenEnd = Math.sqrt(xlen*xlen+ ylen*ylen);    // 计算起始两指之间的间距

                       if (nLenEnd > nLenStart + 20) {

                            realDataChart.SetZoom(2);

                       }

                       else if (nLenEnd < nLenStart - 20) {

                            realDataChart.SetZoom(0.5f);

                       }

                   }

                  

                   return false;

              }

         });

  上述代码主要执行了一下两步操作。

1.      添加监听触屏事件的函数接口。

2.      在接口内,通过对手势进行判断,来执行相对应的操作。

更多相关文章

  1. android 处理txt文件类FileUtils,利用java反射机制访问非sdk类和
  2. Android第三方登录详解2
  3. 写在20111011:checkbox的onClick事件
  4. 为开发者准备的最佳 Android(安卓)函数库(2016年版)
  5. android SoundRecorder设计二
  6. android:自定义监听(简单)
  7. 搜索框(SearchView)用法
  8. Android(安卓)SurfaceFlinger 学习之路(七)----创建图形缓冲区Gr
  9. 我的第一个Android程序,BMI计算器,分享一下源码

随机推荐

  1. Android(安卓)颜色渲染(四) BitmapShader
  2. Android动态权限管理
  3. ue4 创建Android和ios ar应用
  4. android中清空一个表---类似truncate tab
  5. Android屏幕中隐藏标题栏
  6. android google map apiKey 相关操作
  7. Android单选框(RadioButton)
  8. android 动态向Gallery中添加图片及倒影&
  9. android 中文 api (71) —— BluetoothServ
  10. android移植详解