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. Substrings

    hdu1238:http://acm.hdu.edu.cn/showproblem.php?pid=1238 题意:给你n个串,求一个子串,这个子串在所有串中都出现,或者在逆串中出现.求最大的这个子串 ...

  2. java代码调用rtx发送提醒消息

    http://www.cnblogs.com/qstar/archive/2012/02/03/Astar.html 借用一下!好东西

  3. 打patch p0 p1区别

    如何使用patch 对于一个patch文件,有两种常用使用方法: 1.代码:cat new-patch | patch -p0 2.代码:patch -p0 < new-patch   patc ...

  4. BZOJ1709: [Usaco2007 Oct]Super Paintball超级弹珠

    1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 324  Solved: ...

  5. Pascal's Triangle II —LeetCode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. Java---常用基础面试知识点

    综合网上的一点资源,给大家整理了一些Java常用的基础面试知识点,希望能帮助到刚开始学习或正在学习的学员. 1.抽象 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方 ...

  7. 转自http://blog.sina.com.cn/daylive——C++ STL map

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! 1.map最基本的构造函数: map<string ...

  8. Java图像灰度化的实现过程解析

    概要 本文主要介绍了灰度化的几种方法,以及如何使用Java实现灰度化.同时分析了网上一种常见却并不妥当的Java灰度化实现,以及证明了opencv的灰度化是使用“加权灰度化”法 24位彩色图与8位灰度 ...

  9. Purchase Document Open Interface(PDOI)

    PO模块也有自己的接口表,多用于把其他业务系统在Oracle EBS系统生成采购订单记录. Table Name Description Type PO_HEADERS_INTERFACE This ...

  10. MVC4 Razor视图下使用iframe加载RDLC报表

    MVC视图下默认是不支持服务器端控件的,所以,为了能够通过report viewer控件加载报表,需要在MVC视图添加嵌入的页面. 起初在stackoverflow上找到一个解决方案,见这里.不过这里 ...