问题
I've been trying to achieve
drag and drop (Move) Api < 11
Resize (Larger than original Image)
Is there any external library from which we can resize and move the image in View?
External Materials, Api, Library and Links would be welcome.
回答1:
Playing around with Matrix()
and onTouchListener()
had done by work.
public class CustomizeActivity extends Activity {
private static final String TAG = "Touch" ;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
public static PointF mid = new PointF();
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public static int mode = NONE;
float oldDist;
private float[] matrixValues = new float[9];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
iv=new ImageView(this);
iv.setOnTouchListener(t);
}
OnTouchListener t=new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
}
// Perform the transformation
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
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);
}
};
}
来源:https://stackoverflow.com/questions/14736318/resize-and-move-image-in-android-for-api-11