Android多点触摸交互处理,放大缩小图片
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/frame"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img" android:background="@drawable/paoku00199"/> </FrameLayout>
java
private float currentDistance=0; private float lastDistance=-1; private FrameLayout frame; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frame= (FrameLayout) findViewById(R.id.frame); final ImageView img= (ImageView) findViewById(R.id.img); frame.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //触摸按下 Log.i("MotionEvent","ACTION_DOWN"); break; } switch (event.getAction()){ case MotionEvent.ACTION_MOVE: //触摸移动 Log.i("MotionEvent","ACTION_MOVE"); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) img.getLayoutParams(); if(event.getPointerCount()==1) { //单个触摸点,并拖动图片 System.out.println(String.format("X:%f,Y:%f", event.getX(), event.getY())); layoutParams.leftMargin = (int) event.getX()-10; layoutParams.topMargin = (int) event.getY()-10; img.setLayoutParams(layoutParams); } System.out.println("pointer count 触摸点个数"+event.getPointerCount());//触摸点个数 //多个触摸点的坐标 //手势放大缩小图片 if (event.getPointerCount()>=2){ System.out.println(String.format("多个触摸点的坐标X1:%f;Y1:%f", event.getX(0), event.getX(0))); float offsetX=event.getX(0)-event.getX(1); 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){ //5px;有误差 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) img.getLayoutParams(); System.out.println("放大"); lp.width= (int) (1.1f*img.getWidth()); lp.height=(int) (1.1f*img.getHeight()); img.setLayoutParams(lp); lastDistance=currentDistance; //放大 }else if (lastDistance-currentDistance>5){ lastDistance=currentDistance; //缩小 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) img.getLayoutParams(); lp.width= (int) (0.9f*img.getWidth()); lp.height=(int) (0.9f*img.getHeight()); img.setLayoutParams(lp); } } } break; } switch (event.getAction()){ case MotionEvent.ACTION_UP: //触摸弹起 Log.i("MotionEvent","ACTION_UP"); break; } return true;//true:需要催发后续事件;false:不需要须发后续事件(只能执行一次) } }); }注意:
private float currentDistance=0; private float lastDistance=-1;要onClick写在外面;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。