我有3-4个视图,如TextView,Button,ImageView和UI上的View 现在我想只在特定视图上放大/缩小。 我已经实现了这个功能但是当我捏视图时会闪烁。 我的代码是: - 我已经按照this教程进行了参考
public void zoom(View v, Float scaleX, Float scaleY, PointF pivot) {
v.setPivotX(pivot.x);
v.setPivotY(pivot.y);
v.setScaleX(scaleX);
v.setScaleY(scaleY);
}
Matrix matrix = new Matrix();
// We can be in one of these 3 states
static final int DRAG = 1;
// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;
float redundantXSpace, redundantYSpace;
float width, height;
static final int CLICK = 3;
float saveScale = 1f;
float right, bottom, origWidth, origHeight, bmWidth, bmHeight;
// Definition of the touch states
static final int NONE = 0;
static final int ONE_FINGER_DRAG = 1;
static final int TWO_FINGERS_DRAG = 2;
int mode = NONE;
PointF firstFinger;
float lastScrolling;
float distBetweenFingers;
float lastZooming;
PointF mid = new PointF();
float oldDist = 1f;
PointF oldDistPoint = new PointF();
// We can be in one of these 2 states
static final int ZOOM = 1;
static final int MIN_FONT_SIZE = 10;
static final int MAX_FONT_SIZE = 50;
float scalex, scaley = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: // Start gesture
firstFinger = new PointF(event.getX(), event.getY());
mode = ONE_FINGER_DRAG;
break;
case MotionEvent.ACTION_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN: // second finger
oldDist = spacing(event);
distBetweenFingers = spacing(event);
// the distance check is done to avoid false alarms
if (distBetweenFingers > 5f) {
mode = TWO_FINGERS_DRAG;
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == TWO_FINGERS_DRAG) {
PointF newDist = spacingPoint(event);
float newD = spacing(event);
// Log.e(TAG, "newDist=" + newDist);
float[] old = new float[9];
float[] newm = new float[9];
// Log.e(TAG, "x=" + old[0] + ":&:" + old[2]);
// Log.e(TAG, "y=" + old[4] + ":&:" + old[5]);
float scale = newD / oldDist;
float scalex = newDist.x / oldDistPoint.x;
float scaley = newDist.y / oldDistPoint.y;
zoom(v,scale, scale,new PointF(0, 0));
}
break;
}
return true;
}
private void scroll(float pan) {
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private PointF spacingPoint(MotionEvent event) {
PointF f = new PointF();
f.x = event.getX(0) - event.getX(1);
f.y = event.getY(0) - event.getY(1);
return f;
}