Android的拖放ImageView的onTouchListener [英] android drag and drop ImageView onTouchListener

查看:238
本文介绍了Android的拖放ImageView的onTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好结果我已经成立了一个onTouchListener我一个ImageView的应用程序中,我的目的是让在以往任何时候他们想要的应用程序内的ImageView的用户将能够拖动和放置。结果结果我已经写了使用示例code一些code在网上找到的,我认为会工作的onTouchListener上,但我有一些问题,而不至于让我周围拖动图像,图像大小调整,当你在它拖动手指变大或变小。
搜索结果
没有人有任何想法,为什么结果这里是我的code:?搜索

  ImageView的t1img;    t1img =(ImageView的)findViewById(R.id.imgT1);    WINDOWWIDTH = getWindowManager()getDefaultDisplay()的getWidth()。
    。WINDOWHEIGHT = getWindowManager()getDefaultDisplay()的getHeight();    t1img.setOnTouchListener(新View.OnTouchListener(){        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){            的LayoutParams =(的LayoutParams)t1img.getLayoutParams();            开关(event.getAction()){                案例MotionEvent.ACTION_DOWN:                打破;                案例MotionEvent.ACTION_MOVE:                    INT XCOORD =(int)的event.getRawX();
                    INT YCOORD =(int)的event.getRawY();                    如果(XCOORD> WINDOWWIDTH){                        XCOORD = WINDOWWIDTH;
                    }                    如果(YCOORD> WINDOWHEIGHT){                        YCOORD = WINDOWHEIGHT;
                    }                    layoutParams.leftMargin = XCOORD - 25;
                    layoutParams.topMargin = YCOORD - 75;                    t1img.setLayoutParams(的LayoutParams);                打破;                默认:                打破;
            }            返回true;
        }
    });


解决方案

如果您需要支持姜饼你可以看看我在这里的例子

https://github.com/NAYOSO/android-dragview

如果你只需要支持果冻豆上面可以使用拖动和Android的库拖放你可以从这篇文章中看到它。

http://developer.android.com/guide/topics/ UI /拖drop.html

有关拖放查看一些解释
首先你需要创建牛逼触摸监听器,然后调用的startDrag开始draging。就这么简单。

 私人final类dragTouchListener实现OnTouchListener {        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(event.getAction()== MotionEvent.ACTION_DOWN){
                DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(V);
                v.startDrag(数据,shadowBuilder,V,0);
                返回true;
            }其他{
                返回false;
            }
        }    }

要监视下降的地方,你可以使用onDragListener的目标

 私有类dropListener实现OnDragListener {    查看draggedView;
    CustomTextView下降;    @覆盖
    公共ondrag当布尔(视图V,事件的dragEvent){
        开关(event.getAction()){
        案例DragEvent.ACTION_DRAG_STARTED:
            draggedView =(查看)event.getLocalState();
            下降=(CustomTextView)draggedView;
            draggedView.setVisibility(View.INVISIBLE);
            打破;
        案例DragEvent.ACTION_DRAG_ENTERED:
            打破;
        案例DragEvent.ACTION_DRAG_EXITED:
            打破;
        案例DragEvent.ACTION_DROP:            CustomTextView的DropTarget =(CustomTextView)V;
            dropTarget.setText(dropped.getText()的toString());
            打破;
        案例DragEvent.ACTION_DRAG_ENDED:
            打破;
        默认:
            打破;
        }
        返回true;
    }}

你可以从我的code看到有许多事件,但主要的一个是观点开始被拖动时,滴加结束了。

不要忘记设置监听器来查看

  tvDrag.setOnTouchListener(新dragTouchListener());
    tvDrop.setOnDragListener(新dropListener())

我希望我的解释是很清楚!
如果您还有问题我会尽量今晚或明天来回答吧:)

Hello
I've set up an onTouchListener within my application for an ImageView, my aim was to have an ImageView that the user would be able to drag around and place where ever they want within the app.

I've written some code using sample code found on the web for an onTouchListener that I thought would work but I'm having some problems with it, rather than allowing me to drag the image around, the image resizes and gets bigger or smaller when you drag your finger over it.

Does anyone have any ideas why?
Here's my code:

    ImageView t1img;

    t1img = (ImageView) findViewById(R.id.imgT1);

    windowWidth = getWindowManager().getDefaultDisplay().getWidth();
    windowHeight = getWindowManager().getDefaultDisplay().getHeight();

    t1img.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            layoutParams = (LayoutParams) t1img.getLayoutParams();

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                break;

                case MotionEvent.ACTION_MOVE:

                    int xCoord = (int) event.getRawX();
                    int yCoord = (int) event.getRawY();

                    if (xCoord > windowWidth) {

                        xCoord = windowWidth;
                    }

                    if (yCoord > windowHeight) {

                        yCoord = windowHeight;
                    }

                    layoutParams.leftMargin = xCoord - 25;
                    layoutParams.topMargin = yCoord - 75;

                    t1img.setLayoutParams(layoutParams);

                break;

                default:

                break;
            }

            return true;
        }
    });

解决方案

If you need to support gingerbread you can take a look at my example here

https://github.com/NAYOSO/android-dragview

if you only need to support jelly bean above you can use the Drag and Drop from android library you can see it from this article

http://developer.android.com/guide/topics/ui/drag-drop.html

For some explanation about the Drag and Drop view at first you need t create the touch listener and then call startDrag to start draging. As simple as that.

private final class dragTouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                v.startDrag(data, shadowBuilder, v, 0);
                return true;
            } else {
                return false;
            }
        }

    }

To monitor the target of dropping place you can use onDragListener

private class dropListener implements OnDragListener {

    View draggedView;
    CustomTextView dropped;

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            draggedView = (View) event.getLocalState();
            dropped = (CustomTextView) draggedView;
            draggedView.setVisibility(View.INVISIBLE);
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            break;
        case DragEvent.ACTION_DROP:

            CustomTextView dropTarget = (CustomTextView) v;
            dropTarget.setText(dropped.getText().toString());
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            break;
        default:
            break;
        }
        return true;
    }

}

As you can see from my code there is many event but the main one is when the view is start being dragged, dropped and ended.

Don't forget to set the listener to view

    tvDrag.setOnTouchListener(new dragTouchListener());
    tvDrop.setOnDragListener(new dropListener())

I hope my explanation is clear enough! If you have further question I will try to answer it tonight or tomorrow :)

这篇关于Android的拖放ImageView的onTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