ZoomView 是一个可以实现单指平移,双指缩放和旋转的view,内部包含的view也会跟着一起变化,示例继承自RelativeLayout,也可以根据自己需要继承自所有View类的子类。
效果如下:
核心逻辑代码如下所示,需要的同学直接copy下面的代码到工程中就可以直接使用:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
public class ZoomView extends RelativeLayout {
// 属性变量
private float translationX; // 移动X
private float translationY; // 移动Y
private float scale = 1; // 伸缩比例
private float rotation; // 旋转角度
// 移动过程中临时变量
private float actionX;
private float actionY;
private float spacing;
private float degree;
private int moveType; // 0=未选择,1=拖动,2=缩放
public ZoomView(Context context) {
this(context, null);
}
public ZoomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZoomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setClickable(true);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
moveType = 1;
actionX = event.getRawX();
actionY = event.getRawY();
break;
case MotionEvent.ACTION_POINTER_DOWN:
moveType = 2;
spacing = getSpacing(event);
degree = getDegree(event);
break;
case MotionEvent.ACTION_MOVE:
if (moveType == 1) {
translationX = translationX + event.getRawX() - actionX;
translationY = translationY + event.getRawY() - actionY;
setTranslationX(translationX);
setTranslationY(translationY);
actionX = event.getRawX();
actionY = event.getRawY();
} else if (moveType == 2) {
scale = scale * getSpacing(event) / spacing;
setScaleX(scale);
setScaleY(scale);
rotation = rotation + getDegree(event) - degree;
if (rotation > 360) {
rotation = rotation - 360;
}
if (rotation < -360) {
rotation = rotation + 360;
}
setRotation(rotation);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
moveType = 0;
}
return super.onTouchEvent(event);
}
// 触碰两点间距离
private float getSpacing(MotionEvent event) {
//通过三角函数得到两点间的距离
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
// 取旋转角度
private float getDegree(MotionEvent event) {
//得到两个手指间的旋转角度
double delta_x = event.getX(0) - event.getX(1);
double delta_y = event.getY(0) - event.getY(1);
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
}
使用方式如下,是不是非常简单:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.zhangxq.zoomview.ZoomView
android:id="@+id/zoomView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/test" />
</com.zhangxq.zoomview.ZoomView>
</RelativeLayout>
版权声明:本文为zxq614原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。