amar test
amar test

Reputation: 79

Image goes outside parent and dissapeared in android

I am working on an image Editing demo.
I have made a custom ImageView for dragging and zooming.
My problem is that when I move the ImageView outside its parent, it never comes back and it disappears.

Can anybody please tel me how to bring it back on touch?
My custom ImageView is as below:

ImageView

package PhotoLib;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class CollageView extends ImageView {

    private static final int PADDING = 8;
    private static final float STROKE_WIDTH = 8.0f;

    private Paint mBorderPaint;

    public CollageView(Context context) {
        this(context, null);
    }

    public CollageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        setPadding(PADDING, PADDING, PADDING, PADDING);
    }

    public CollageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initBorderPaint();
    }

    private void initBorderPaint() {
        mBorderPaint = new Paint();
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setColor(Color.WHITE);
        mBorderPaint.setStrokeWidth(STROKE_WIDTH);

    }

    public void setBorder(int width) {
        mBorderPaint.setStrokeWidth(width);
        mBorderPaint.setColor(Color.TRANSPARENT);

    }

    public void setBorder_grid(int width) {
        mBorderPaint.setStrokeWidth(width);
        mBorderPaint.setColor(Color.TRANSPARENT);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
            canvas.drawRect(PADDING, PADDING, getWidth() - PADDING, getHeight()
                    - PADDING, mBorderPaint);

        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void BringtoFront(View v) {
        v.bringToFront();
    }

}

TOuch.class

public class MultiTouchListener implements OnTouchListener {

    private static final int INVALID_POINTER_ID = -1;
    public boolean isRotateEnabled = true;
    public boolean isTranslateEnabled = true;
    public boolean isScaleEnabled = true;
    public float minimumScale = 0.2f;
    public float maximumScale = 10.0f;
    private int mActivePointerId = INVALID_POINTER_ID;
    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private float mPrevX;
    private float mPrevY;
    private ScaleGestureDetector mScaleGestureDetector;
    public Rect rect;
    private Matrix matrix = new Matrix();
    private Matrix savedMatrix = new Matrix();

    private int mode = NONE;
    // new variables..!!!
    private PointF start = new PointF();
    private PointF mid = new PointF();
    private float dx; // postTranslate X distance
    private float dy; // postTranslate Y distance
    private float[] matrixValues = new float[9];
    float matrixX = 0; // X coordinate of matrix inside the ImageView
    float matrixY = 0; // Y coordinate of matrix inside the ImageView
    float width = 0; // width of drawable
    float height = 0; // height of drawable

    public MultiTouchListener() {
        mScaleGestureDetector = new ScaleGestureDetector(
                new ScaleGestureListener());
    }

