我正在开发一个电子商务应用,当我将一个项目从recyclerview拖到一个布局时,我需要更改拖动项目的边框颜色并更改此项目原始空间的背景。
这是我开始拖动时的代码:
@Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
//Start dragging the item touched
v.startDrag(data, //Data to be dragged
shadowBuilder, //Drag shadow
v, //Local data about the drag and drop operation
0); //No flags
v.setVisibility(View.INVISIBLE);
return true;
}
我曾尝试创建自己的ShadowBuilder,但是当我开始拖动项目时,它具有透明背景并且未显示拖动的项目:
public class MyDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow; // The drag shadow image, defined as a drawable thing
private Context context;
private View view;
public MyDragShadowBuilder (Context ctx, View v) {
super(v);
this.context = ctx;
this.view = v;
shadow = new ColorDrawable(R.drawable.border_cardview_red);
}
// Defines a callback that sends the drag shadow dimensions and touch point back to the system.
@ Override
public void onProvideShadowMetrics (Point size, Point touch){
int width, height;
width = getView().getWidth() / 2; //Width of the shadow
height = getView().getHeight() / 2; //Height of the shadow
// The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
// Canvas that the system will provide. As a result, the drag shadow will fill the canvas.
shadow.setBounds(0, 0, width, height);
size.set(width, height); //Sets the size parameter's width and height values. These get back to the syste through the size parameter.
touch.set(width/2, height/2); //Sets the touch point's position to be in the middle of the drag shadow
}
// Defines a callback that draws the drag shadow in a Canvas that the system constructs from the dimensions passed in onProvideShadowMetrics ().
@ Override
public void onDrawShadow (Canvas canvas) {
shadow.draw(canvas);
}
}
这是我的bordercard_view_red:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<stroke android:width="1dp" android:color="@color/primary_color" />
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
我不知道如何使用其他边框颜色拖动项目并更改拖动此项目的空白区域的背景。
在此图片中,您可以看到我需要:)
我知道你可以帮助我:) 提前致谢
答案 0 :(得分:0)
您可以通过扩展它来使用自定义View.DragShadowBuilder。
Changing dragshadow in android, while dragging happens
(这涉及不同的问题,但stackoverflow中的文章会给你一个关于如何扩展它的提示。)
基本上,View.DragShadowBuilder中的onDrawShadow涉及阴影图像的绘制。 您可以使用所需的边框制作阴影图像。
要获取视图的大小,可以调用getView或从onProvideShadowMetrics()获取信息。