package com.example.touchdemo;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.FrameLayout;import android.widget.FrameLayout.LayoutParams;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity { PRivate FrameLayout flt_touch; private ImageView iv_img; //当前的距离 float correntDistance; //最后上次的距离 float lastDistance=-1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flt_touch=(FrameLayout) findViewById(R.id.flt_touch); iv_img=(ImageView) findViewById(R.id.iv_img); flt_touch.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //触摸按下 Log.d("touch", "触摸按下");// Toast.makeText(MainActivity.this, "触摸按下", Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_MOVE: //触摸移动 Log.d("touch", "触摸移动"); //获取触摸点打印出来 Log.d("touch", "x: "+event.getX()+", y: "+event.getY());// Toast.makeText(MainActivity.this, "触摸移动", Toast.LENGTH_SHORT).show(); // 重新设置控件布局 FrameLayout.LayoutParams lp=(LayoutParams) iv_img.getLayoutParams(); lp.leftMargin=(int) event.getX(); lp.topMargin=(int) event.getY(); iv_img.setLayoutParams(lp); //获取多个触摸点 event.getX(1) event.getX(2) Log.d("touch", "x1: "+event.getX(0)+", y1: "+event.getY(0)); if(event.getPointerCount()==2){ Log.d("touch", "x2: "+event.getX(1)+", y2: "+event.getY(1)); } //获取触摸点总数量 Log.d("touch", String.valueOf(event.getPointerCount())); //放大或缩小图片 if(event.getPointerCount()>=2){ float offsetX=event.getX(0)-event.getX(1); float offsetY=event.getY(0)-event.getY(1); correntDistance=(float)Math.sqrt(offsetX*offsetX+offsetY*offsetY); Log.d("touch", correntDistance+""); if(lastDistance<0){ lastDistance=correntDistance; }else{ //理论是0,但是安卓手指按下,总有一些偏大偏小变化,所以这里值是5以上比较合试 if(correntDistance-lastDistance>5){ Log.d("touch", "放大"); FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams(); lpImg.width=(int) (iv_img.getWidth()*1.1f); lpImg.height=(int) (iv_img.getHeight()*1.1f); iv_img.setLayoutParams(lp); lastDistance=correntDistance; }else if(lastDistance-correntDistance>5){ Log.d("touch", "缩小"); FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams(); lpImg.width=(int) (iv_img.getWidth()*0.9f); lpImg.height=(int) (iv_img.getHeight()*0.9f); iv_img.setLayoutParams(lp); lastDistance=correntDistance; } } } break; case MotionEvent.ACTION_UP: //触摸弹起 Log.d("touch", "触摸弹起");// Toast.makeText(MainActivity.this, "触摸弹起", Toast.LENGTH_SHORT).show(); break; default: break; } return true; } }); }}<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/flt_touch" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>h</FrameLayout>
新闻热点
疑难解答