如何显示自定义 DragShadow 而不是 ListViewFragment 行?
我有一个 ListFragment,它有一个自定义适配器,这样我就可以在每一行上有一个文本视图和一个图像视图。当将此行拖动到我的画布(一个单独的片段)时,它将整行显示为拖动阴影(默认行为)。我只想将图标/图像拖到画布上。下面的代码示例生成原始拖动阴影(列表视图中的整行),并在单元格后面绘制所需的图像(由于边界设置代码而有点拉伸)。 这里有漂亮的图片。
根据 View.DragShadowBuilder 文档,您可以丢弃视图它被传递到构造函数并绘制您自己的视图。我使用这种方法的问题是画布/视图大小已设置,而我是图形新手。我尝试调整画布大小和/或清除画布并在其上绘制我自己的可绘制/位图,但没有成功。我尝试使用诸如 canvas.drawColor(Color.BLACK)
之类的内容在画布上着色,但这只会使背景变黑,但仍在原始单元格后面。
文档中提到的另一种可能性是传递范围内的任何视图。为此,我创建了一个图像视图并向其中添加了一个位图。当我拖动时,我得到一个空的拖动阴影。调试告诉我图像视图的高度和宽度为 0(因此视图和画布在拖动阴影生成器中最终为 0)。但是,如果我询问可绘制对象的高度和宽度,它会给出预期的尺寸。添加位图后,我是否需要做一些事情来使图像视图更新其大小?
哪种方法是首选?您有这两种方法的工作示例吗?
类别
public class IconListFragment extends ListFragment
{
...
private void startDraggingItem( View view, int position )
{
Log.d( TAG, "Dragging item." );
// I'd like to use the custom imageview instead of the current view
ImageView image = new ImageView( getActivity() );
image.setImageBitmap( getIconBitmap( position ) );
// Problem: Shows empty drag shadow (debugging shows that the height and width are 0)
// The drawable is present and has the right dimensions
// view.startDrag( data, new FlowchartDragShadowBuilder( image, position ), null, ZERO );
// Start the drag
// Problem: Shows both images
view.startDrag( data, new FlowchartDragShadowBuilder( view, position ), null, ZERO );
}
private class FlowchartDragShadowBuilder extends View.DragShadowBuilder
{
private Drawable shadow;
int position;
public FlowchartDragShadowBuilder( View view, int pos )
{
super( view );
Log.d( TAG, "Building shadow." );
shadow = getIconDrawable( pos );
shadow.setCallback( view );
position = pos;
shadow.setBounds( ZERO, ZERO, view.getWidth(), view.getHeight() );
}
@Override
public void onDrawShadow( Canvas canvas )
{
super.onDrawShadow( canvas );
Log.d( TAG, "Drawing shadow." );
shadow.draw( canvas );
getView().draw( canvas );
}
}
...
I have a ListFragment that has a custom adapter so that I could have a text view and an image view on each row. When dragging this row to my canvas (a separate fragment), it shows the whole row as the drag shadow (default behavior). I want to drag only the icon/image to the canvas. My code sample below produces the original drag shadow (whole row from list view) with the desired image drawn behind the cell (a bit stretched because of the bounds setting code). Pretty picture here.
According to the View.DragShadowBuilder documentation, you can discard the view that is passed into the constructor and draw your own view. Problem I have with that approach is that the canvas/view size is set and I'm new to graphics. I've been unsuccessful in my attempts to resize and/or clear the canvas and draw my own drawable/bitmap on it. I tried coloring over the canvas with something like canvas.drawColor(Color.BLACK)
, but that just makes the background black but still behind the original cell.
Another possibility mentioned in the documentation is passing in any view that is in scope. To that end, I created an image view and added a bitmap to it. When I drag, I get an empty drag shadow. Debugging tells me that the height and width of the image view are 0 (and therefore the view and canvas end up being 0 in the drag shadow builder). However, if I ask for the height and width of the drawable, it gives me the expected dimensions. Do I need to do something to make the image view update it's size after adding the bitmap?
Which approach is preferred? Do you have a working example of either approach?
Class
public class IconListFragment extends ListFragment
{
...
private void startDraggingItem( View view, int position )
{
Log.d( TAG, "Dragging item." );
// I'd like to use the custom imageview instead of the current view
ImageView image = new ImageView( getActivity() );
image.setImageBitmap( getIconBitmap( position ) );
// Problem: Shows empty drag shadow (debugging shows that the height and width are 0)
// The drawable is present and has the right dimensions
// view.startDrag( data, new FlowchartDragShadowBuilder( image, position ), null, ZERO );
// Start the drag
// Problem: Shows both images
view.startDrag( data, new FlowchartDragShadowBuilder( view, position ), null, ZERO );
}
private class FlowchartDragShadowBuilder extends View.DragShadowBuilder
{
private Drawable shadow;
int position;
public FlowchartDragShadowBuilder( View view, int pos )
{
super( view );
Log.d( TAG, "Building shadow." );
shadow = getIconDrawable( pos );
shadow.setCallback( view );
position = pos;
shadow.setBounds( ZERO, ZERO, view.getWidth(), view.getHeight() );
}
@Override
public void onDrawShadow( Canvas canvas )
{
super.onDrawShadow( canvas );
Log.d( TAG, "Drawing shadow." );
shadow.draw( canvas );
getView().draw( canvas );
}
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试启用绘图缓存。问题可能是因为它没有添加到任何布局中。
然后开始拖动它。希望有帮助。
更新
在
onDrawShadow()
中通过调用imageview上的getdrawingCache()
方法获取视图位图Try enabling the drawing cache. The problem may be because it is not added to any layout.
Then start dragging it. Hope it helps.
Update
In
onDrawShadow()
get the view bitmap by callinggetdrawingCache()
method on the imageview不确定您在 getIconBitmap() 中做什么,但也许该函数不起作用。
我使用新的图像视图:
只要您可以在新画布上绘图,您就可以制作任何您想要的阴影。只需记住在位图上调用 destroy() 来释放内存即可。
Not sure what you're doing in getIconBitmap() but maybe that function isn't working.
I use a new imageview:
As long as you can draw onto the new canvas you can make any shadow you want. Just remember to call destroy() on the bitmap to free up the memory.