Wie kann ich ein Bild in Android auswählen und die Auswahl aufheben?

Ich versuche ein Projekt zu schreiben. In meinem Projekt kann ich ein Bild erfolgreich ziehen.Dies ist das BILD und hier ist mein ViewOnTouchListener-Code:

package com.seifi.dragtestapplication;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
class ViewOnTouchListener implements View.OnTouchListener {
    Point pushPoint, closePoint;
    int lastImgLeft;
    int lastImgTop;
    FrameLayout.LayoutParams viewLP;
    FrameLayout.LayoutParams pushBtnLP;
    FrameLayout.LayoutParams closeBtnLP;
    int lastPushBtnLeft, lastCloseBtnLeft;
    int lastPushBtnTop, lastCloseBtnTop;
    private View mPushView, mCloseView, mShadowView;

    ViewOnTouchListener(View mPushView, View mCloseView, View mShadowView) {
        this.mPushView = mPushView;
        this.mCloseView = mCloseView;
        this.mShadowView = mShadowView;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                if (null == viewLP) {
                    viewLP = (FrameLayout.LayoutParams) view.getLayoutParams();
                }
                if (null == pushBtnLP) {
                    pushBtnLP = (FrameLayout.LayoutParams) mPushView.getLayoutParams();
                }

                if (null == closeBtnLP) {
                    closeBtnLP = (FrameLayout.LayoutParams) mCloseView.getLayoutParams();
                }
                closePoint = getRawPoint(event);
                pushPoint = getRawPoint(event);
                lastImgLeft = viewLP.leftMargin;
                lastImgTop = viewLP.topMargin;
                lastPushBtnLeft = pushBtnLP.leftMargin;
                lastPushBtnTop = pushBtnLP.topMargin;

                lastCloseBtnLeft = closeBtnLP.leftMargin;
                lastCloseBtnTop = closeBtnLP.topMargin;
                (mShadowView).setBackgroundResource(R.drawable.my_shadow_pressed);
                break;
            case MotionEvent.ACTION_MOVE:
                Point newPoint = getRawPoint(event);
                float moveX = newPoint.x - pushPoint.x;
                float moveY = newPoint.y - pushPoint.y;

                viewLP.leftMargin = (int) (lastImgLeft + moveX);
                viewLP.topMargin = (int) (lastImgTop + moveY);
                view.setLayoutParams(viewLP);

                pushBtnLP.leftMargin = (int) (lastPushBtnLeft + moveX);
                pushBtnLP.topMargin = (int) (lastPushBtnTop + moveY);
                mPushView.setLayoutParams(pushBtnLP);

                closeBtnLP.leftMargin = (int) (lastCloseBtnLeft + moveX);
                closeBtnLP.topMargin = (int) (lastCloseBtnTop + moveY);
                mCloseView.setLayoutParams(closeBtnLP);

                break;
            case MotionEvent.ACTION_UP: {
                (mShadowView).setBackgroundResource(R.drawable.my_shadow);
                break;
            }
        }
        return false;
    }

    private Point getRawPoint(MotionEvent event) {
        return new Point((int) event.getRawX(), (int) event.getRawY());
    }
}

Blick auf das Bild bitte und das ist mein DragView Code:

