android - 在拖拽一张图片时,如何保证其它图片不动?
怪我咯
怪我咯 2017-04-17 11:31:13
0
1
443

在我的应用程序中,我可以将一张图片覆盖在另一张图片上,但是,当我拖动一张图片是,它左侧和右侧的图片也都在屏幕上移动,我不清楚为什么会出现这样的状况,以下是我目前使用的代码:

int cards[] = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,R.drawable.c6,
                R.drawable.c7,R.drawable.c8,R.drawable.c9,R.drawable.c10,R.drawable.c11,R.drawable.c12,R.drawable.c13};
    ImageView[] Images = new ImageView[cards.length];
            for (int i = 0; i < cards.length; i++) {           
                Images[i] = new ImageView(this);

                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                if (i != 0) {          

                     params.addRule(RelativeLayout.ALIGN_LEFT,i-1);
                     params.leftMargin= 40;
                     Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
                     Images[i].setId(i);
                     Images[i].setOnTouchListener(this);
                    layout.addView(Images[i], params);
                } else {
                     Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
                     Images[i].setId(i);
                     Images[i].setOnTouchListener(this);
                    layout.addView(Images[i], params);
                }

            }

//Ontouch

@Override
    public boolean onTouch(View p_v, MotionEvent p_event){
        params = (RelativeLayout.LayoutParams)p_v.getLayoutParams();
        switch (p_event.getAction()){       
        case MotionEvent.ACTION_DOWN:
        {
            status = START_DRAGGING;            
            imageView1 = new ImageView(this);
            imageView1.setImageBitmap(p_v.getDrawingCache());

            m_lastTouchX = p_event.getX();
            m_lastTouchY = p_event.getY();  

            break;
        }
        case MotionEvent.ACTION_UP:
        {
                status=STOP_DRAGGING;           
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
            if (status == START_DRAGGING) {
                m_dx = p_event.getX() - m_lastTouchX;
                m_dy = p_event.getY() - m_lastTouchY;

                m_posX = m_prevX + m_dx;
                m_posY = m_prevY + m_dy;
                System.out.println("Dragging");
                    params.leftMargin = (int) m_posX;

                    params.topMargin=(int) m_posY;
                    p_v.bringToFront();
                    p_v.setLayoutParams(params);
                    imageView1.invalidate();
                    m_prevX = m_posX;
                    m_prevY = m_posY;

            }


            break;

        }
        }
        return true;
    }

原问题:How to stop other images to move while dragging of one image

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
小葫芦

答:Niko
你可以用下述的你一小片段达到这种效果,我在RelativeLayout上添加了一些图片,在canvas中完成拖拽的操作,效果会更好。你只需要控制layout参数来刷新MotionEvent.ACTION_UP即可:

private ImageView[] Images;
private boolean mDragging = false;
private Rect mImageRect = new Rect();
int mX = 0;
int mY = 0;
private Bitmap mBitmap;
private ImageView mImage;

@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mX = (int) ev.getX();
        mY = (int) ev.getY();

        for (ImageView image : Images) {
            image.getHitRect(mImageRect);

            if (mImageRect.contains(mX, mY)) {
                mDragging = true;

                mImage = image;
                mBitmap = ((BitmapDrawable) mImage.getDrawable())
                        .getBitmap();

                mImage.setVisibility(View.GONE);
                postInvalidate();
                break;
            }
        }

        break;
    case MotionEvent.ACTION_MOVE:
        if (mDragging) {
            mX = (int) ev.getX();
            mY = (int) ev.getY();

            postInvalidate();
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mDragging) {
            mDragging = false;
            mBitmap = null;
            mImage.setVisibility(View.VISIBLE);
            mImage = null;
        }
        break;
    }

    return super.onTouchEvent(ev);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (mBitmap != null) {
        canvas.drawBitmap(mBitmap, mX, mY, null);
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!