Android多点触摸操作
2015-10-14 17:04
507 查看
Android中的多点触摸可以实现图片的放大、缩小和旋转等处理
主要通过setOnTouchListener方法来监听用户的触摸事件,通过event.getX(0)和 event.getX(1)来获取第一个触控点和第二个触控点的x轴(或者y轴)坐标,
接下来在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP这几种情况中来对获取到的x轴或者y轴进行处理,就能
实现我们想要的效果了。
下面这个小Demo实现了对图片的放大和缩小处理:
xml代码:
主要通过setOnTouchListener方法来监听用户的触摸事件,通过event.getX(0)和 event.getX(1)来获取第一个触控点和第二个触控点的x轴(或者y轴)坐标,
接下来在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP这几种情况中来对获取到的x轴或者y轴进行处理,就能
实现我们想要的效果了。
下面这个小Demo实现了对图片的放大和缩小处理:
package com.example.administrator.translation; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; public class MainActivity extends ActionBarActivity { private RelativeLayout layout; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = (RelativeLayout) findViewById(R.id.layout); iv = (ImageView) findViewById(R.id.iv); layout.setOnTouchListener(new View.OnTouchListener() { float currentDistance; float lastDistance = -1; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: //判断几个触控点 if (event.getPointerCount() >= 2) { //两点之间x的坐标差 float offsetX = event.getX(0) - event.getX(1); //两点之间y的坐标差 float offsetY = event.getY(0) - event.getY(1); //两点之间的距离 currentDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY); if (lastDistance < 0) { //没有缩放 lastDistance = currentDistance; } else { if (currentDistance - lastDistance > 5) {//放大 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams(); lp.width = (int) (1.1f * iv.getWidth()); lp.height = (int) (1.1f * iv.getHeight()); iv.setLayoutParams(lp); lastDistance = currentDistance; } else if (currentDistance - lastDistance < -5) {//缩小 int currentIvWidth = iv.getWidth(); int currentIvHeight = iv.getHeight(); if (currentIvWidth > 50 && currentIvHeight >50) { RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams(); lp.width = (int) (0.9f * iv.getWidth()); lp.height = (int) (0.9f * iv.getHeight()); iv.setLayoutParams(lp); lastDistance = currentDistance; } } } } break; case MotionEvent.ACTION_UP: break; } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:src="@mipmap/a" android:layout_height="wrap_content" /> </RelativeLayout>
相关文章推荐
- Android 抖动动画类似ios长按动画
- android 数据存储之 SharedPreference
- apk间数据传递(android:shareUserId 介绍、Contenet provider)
- Android 省电开发之 JobSchedule
- UsbAccessory
- Android沉浸式状态栏
- Android编译系统分析
- Android ListView (1) -- 使用自定义的分割线
- Android数据持久化存储-SharedPreferences
- Android开发中需要注意的几点
- androidstudio 常用快捷键
- Android最新版支付宝钱包的实例源码
- android CompoundDrawablesWithIntrinsicBounds用法
- 如何更新Android SDK
- Android Studio生成Jni头文件的方法
- Android自定义View系列之可伸缩的TextView
- android获取SDCard中照片并按照时间顺序显示
- 揭开Android神秘面纱之Activity
- Android之TelephonyManager
- Android onCreate 详解