我正在通过Android设备开发具有触摸屏控制功能的交互式视频装置。到目前为止,我想出了以下用于平移,缩放和平移限制的代码。但我无法弄清楚如何限制缩放(最小/最大)以及如何避免图像离开边界(它应该显示在整个屏幕上而没有任何偏移)。 谢谢你的帮助!
public boolean onTouchEvent(MotionEvent me) {
switch (me.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
startPoint.set(me.getX(), me.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(me);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(midPoint, me);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float transX = me.getX() - startPoint.x;
float transY = me.getY() - startPoint.y;
matrix.set(savedMatrix);
float[] f = new float[9];
matrix.getValues(f);
float viewWidth = 1317.0f + 1317.0f * ((f[Matrix.MSCALE_X] - 1) / 1.1365f );
float viewWidth2 = 772.0f + 772.0f * ((f[Matrix.MSCALE_X] - 1) / 1.998f);
float viewHeight = 1097.0f + 1097.0f * ((f[Matrix.MSCALE_X] - 1) / 1.463f);
float viewHeight2 = 749.0f + 749.0f * ((f[Matrix.MSCALE_X] - 1) / 2.994f);
if (f[Matrix.MTRANS_X] + transX < -viewWidth){
Log.e("", "left bound");
transX = -f[Matrix.MTRANS_X] -viewWidth;
}
if(f[Matrix.MTRANS_X] + transX > -viewWidth2 ){
transX = -f[Matrix.MTRANS_X] -viewWidth2;
}
if (f[Matrix.MTRANS_Y] + transY > viewHeight){
transY = -f[Matrix.MTRANS_Y] + viewHeight;
}
if(f[Matrix.MTRANS_Y] + transY < viewHeight2){
transY = -f[Matrix.MTRANS_Y] + viewHeight2;
}
matrix.postTranslate(transX, transY);
} else if (mode == ZOOM) {
float newDist = spacing(me);
if (newDist > 10f) {
Log.e("MainActivity", "Correction: " + Float.toString(newDist / newDist));
float transX = me.getX() - startPoint.x;
float transY = me.getY() - startPoint.y;
matrix.set(savedMatrix);
float scale = newDist / oldDist;
float[] f = new float[9];
matrix.getValues(f);
matrix.postScale(scale, scale, -startPoint.x+300.f, -startPoint.y+900.0f);
if (f[Matrix.MSCALE_X] >= 270.0f) {
matrix.postScale((270.0f)/(f[Matrix.MSCALE_X]/scale), (270.0f)/(f[Matrix.MSCALE_Y]*scale), 0f ,0f);
mode = DRAG;
savedMatrix.set(matrix);
midPoint(midPoint, me);
} else if (f[Matrix.MSCALE_X] < 0.29f) {
matrix.postScale((0.29f)/(f[Matrix.MSCALE_X]/scale), (0.29f)/(f[Matrix.MSCALE_Y]*scale), 0f ,0f);
mode = DRAG;
savedMatrix.set(matrix);
midPoint(midPoint, me);
}
}
}
break;
}
// Matrix
try {
socket = new DatagramSocket();
// socket.setBroadcast(true);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dataString = matrix.toShortString();
Log.e("Activity_Main", dataString);
try {
DatagramPacket packet = new DatagramPacket(dataString.getBytes(), dataString.length(), serverAddr, PORT);
socket.send(packet);
socket.close();
} catch (Exception e) {
}
return true;
}
@SuppressLint("FloatMath")
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 void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}