转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/51179151
本文出自【吴孝城的CSDN博客】
Android点击触控事件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fl" tools:context="cn.wuxiaocheng.multitouch.MainActivity"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </FrameLayout>MainActivity.java
package cn.wuxiaocheng.multitouch; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private FrameLayout root; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (FrameLayout) findViewById(R.id.fl); img = (ImageView) findViewById(R.id.img); root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: Log.d("MotionEvent", "按下"); break; case MotionEvent.ACTION_MOVE: Log.d("MotionEvent", "移动"); FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) img.getLayoutParams(); lp.leftMargin = (int) motionEvent.getX(); lp.topMargin = (int) motionEvent.getY(); img.setLayoutParams(lp); break; case MotionEvent.ACTION_UP: Log.d("MotionEvent", "松开"); break; } //要返回true,不然 return true; } }); } }
放大缩小
package cn.wuxiaocheng.multitouch; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends AppCompatActivity { private FrameLayout root; private float currentDistance;//当前距离 private float lastDistance = -1;//最终距离 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (FrameLayout) findViewById(R.id.fl); root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("ffffffffffffff"); break; case MotionEvent.ACTION_MOVE: Log.d("MotionEvent", "移动"); //当至少有两个触摸点时 if (motionEvent.getPointerCount() >= 2) { float offSetX = motionEvent.getX(0) - motionEvent.getX(1); float offSetY = motionEvent.getY(0) - motionEvent.getY(1); //先算X轴的平方,再算Y轴的平方,然后开平方,得到两点的距离 currentDistance = (float) Math.sqrt(offSetX * offSetX + offSetY * offSetY); if (lastDistance < 0) { lastDistance = currentDistance; } else { //放大或缩小的像素大于5个像素时 if (currentDistance - lastDistance > 5) { Log.d("触摸", "放大"); lastDistance = currentDistance; } else if (lastDistance - currentDistance > 5) { Log.d("触摸", "缩小"); lastDistance = currentDistance; } } } break; case MotionEvent.ACTION_UP: Log.d("MotionEvent", "松开"); break; } //要返回true,不然 return true; } }); } }
图片放大缩小
package cn.wuxiaocheng.multitouch; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private FrameLayout root; private ImageView img; private float currentDistance;//当前距离 private float lastDistance = -1;//最终距离 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (FrameLayout) findViewById(R.id.fl); img = (ImageView) findViewById(R.id.img); root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("ffffffffffffff"); break; case MotionEvent.ACTION_MOVE: Log.d("MotionEvent", "移动"); //当至少有两个触摸点时 if (motionEvent.getPointerCount() >= 2) { float offSetX = motionEvent.getX(0) - motionEvent.getX(1); float offSetY = motionEvent.getY(0) - motionEvent.getY(1); //先算X轴的平方,再算Y轴的平方,然后开平方,得到两点的距离 currentDistance = (float) Math.sqrt(offSetX * offSetX + offSetY * offSetY); if (lastDistance < 0) { lastDistance = currentDistance; } else { //放大或缩小的像素大于5个像素时 if (currentDistance - lastDistance > 5) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) img.getLayoutParams(); lp.width = (int) (1.1 * img.getWidth()); lp.height = (int) (1.1 * img.getHeight()); img.setLayoutParams(lp); lastDistance = currentDistance; } else if (lastDistance - currentDistance > 5) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) img.getLayoutParams(); lp.width = (int) (0.9 * img.getWidth()); lp.height = (int) (0.9 * img.getHeight()); img.setLayoutParams(lp); lastDistance = currentDistance; } } } break; case MotionEvent.ACTION_UP: Log.d("MotionEvent", "松开"); break; } //要返回true,不然 return true; } }); } }