拖放与onDraglistener动画回到原来的位置,如果没有目标下降 [英] drag and drop with onDraglistener animate to go back to original position if not dropped on target

查看:124
本文介绍了拖放与onDraglistener动画回到原来的位置,如果没有目标下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作出的游戏应用程序,我需要使用ondraglistener的拖放在这里我在做什么上实现onDraglistener的类。

I am making an game application where i need to use ondraglistener for drag and drop here what I am doing on a class which implements onDraglistener.

  @Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        Log.v("here","drag started");
        //no action necessary
        break;
    case DragEvent.ACTION_DRAG_ENTERED:
        //no action necessary
        Log.v("here","drag enteres");
        break;
    case DragEvent.ACTION_DRAG_LOCATION:
     int mCurX = (int) event.getX();
    int mCurY = (int) event.getY();
    Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );
    break;
    case DragEvent.ACTION_DRAG_EXITED:        
        //no action necessary

        Log.v("here","drag exits");
        break;
    case DragEvent.ACTION_DROP:
            .................do what ever when dropped...
     }

在这里我在做什么上进行draged视图的touchlistener和丢弃的:

here what I am doing on touchlistener of a view to be draged and dropped:

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        /*
         * Drag details: we only need default behavior
         * - clip data could be set to pass data as part of drag
         * - shadow can be tailored
         */
        view.setTag("option"+index);
        ClipData data = ClipData.newPlainText("tag", "option"+index);
        shadowBuilder = new View.DragShadowBuilder(view);

        //start dragging the item touched
        view.startDrag(data, shadowBuilder, view, 0);


        offsetX = (int)view.getX();//(int)motionEvent.getX();
         offsetY = (int)view.getY();//motionEvent.getY();
         Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());

         return false;
 }

现在它建立其中获得拖到任何地方,你拖动手指的影子建设者。现在,我怎么能动画阴影生成器或视图回到它的位置时,没有下降的目标。我试过公共布尔onInterceptTouchEvent(MotionEvent事件),并移至父视图,并采取X,Y位置和动画它回到它原来的位置,但它会采取行动取消。有没有什么办法让x,y坐标到一个地方的观点下降等那么目标。然后,我将能够提前positions.Thanks之间制作动画

Now it builds the shadow builder which get dragged to anywhere you drag finger. Now how could I animate the shadow builder or the view going back to its position when not dropped to target. I tried public boolean onInterceptTouchEvent(MotionEvent event) and moving to parent view and taking x,y position and animating it to go back to its original position but it gets action cancelled. Is there any way to get x,y coordinates to a place the view dropped other then the target. Then I would be able to animate it between the positions.Thanks in advance

推荐答案

您需要处理降:

switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_ENTERED:
     //break here was removed in purpose so you can see the shadow and the element is still at its initial place.
    case DragEvent.ACTION_DROP:
            View view = (View) event.getLocalState();
        if (prioritiesList.contains(v)) {

            ViewGroup viewgroup = (ViewGroup) view.getParent();
            viewgroup.removeView(view);

            LinearLayout containView = (LinearLayout) v;
            containView.addView(view);
            view.setVisibility(View.VISIBLE);
                    break;
}

这篇关于拖放与onDraglistener动画回到原来的位置,如果没有目标下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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