图像在父母之外,在android中消失

时间:2015-05-04 05:44:41

标签: android imageview ontouchlistener

我正在制作图像编辑演示。
我已经制作了一个用于拖动和缩放的自定义ImageView 我的问题是,当我将ImageView移到其父级之外时,它永远不会回来并消失。

有人可以打电话给我如何重新联系吗? 我的自定义ImageView如下:

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;
    }

1 个答案:

答案 0 :(得分:1)

经过长时间的搜索努力,我得到了这个答案,这对我来说非常适合..

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();