Resize and Move Image in Android for Api < 11 [closed]

社会主义新天地 提交于 2019-12-12 05:14:57

问题


I've been trying to achieve

  1. drag and drop (Move) Api < 11

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!