比较重要的方法:
(1)root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) {。。。。。。。。。。。。。。。。。。。。。 在这里view指的就是调用了setOnTouchListener方法的root.官方文档是这样说的。
(2)RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams()。这个方法中得到的是某个view的layoutParams。(3)event.getPointerCount()。这个方法中是计算有多少个触摸点在手机上。
(4)event.getX(0) , event.getX(1)里面的参数指定的是第几个触摸点的X的坐标(5)图片缩放的基本原理:首先我们指定是只有俩个触摸点。然后我们判断来个触摸点之间的距离。如果现在的俩个点之间的距离比先前的俩个触摸点之间的距离大的话,那么就进行放大,否则的话,就是缩小了。核心代码如下:if (event.getPointerCount() >= 2) { 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) { lastdistance=currentdistance; RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.width = (int) (1.1f * imageView.getWidth()); layoutParams.height = (int) (1.1f * imageView.getHeight()); imageView.setLayoutParams(layoutParams); System.out.println("放大"); }else if (lastdistance - currentdistance > 5) { lastdistance=currentdistance; RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.width = (int) (0.9f * imageView.getWidth()); layoutParams.height = (int) (0.9f * imageView.getHeight()); imageView.setLayoutParams(layoutParams); System.out.println("缩小"); } }布局文件中,只有一个ImageView.MainActivity中的内容:import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MotionEvent; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity { private RelativeLayout root; private ImageView imageView; private float currentdistance; private float lastdistance=-1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (RelativeLayout) findViewById(R.id.root); imageView = (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: if (event.getPointerCount() >= 2) { 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) { lastdistance=currentdistance; RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.width = (int) (1.1f * imageView.getWidth()); layoutParams.height = (int) (1.1f * imageView.getHeight()); imageView.setLayoutParams(layoutParams); System.out.println("放大"); }else if (lastdistance - currentdistance > 5) { lastdistance=currentdistance; RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.width = (int) (0.9f * imageView.getWidth()); layoutParams.height = (int) (0.9f * imageView.getHeight()); imageView.setLayoutParams(layoutParams); System.out.println("缩小"); } } /* RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.topMargin= (int) event.getX(); layoutParams.leftMargin = (int) event.getY(); imageView.setLayoutParams(layoutParams); System.out.println("move");*/ break; case MotionEvent.ACTION_UP: System.out.println("up"); break; } return true; } }); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @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); } }