package com.seifi.dragtestapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DragView extends LinearLayout {
    private ImageView mView;
    private ImageView mPushView;
    private ImageView mCloseView;
    private View mShadowView;
    private float _1dp;
    private boolean mCenterInParent = true;
    private Drawable mImageDrawable, mPushImageDrawable, mCloseImageDrawable, mShadowImageDrawable;
    private float mImageHeight, mImageWidth, mPushImageHeight, mPushImageWidth, mCloseImageHeight, mCloseImageWidth, mShadowImageWidth, mShadowImageHeight;
    private int mLeft = 0, mTop = 0;


    public DragView(Context context) {
        this(context, null, 0);
    }

    public DragView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this._1dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
        this.parseAttr(context, attrs);
        View mRoot = View.inflate(context, R.layout.drag_layout, null);
        addView(mRoot, -1, -1);
        mPushView = (ImageView) mRoot.findViewById(R.id.push_view);
        mView = (ImageView) mRoot.findViewById(R.id.view);
        mCloseView = (ImageView) mRoot.findViewById(R.id.close_view);
        mShadowView = (View)mRoot.findViewById(R.id.shadowView);
        mView.setOnTouchListener(new ViewOnTouchListener(mPushView, mCloseView, mShadowView));
    }



    private void parseAttr(Context context, AttributeSet attrs) {
        if (null == attrs) return;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SingleFingerView);
        if (a != null) {
            int n = a.getIndexCount();
            for (int i = 0; i < n; i++) {
                int attr = a.getIndex(i);
                if (attr == R.styleable.SingleFingerView_centerInParent) {
                    this.mCenterInParent = a.getBoolean(attr, true);
                } else if (attr == R.styleable.SingleFingerView_image) {
                    this.mImageDrawable = a.getDrawable(attr);
                } else if (attr == R.styleable.SingleFingerView_image_height) {
                    this.mImageHeight = a.getDimension(attr, 200 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_image_width) {
                    this.mImageWidth = a.getDimension(attr, 200 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_push_image) {
                    this.mPushImageDrawable = a.getDrawable(attr);
                } else if (attr == R.styleable.SingleFingerView_push_image_width) {
                    this.mPushImageWidth = a.getDimension(attr, 30 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_push_image_height) {
                    this.mPushImageHeight = a.getDimension(attr, 30 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_left) {
                    this.mLeft = (int) a.getDimension(attr, 0 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_top) {
                    this.mTop = (int) a.getDimension(attr, 0 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_close_image) {
                    this.mCloseImageDrawable = a.getDrawable(attr);
                } else if (attr == R.styleable.SingleFingerView_close_image_width) {
                    this.mCloseImageWidth = a.getDimension(attr, 30 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_close_image_height) {
                    this.mCloseImageHeight = a.getDimension(attr, 30 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_shadow_image) {
                    this.mShadowImageDrawable = a.getDrawable(attr);
                } else if (attr == R.styleable.SingleFingerView_shadow_image_width) {
                    this.mShadowImageWidth = a.getDimension(attr, 200 * _1dp);
                } else if (attr == R.styleable.SingleFingerView_shadow_image_height) {
                    this.mShadowImageHeight = a.getDimension(attr, 200 * _1dp);
                }
            }
        }
    }

    private void setViewToAttr(int pWidth, int pHeight) {
        if (null != mImageDrawable) {
            this.mView.setBackgroundDrawable(mImageDrawable);
        }
        if (null != mPushImageDrawable) {
            this.mPushView.setBackgroundDrawable(mPushImageDrawable);
        }

        if (null != mCloseImageDrawable) {
            this.mCloseView.setBackgroundDrawable(mCloseImageDrawable);
        }

        if (null != mShadowImageDrawable) {
            this.mShadowView.setBackgroundDrawable(mShadowImageDrawable);
        }

        FrameLayout.LayoutParams viewLP = (FrameLayout.LayoutParams) this.mView.getLayoutParams();
        viewLP.width = (int) mImageWidth;
        viewLP.height = (int) mImageHeight;
        int left = 0, top = 0;
        if (mCenterInParent) {
            left = pWidth / 2 - viewLP.width / 2;
            top = pHeight / 2 - viewLP.height / 2;
        } else {
            if (mLeft > 0) left = mLeft;
            if (mTop > 0) top = mTop;
        }
        viewLP.leftMargin = left;
        viewLP.topMargin = top;
        this.mView.setLayoutParams(viewLP);
        this.mShadowView.setLayoutParams(viewLP);


        FrameLayout.LayoutParams pushViewLP = (FrameLayout.LayoutParams) mPushView.getLayoutParams();
        pushViewLP.width = (int) mPushImageWidth;
        pushViewLP.height = (int) mPushImageHeight;
        pushViewLP.leftMargin = (int) (viewLP.leftMargin + mImageWidth - mPushImageWidth / 2);
        pushViewLP.topMargin = (int) (viewLP.topMargin + mImageHeight - mPushImageHeight / 2);
        mPushView.setLayoutParams(pushViewLP);

        FrameLayout.LayoutParams closeViewLP = (FrameLayout.LayoutParams) mCloseView.getLayoutParams();
        closeViewLP.width = (int) mCloseImageWidth;
        closeViewLP.height = (int) mCloseImageHeight;
        closeViewLP.leftMargin = (int) (viewLP.leftMargin - mCloseImageWidth / 2);
        closeViewLP.topMargin = (int) (viewLP.topMargin - mCloseImageHeight / 2);
        mCloseView.setLayoutParams(closeViewLP);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setParamsForView(widthMeasureSpec, heightMeasureSpec);
    }

    private boolean hasSetParamsForView = false;

    private void setParamsForView(int widthMeasureSpec, int heightMeasureSpec) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (null != layoutParams && !hasSetParamsForView) {
            System.out.println("");
            hasSetParamsForView = true;
            int width;
            if ((getLayoutParams().width == LayoutParams.MATCH_PARENT)) {
                width = MeasureSpec.getSize(widthMeasureSpec);
            } else {
                width = getLayoutParams().width;
            }
            int height;
            if ((getLayoutParams().height == LayoutParams.MATCH_PARENT)) {
                height = MeasureSpec.getSize(heightMeasureSpec);
            } else {
                height = getLayoutParams().height;
            }
            setViewToAttr(width, height);
        }
    }
}

alles ist ok. Aber ich muss etwas tun, damit der Schatten und der Rand verschwinden, wenn der Benutzer aus dem Bild (Aufkleber) klickt. Wenn der Benutzer erneut auf das Bild klickt, wird es erneut angezeigt. Ich weiß nicht, wie ich es machen soll.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage