如何在android中启动拖动后更改拖动阴影?

时间:2015-05-26 06:51:34

标签: android drag

我想在调用startDrag后更改dragShadow的图像。 但我找不到任何可以做到这一点的API。 任何建议我该怎么做?

            ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
            String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);
            View.DragShadowBuilder dragShadow= new View.DragShadowBuilder(((ImageButton)findViewById(R.id.rele_button)));

1 个答案:

答案 0 :(得分:0)

更多代码 扩展View.DragShadowBuilder的类的代码

public class ImageDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow;

ImageDragShadowBuilder() {
    super();
}

public static View.DragShadowBuilder fromResource(Context context, int drawableId) {
    ImageDragShadowBuilder builder = new ImageDragShadowBuilder();

    builder.shadow = context.getResources().getDrawable(drawableId);
    if (builder.shadow == null) {
        throw new NullPointerException("Drawable from id is null");
    }

    builder.shadow.setBounds(0, 0, builder.shadow.getMinimumWidth(), builder.shadow.getMinimumHeight());

    return builder;
}

public static View.DragShadowBuilder fromBitmap(Context context, Bitmap bmp) {
    if (bmp == null) {
        throw new IllegalArgumentException("Bitmap cannot be null");
    }

    ImageDragShadowBuilder builder = new ImageDragShadowBuilder();

    builder.shadow = new BitmapDrawable(context.getResources(), bmp);
    builder.shadow.setBounds(0, 0, builder.shadow.getMinimumWidth(), builder.shadow.getMinimumHeight());

    return builder;
}

@Override
public void onDrawShadow(Canvas canvas) {
    shadow.draw(canvas);
}

@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
    shadowSize.x = shadow.getMinimumWidth();
    shadowSize.y = shadow.getMinimumHeight();

    shadowTouchPoint.x = (int)(shadowSize.x / 2);
    shadowTouchPoint.y = (int)(shadowSize.y / 2);
}

}

开始拖动后的更改代码称为

此代码属于onDrag函数

case DragEvent.ACTION_DRAG_ENTERED:
                    ImageDragShadowBuilder.fromResource(getBaseContext(), R.mipmap.phone_icon);