touch ImageView
package com.example.touchdemo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView image;
float lastDistance = -1;
float x0 = 0;
float y0 = 0;
private float x;
private float y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imgage);
image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
Log.i("TAG","触摸开始......");
switch (action) {
//加上down方法,即实现单点触控,又实现放大缩小
case MotionEvent.ACTION_DOWN:
x = event.getRawX();
y = event.getRawX();
break;
case MotionEvent.ACTION_UP:
//注掉这两行,只支持放大缩小,不支持单点触控
/*image.setX(event.getX());
image.setY(event.getY());*/
break;
case MotionEvent.ACTION_MOVE:
//得到触摸点的个数
int count = event.getPointerCount();
Log.i("TAG","触摸点的个数:"+count);
if(count > 1){
Log.i("TAG", event.getX(0)+","+event.getY(0)+"|"+event.getX(1)+","+event.getY(1));
//获得两点的坐标差
float distanceX = event.getX(0) - event.getX(1);
float distanceY = event.getY(0) - event.getY(1);
//获得两点之间的距离
float betweenDistance = (float) Math.sqrt(distanceX*distanceX + distanceY*distanceY);
Log.i("TAG","当前两点之间的新距离:"+betweenDistance);
if(betweenDistance < 1){
lastDistance = betweenDistance;
}else if((betweenDistance - lastDistance) > 5){
//放大
lastDistance = betweenDistance;
//获得布局参数
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams();
params.width = (int) (image.getWidth()*1.1f);
params.height = (int) (image.getHeight()*1.1f);
image.setLayoutParams(params);
}else if((lastDistance - betweenDistance) > 5){
lastDistance = betweenDistance;
//获得布局参数
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams();
params.width = (int) (image.getWidth()*0.9f);
params.height = (int) (image.getHeight()*0.9f);
image.setLayoutParams(params);
}
}else if(count == 1){
//注掉这两行,只支持放大缩小,不支持单点触控
/*image.setX(event.getX());
image.setY(event.getY());*/
/**
* 即实现单点触控,又实现放大缩小
* */
//移动距离
float rawX = event.getRawX()-x;
float rawy = event.getRawY()-y;
//定义新的
int left = (int) (image.getLeft()+rawX);
int top = (int) (image.getTop()+rawy);
int right = (int) (image.getRight()+rawX);
int bottom = (int) (image.getBottom()+rawy);
//赋值
image.layout(left, top, right, bottom);
//改成新的按下作标
x=event.getRawX();
y=event.getRawY();
}
break;
default:
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.activity_main, menu);
return true;
}
}