在View.OnDragListener得到拖影的位置 [英] get dragged shadow location on View.OnDragListener

查看:323
本文介绍了在View.OnDragListener得到拖影的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用拖放在我的应用程序拖放功能。它工作正常。现在我面临的问题,而动画视图时,视图又回到它原来的位置,如果它放置于不下​​降视图。

I am using drag and drop functionality in my application. It works fine. Now i am facing issue while animating view when view comes back to it's original position if it is not placed on dropped view.

我已经

  View.DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
                                view.startDrag(data, shadowBuilder, view, 0);

和我shadowbuilder类是,

And my shadowbuilder class is,

private static class MyDragShadowBuilder extends View.DragShadowBuilder {
    private static Drawable shadow;

    public MyDragShadowBuilder(View v) {
        // Stores the View parameter passed to myDragShadowBuilder.
        super(v);
        v.buildDrawingCache();
        shadow =  new BitmapDrawable(v.getDrawingCache());
    }

    @Override
    public void onProvideShadowMetrics (Point size, Point touch){
        // Defines local variables
        int width, height;
        width = (int)(getView().getWidth() * 1.1);
        height =(int)(getView().getHeight() * 1.1);
        shadow.setBounds(0, 0, width, height);
        size.set(width, height);
        touch.set(width / 2, height / 2);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        // Draws the ColorDrawable in the Canvas passed in from the system.
        shadow.draw(canvas);
    }
}

现在,当我发现不成功的下降,那么我希望我的观点得到动画,回到它原来的位置。

Now, when i found unsuccessful drop, then i want my view to get animated and go back to it's original position.

   case DragEvent.ACTION_DRAG_ENDED:
                if (!event.getResult()) {
                      if (dragged.getTag() == v.getTag()){
                         int[] location={0,0};
                         v.getLocationOnScreen(location);
                         Log.v("values::", "x:" + x + "  - y:" +x);
                         addViewRemoveViewFromWindow(dragged, location);
                    }
                }

                break;

我能获得位置阵列即我的终点位置我的观点的原始位置。我想在屏幕上我的观点拖(阴影视图)的位置。但我不能够获得它在任何情况下。试图得到它 DragEvent.ACTION_DRAG_LOCATION:情况,但没有运气。它总是返回原来的两种视图的位置或丢弃视图位置的位置。我想拖影图像的位置。我想在窗口申请两个位置之间的转换动画。在窗口动画视图2位置之间,当我试图通过使用2静态位置正常工作。

I am able to get my view's original position in location array i.e. my end position. And i want my that dragged view(shadow view) position on screen. But i am not able to get it in any cases. Have tried to get it on DragEvent.ACTION_DRAG_LOCATION: case but with no luck. It always returns position of either original view's position or dropped view position. I want dragged shadow images's location. I want to apply translation animation in window between two locations. That animating view in window between 2 locations works fine when i tried by using 2 static locations.

推荐答案

要动画的影子初始视图您可以使用此代码片段。

To animate your shadow to initial view you can use this snippet.

private void animateDragToStart(View initialView, float fromX, float fromY) {
    float topMargin = fromY - initialView.getTop();
    float leftMargin = fromX - initialView.getLeft();

    Animation translateAnimation = new TranslateAnimation(leftMargin - (initialView.getWidth() / 2), 0, topMargin - (initialView.getHeight() / 2), 0);
    translateAnimation.setDuration(500);
    translateAnimation.setInterpolator(new AccelerateInterpolator());
    initialView.startAnimation(translateAnimation);
    initialView.setVisibility(View.VISIBLE);
}

在我的情况下,我从下拉的情况下调用方法

In my case I called method from the DROP case

case DragEvent.ACTION_DROP:
View initialView = (View) event.getLocalState();//get your initial view
initialView.setVisibility(View.VISIBLE);
animateDragToStart(initialView, event.getX(), event.getY());

// Returns true. DragEvent.getResult() will return true.
return true;

和创建卷影时,你应该隐藏原始视图。

And when the shadow is created you should hide the original view.

View.DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);

问题是老了,但我回答,因为可以帮助给别人。

The question is old, but I answered because could help to somebody.

这篇关于在View.OnDragListener得到拖影的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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