1.1.触摸事件(必会)
? 拖拽
? XML配置
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/image" />
? Java代码
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
imageView.setOnTouchListener(new MyOnTouchListener());
}
private class
MyOnTouchListener implements OnTouchListener
{
private floatx;
private floaty;
private Matrix currentMatrix
= new Matrix(); // 用来操作图片的矩阵
private Matrix oldMatrix
= new Matrix();
public boolean
onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 按下时
x = event.getX(); // 获取x轴坐标
y = event.getY(); // 获取y轴坐标
oldMatrix.set(imageView.getImageMatrix()); // 记住位置
break;
case MotionEvent.ACTION_MOVE:
// 移动时
currentMatrix.set(oldMatrix);
// 设置成按下时记住的位置
currentMatrix.postTranslate(event.getX() - x, event.getY() - y); // 改变位置
break;
}
imageView.setImageMatrix(currentMatrix); // 移动图片
return true;
}
}
}
? 多点触摸
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
imageView.setOnTouchListener(new MyOnTouchListener());
}
private class
MyOnTouchListener implements
OnTouchListener {
private floatx; // 图片移动前的x轴坐标
private floaty; // 图片移动前的y轴坐标
private Matrix currentMatrix
= new Matrix(); // 用来移动图片的矩阵
private Matrix oldMatrix
= new Matrix(); // 图片移动前的矩阵
private inttype; // 操作类型, 一根手指触摸还是两根手指触摸
private floatstart; // 第二根手指按下时的距离
private floatend; // 两根手指移动后的距离
private PointF point; // 放大时的中心点
public boolean
onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
type = 1;
x = event.getX();
y = event.getY();
oldMatrix.set(imageView.getImageMatrix());
break;
case MotionEvent.ACTION_MOVE:
currentMatrix.set(oldMatrix);
if (type ==
1) { // 1根手指触摸
currentMatrix.postTranslate(event.getX() - x, event.getY() - y);
}
else { // 2跟手指触摸
end = countDistance(event); // 计算结束时距离
float scale = end
/ start; // 计算缩放比例
currentMatrix.postScale(scale, scale, point.x, point.y); // 设置缩放
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
type = 2;
start = countDistance(event); // 计算开始时距离
point = countPoint(event); // 计算中心点
oldMatrix.set(imageView.getImageMatrix());
break;
}
imageView.setImageMatrix(currentMatrix); // 改变图片
return true;
}
}
public float
countDistance(MotionEvent event) {
float a = event.getX(1) - event.getX(0); // x轴距离
float b = event.getY(1) - event.getY(0); // y轴距离
return (float)
Math.sqrt(a * a + b * b); // 勾股定理
}
public PointF countPoint(MotionEvent event) {
float x = (event.getX(0) + event.getX(1)) / 2;
// x轴中间点
float y = (event.getY(0) + event.getY(1)) / 2;
// y轴中间点
return new
PointF(x, y);
}
}