图片缩放,多点触摸响应
- 布局设置监听事件,setOnTouchListener
三种状态,按下,移动,抬起
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
- 获取图片的位置
- FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams();//获得布局对象
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams(); lp.leftMargin = (int) iv.getX(); lp.topMargin = (int) iv.getY(); iv.setLayoutParams(lp);
获取多点坐标
float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); currentDistance = (float) Math.sqrt(x * x + y * y); if (Distance
java
package com.lgq.scale; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 iv; float Distance = -1; float currentDistance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (FrameLayout) findViewById(R.id.root); iv = (ImageView) findViewById(R.id.iv); root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // System.out.println("down"); break; case MotionEvent.ACTION_MOVE: FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams(); if (event.getPointerCount()