1 package com.example.qjm3662.android_touch_test;
  2 
  3 import android.os.Bundle;
  4 import android.support.design.widget.FloatingActionButton;
  5 import android.support.design.widget.Snackbar;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.support.v7.widget.Toolbar;
  8 import android.util.Log;
  9 import android.view.MotionEvent;
 10 import android.view.View;
 11 import android.view.Menu;
 12 import android.view.MenuItem;
 13 import android.widget.ImageView;
 14 import android.widget.LinearLayout;
 15 
 16 public class MainActivity extends AppCompatActivity {
 17 
 18     private String TAG = "MainActivity";
 19     private LinearLayout root;
 20     private ImageView iv;
 21     @Override
 22     protected void onCreate(Bundle savedInstanceState) {
 23         super.onCreate(savedInstanceState);
 24         setContentView(R.layout.activity_main);
 25 
 26         root = (LinearLayout) findViewById(R.id.container);
 27         iv = (ImageView) findViewById(R.id.iv);
 28 
 29         root.setOnTouchListener(new View.OnTouchListener() {
 30 
 31             float offsetX;
 32             float offsetY;
 33             float curruntDistance;      //当前距离
 34             float lastDistance = -1;    //前一次的距离
 35             @Override
 36             public boolean onTouch(View v, MotionEvent event) {
 37                 switch(event.getAction()){
 38                     case MotionEvent.ACTION_DOWN:
 39                         Log.e(TAG,"down");
 40                         break;
 41                     case MotionEvent.ACTION_UP:
 42                         Log.e(TAG,"up");
 43                         break;
 44                     case MotionEvent.ACTION_MOVE:
 45                         if(event.getPointerCount() > 1){
 46                             
 47                             //两个触摸点之间的距离
 48                             offsetX = event.getX(0) - event.getX(1);
 49                             offsetY = event.getY(0) - event.getY(1);
 50 
 51                             //当前距离
 52                             curruntDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
 53 
 54                             //第一次给lastDistance赋值
 55                             if(lastDistance < 0){
 56                                 lastDistance = curruntDistance;
 57                             }else{
 58                                 //获取ImageView的layoutParms
 59                                 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
 60                                 
 61                                 //与上一次相比距离增大(5px是为了考虑操作误差)
 62                                 if(curruntDistance - lastDistance > 5){
 63                                     
 64                                     //放大图片操作
 65                                     layoutParams.width = (int) (1.1f*iv.getWidth());
 66                                     layoutParams.height = (int) (1.1f*iv.getHeight());
 67 
 68                                     //操作完务必给lastDistance重新赋值
 69                                     lastDistance = curruntDistance;
 70                                     iv.setLayoutParams(layoutParams);
 71                                     Log.e(TAG,"放大");
 72                                 }
 73                                 //与上一次相比距离减小(5px是为了考虑操作误差)
 74                                 else if(lastDistance - curruntDistance > 5){
 75                                     layoutParams.width = (int) (0.9f*iv.getWidth());
 76                                     layoutParams.height = (int) (0.9f*iv.getHeight());
 77                                     lastDistance = curruntDistance;
 78                                     iv.setLayoutParams(layoutParams);
 79                                     Log.e(TAG,"缩小");
 80                                 }
 81                             }
 82                         }
 83 
 84 //                        Log.e(TAG, "move");
 85 //                        //多点触控
 86 //                        Log.e(TAG,"Point count : " + event.getPointerCount());
 87 //                        if(event.getPointerCount() > 1){
 88 //                            Log.e(TAG,String.format("x1:%f,y1:%f\nx2:%f,y2:%f",event.getX(0),event.getY(0),event.getX(1),event.getY(1)));
 89 //                        }
 90                         
 91                         
 92 //                        //获取单个触摸点
 93 //                        LinearLayout.LayoutParams lp;
 94 //                        lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
 95 //                        Log.e(TAG,String.format("x:%f,y:%f",event.getX(),event.getY()));
 96 //                        lp.leftMargin = (int) event.getX();
 97 //                        lp.topMargin = (int) event.getY();
 98 //                        iv.setLayoutParams(lp);
 99                         break;
                 }
                 return true;
             }
         });
     }
 
 }  

Android_Touch_Test的更多相关文章

随机推荐

  1. 使用UIImagePickerController时3DTouch引起的Crash问题的解决--备用

    一.crash的场景 程序中用到UIImagePickerController时,如果在IPhone6S上运行APP,当forceTouch 一个图片时程序会crash,并附带如下crash mess ...

  2. JS学习之页面加载

    1.window.opener.location.reload();     意思是让打开的父窗口刷新.window.opener指的是本窗口的父窗口,window.opener.location.h ...

  3. Qt Quick分组属性案例

    import QtQuick 2.4 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4 import QtQuick.Win ...

  4. 利用Tree命令生成磁盘文件列表

    命令原型:D:/>tree /? 以图形显示驱动器或路径的文件夹结构.TREE [drive:][path] [/F] [/A]/F 显示每个文件夹中文件的名称./A 使用 ASCII 字符,而 ...

  5. 查看Linux某个进程打开的文件句柄(file descriptor)数量

    先找到进程的pid 然后: lsof -p [pid] | wc -l 或者 ls /proc/[pid]/fd | wc -l 查看系统总共使用中的文件描述符数量: lsof | wc -l ref ...

  6. 令人头痛的ExtJS日期时间控件

    1 缘由 Ext提供了日期.时间的控件,但没有将日期和时间组合在一起的控件.在网上搜索时,有前辈创建或重写了时间相关的类,并提供了源码.不得不说那位作者对 extjs 框架理解得很透彻,虽然不知道他当 ...

  7. 【HDOJ】1277 全文检索

    AC自动机,静态数组,动态分配TLE. /* 1277 */ #include <iostream> #include <cstdio> #include <cstrin ...

  8. 维基百科上—数据仓库、数据挖掘、OLAP三者之间的区别

    数据仓库可以作为数据挖掘和OLAP等分析工具的资料来源,由于存放于数据仓库中的资料,必需经过筛选与转换,因此可以避免分析工具使用错误的资料,而得到不正确的分析结果. 数据挖掘和OLAP同为分析工具,其 ...

  9. (转载)PHP array_merge() 函数

    (转载)http://www.w3school.com.cn/php/func_array_merge.asp PHP Array 函数 定义和用法 array_merge() 函数把两个或多个数组合 ...

  10. spring3.1的BeanFactory与Quartz1.8整合

    spring的applicationContext.xml配置文件: 加入 <bean id="myJob" class="org.springframework. ...