在Android的影子建设者 [英] Shadow builder in android

查看:194
本文介绍了在Android的影子建设者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作为dragDrop比赛,但我有个小问题,当我在一个视图中单击阴影建设者第一次出现在正确的顶角,然后将其与触摸的地方移动。

另外的阴影助洗剂比初始视图小。我怎样才能使它作为初始看法?

 私人final类MyTouchListener实现OnTouchListener {
    公共布尔onTouch(查看视图,MotionEvent motionEvent){        如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
            ClipData数据= ClipData.newPlainText(,);
            DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
            view.startDrag(数据,shadowBuilder,查看,0);
            view.setVisibility(View.INVISIBLE);
            返回true;
        }其他{
            返回false;
        }
    }
}


解决方案

我可能会迟到,但...
你必须计算触摸的点,你的拖动左上角之间的偏移,和与自定义DragShadowBuilder使用它。

下面是code的失调:

  @覆盖
公共布尔onTouch(查看视图,MotionEvent事件){    开关(event.getAction()){        案例MotionEvent.ACTION_DOWN:{            点偏移=新点((int)的event.getX(),(INT)event.getY());            ClipData数据= ClipData.newPlainText(,);
            DragShadowBuilder shadowBuilder =新CustomDragShadowBuilder(容器,偏移量);
            view.startDrag(数据,shadowBuilder,容器,0);
            view.setVisibility(View.INVISIBLE);
        }
    }    返回true;
}

这里是code为自定义生成器:

 进口android.graphics.Point;
进口android.view.View;公共类CustomDragShadowBuilder扩展View.DragShadowBuilder {// ------------------------------------------------ ------------------------------------------
//私有属性:
私人点_offset;
// ------------------------------------------------ ------------------------------------------// ------------------------------------------------ ------------------------------------------
//构造函数:
公共CustomDragShadowBuilder(查看视图,点偏移){    //商店传递给myDragShadowBuilder View参数。
    超(视图);    //保存偏移:
    _offset =偏移;
}
// ------------------------------------------------ ------------------------------------------// ------------------------------------------------ ------------------------------------------
//定义发送拖影的尺寸和触摸点回到系统的回调。
@覆盖
公共无效onProvideShadowMetrics(点大小,点触控){    //设置阴影大小:
    size.set(getView()的getWidth(),getView()的getHeight());    //设置触摸点的是在拖影的中间位置
    touch.set(_offset.x,_offset.y);
}
// ------------------------------------------------ ------------------------------------------
}

I am working on a dragdrop game, but I have small problem, that when I click in a view the shadow builder appears first at the right-top-corner and then it moves with the touch place.

Also the shadow builder is smaller than the initial view. How can I make it as the initial view?

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

解决方案

I may be late, but… You have to compute the offset between the touched point and your draggable top left corner, and use it with a custom DragShadowBuilder.

Here is the code for the offset :

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

    switch(event.getAction()) {

        case MotionEvent.ACTION_DOWN : {

            Point offset = new Point((int) event.getX(), (int) event.getY());

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new CustomDragShadowBuilder(container, offset);
            view.startDrag(data, shadowBuilder, container, 0);
            view.setVisibility(View.INVISIBLE);
        }
    }

    return true;
}

And here is the code for the custom builder :

import android.graphics.Point;
import android.view.View;

public class CustomDragShadowBuilder extends View.DragShadowBuilder {

// ------------------------------------------------------------------------------------------
// Private attributes :
private Point _offset;
// ------------------------------------------------------------------------------------------



// ------------------------------------------------------------------------------------------
// Constructor :
public CustomDragShadowBuilder(View view, Point offset) {

    // Stores the View parameter passed to myDragShadowBuilder.
    super(view);

    // Save the offset :
    _offset = offset;
}
// ------------------------------------------------------------------------------------------



// ------------------------------------------------------------------------------------------
// Defines a callback that sends the drag shadow dimensions and touch point back to the system.
@Override
public void onProvideShadowMetrics (Point size, Point touch) {

    // Set the shadow size :
    size.set(getView().getWidth(), getView().getHeight());

    // Sets the touch point's position to be in the middle of the drag shadow
    touch.set(_offset.x, _offset.y);
}
// ------------------------------------------------------------------------------------------
}

这篇关于在Android的影子建设者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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