    private static float adjustAngle(float degrees) {
        if (degrees > 180.0f) {
            degrees -= 360.0f;
        } else if (degrees < -180.0f) {
            degrees += 360.0f;
        }

        return degrees;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private static void move(View view, TransformInfo info) {
        computeRenderOffset(view, info.pivotX, info.pivotY);
        adjustTranslation(view, info.deltaX, info.deltaY);

        // Assume that scaling still maintains aspect ratio.
        float scale = view.getScaleX() * info.deltaScale;
        scale = Math.max(info.minimumScale, Math.min(info.maximumScale, scale));
        view.setScaleX(scale);
        view.setScaleY(scale);

        float rotation = adjustAngle(view.getRotation() + info.deltaAngle);
        view.setRotation(rotation);
    }

    @SuppressLint("NewApi")
    private static void adjustTranslation(View view, float deltaX, float deltaY) {
        float[] deltaVector = { deltaX, deltaY };
        view.getMatrix().mapVectors(deltaVector);
        view.setTranslationX(view.getTranslationX() + deltaVector[0]);
        view.setTranslationY(view.getTranslationY() + deltaVector[1]);
    }

    @SuppressLint("NewApi")
    private static void computeRenderOffset(View view, float pivotX,
            float pivotY) {
        if (view.getPivotX() == pivotX && view.getPivotY() == pivotY) {
            return;
        }

        float[] prevPoint = { 0.0f, 0.0f };
        view.getMatrix().mapPoints(prevPoint);

        view.setPivotX(pivotX);
        view.setPivotY(pivotY);

        float[] currPoint = { 0.0f, 0.0f };
        view.getMatrix().mapPoints(currPoint);

        float offsetX = currPoint[0] - prevPoint[0];
        float offsetY = currPoint[1] - prevPoint[1];

        view.setTranslationX(view.getTranslationX() - offsetX);
        view.setTranslationY(view.getTranslationY() - offsetY);
    }

    @SuppressLint({ "NewApi", "ClickableViewAccessibility" })
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        mScaleGestureDetector.onTouchEvent(view, event);

        if (!isTranslateEnabled) {
            return false;
        }

        int action = event.getAction();
        switch (action & event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN: {
            view.bringToFront();
            mPrevX = event.getX();
            mPrevY = event.getY();

            // Save the ID of this pointer.
            mActivePointerId = event.getPointerId(0);

            rect = new Rect(view.getLeft(), view.getTop(), view.getRight(),
                    view.getBottom());
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position.

            if (mode == DRAG) {
                matrix.set(savedMatrix);

                matrix.getValues(matrixValues);
                matrixX = matrixValues[2];
                matrixY = matrixValues[5];
                width = matrixValues[0]
                        * (((ImageView) view).getDrawable().getIntrinsicWidth());
                height = matrixValues[4]
                        * (((ImageView) view).getDrawable()
                                .getIntrinsicHeight());

                dx = event.getX() - start.x;
                dy = event.getY() - start.y;

                // if image will go outside left bound
                if (matrixX + dx < 0) {
                    dx = -matrixX;
                }
                // if image will go outside right bound
                if (matrixX + dx + width > view.getWidth()) {
                    dx = view.getWidth() - matrixX - width;
                }
                // if image will go oustside top bound
                if (matrixY + dy < 0) {
                    dy = -matrixY;
                }
                // if image will go outside bottom bound
                if (matrixY + dy + height > view.getHeight()) {
                    dy = view.getHeight() - matrixY - height;
                }
                matrix.postTranslate(dx, dy);
            }

            int pointerIndex = event.findPointerIndex(mActivePointerId);
            if (pointerIndex != -1) {
                float currX = event.getX(pointerIndex);
                float currY = event.getY(pointerIndex);

                // Only move if the ScaleGestureDetector isn't processing a
                // gesture.
                if (!mScaleGestureDetector.isInProgress()) {
                    adjustTranslation(view, currX - mPrevX, currY - mPrevY);
                }
            }

            break;
        }

        case MotionEvent.ACTION_CANCEL:

            mActivePointerId = INVALID_POINTER_ID;
            break;

        case MotionEvent.ACTION_UP:
            view.performClick();
            mActivePointerId = INVALID_POINTER_ID;
            break;

        case MotionEvent.ACTION_POINTER_UP: {

            // Extract the index of the pointer that left the touch sensor.
            int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            int pointerId = event.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mPrevX = event.getX(newPointerIndex);
                mPrevY = event.getY(newPointerIndex);
                mActivePointerId = event.getPointerId(newPointerIndex);
            }

            break;
        }
        }

        return true;
    }

    private class ScaleGestureListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {

        private float mPivotX;
        private float mPivotY;
        private Vector2D mPrevSpanVector = new Vector2D();

        @Override
        public boolean onScaleBegin(View view, ScaleGestureDetector detector) {
            mPivotX = detector.getFocusX();
            mPivotY = detector.getFocusY();
            mPrevSpanVector.set(detector.getCurrentSpanVector());
            return true;
        }

        @Override
        public boolean onScale(View view, ScaleGestureDetector detector) {
            TransformInfo info = new TransformInfo();
            info.deltaScale = isScaleEnabled ? detector.getScaleFactor() : 1.0f;
            info.deltaAngle = isRotateEnabled ? Vector2D.getAngle(
                    mPrevSpanVector, detector.getCurrentSpanVector()) : 0.0f;
            info.deltaX = isTranslateEnabled ? detector.getFocusX() - mPivotX
                    : 0.0f;
            info.deltaY = isTranslateEnabled ? detector.getFocusY() - mPivotY
                    : 0.0f;
            info.pivotX = mPivotX;
            info.pivotY = mPivotY;
            info.minimumScale = minimumScale;
            info.maximumScale = maximumScale;

            move(view, info);
            return false;
        }
    }

    private class TransformInfo {

        public float deltaX;
        public float deltaY;
        public float deltaScale;
        public float deltaAngle;
        public float pivotX;
        public float pivotY;
        public float minimumScale;
        public float maximumScale;
    }

Upvotes: 0

Views: 207

Answers (1)

amar test
amar test

Reputation: 79

After a long searching effort i got this answer,Which works perfect for me..

ObjectAnimator animX = ObjectAnimator.ofFloat( my_select_collageview, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat( my_select_collageview, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

Upvotes: 1

Related Questions