Android触控
来源:互联网 发布:paxos算法 实现 编辑:程序博客网 时间:2024/05/22 05:09
转载请标明出处: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; } }); }}
0 0
- android 触控
- Android触控
- android 触控 MotionEvent
- Android触控-MotionEvent(一)
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- 前端开发学习笔记(一)
- AVI Player with Playmaker actions 使用UGUI显示视频以及控制播放视频 (二)
- 深度学习方法:受限玻尔兹曼机RBM(一)基本概念
- Java NIO系列1:从操作系统的角度剖析I/O
- 树的前中后非递归遍历
- Android触控
- 线程示例:多线程卖票
- EditText弹出软键盘,空白处隐藏
- 深度学习方法:受限玻尔兹曼机RBM(二)网络模型
- 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
- 【数据库】4种视图的特点及应用场景
- CSS hack
- 导航栏设置的颜色和实际的颜色有色差
- 文件同步rsync