Android_Touch_Test
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的更多相关文章
随机推荐
- wxpython线程安全的方法
wx中实现了3个线程安全的函数.如果在线程中,直接访问并更新主线程的UI,会遇到问题,有时候阻塞UI或者更新不起作用,有时严重的话会引起python崩溃. 三个安全线程如下: wx.PostEvent ...
- 转:CentOS6.3配置yum源
全新以最小化包安装了64位的CentOS6.3系统,作为本地的Web服务器使用,现记录全过程第二步,配置网易163的yum源 1. 下载repo文件 下载地址:http://mirrors.16 ...
- protractor protractor.conf.js [launcher] Process exited with error code 1 undefined:1190
y@y:karma-t01$ protractor protractor.conf.js [launcher] Process exited with error code undefined: vl ...
- cf C. Hamburgers
http://codeforces.com/contest/371/problem/C 二分枚举最大汉堡包数量就可以. #include <cstdio> #include <cst ...
- h.264的POC计算
本文参考自http://wenku.baidu.com/link?url=ZPF0iSKzwLQg_8K02pnnd_-Zd6ISnsOGWsGYb98ucLkELZO4nOv-X-v2GKLzI3r ...
- 主流屏幕对比:IPS/LTPS/CGS/IGZO/AMOLED
IPS.LTPS.CGS.IGZO.AMOLED都是什么屏幕又有什么区别?目前的手机屏幕技术实在太多,本文旨在介绍各种面板以及屏幕技术,便于大家更好地进行区分. 近年来手机屏幕技术层出不穷,早在几年前 ...
- DC-DC转换器原理与应用
DC/DC转换器为转变输入电压后,有效输出固定电压的电压转换器.DC/DC转换器分为三类:升压型DC/DC转换器.降压型DC/DC转换器以及升降压型DC/DC转换器.根据需求可采用三类控制.PWM控制 ...
- Autodesk 开源 3D 打印机
Autodesk 开源 3D 打印机 Autodesk在知识共享-署名-相同方式共享许可证下公开了其3D打印机Ember的树脂.机械设计.电路图纸的细节,在GNU GPL许可证下公开了固件.打印机运行 ...
- -_-#【Angular】定义服务
AngularJS学习笔记 <!DOCTYPE html> <html ng-app="Demo"> <head> <meta chars ...
- Hadoop MapReduce InputFormat/OutputFormat
InputFormat import java.io.IOException; import java.util.List; /** * InputFormat describes the input ...