如何显示自定义DragShadow而不是ListViewFragment行? [英] How to show custom DragShadow instead of ListViewFragment row?

查看:103
本文介绍了如何显示自定义DragShadow而不是ListViewFragment行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义适配器的ListFragment,所以我可以在每行上有一个文本视图和一个图像视图.将这一行拖到我的画布(一个单独的片段)时,它将整个行显示为拖动阴影(默认行为).我只想将图标/图像拖到画布上.我下面的代码示例生成原始拖动阴影(列表视图中的整个行),并在单元格后面绘制了所需的图像(由于边界设置代码而被拉伸了一点). 在这里的漂亮图片.

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.

根据 View.DragShadowBuilder文档,您可以丢弃传递给构造函数的视图并绘制您自己的视图.我对这种方法的问题是画布/视图的大小已设置并且我是图形新手.我尝试调整画布大小和/或清除画布并在其上绘制自己的drawable/bitmap一直没有成功.我尝试使用canvas.drawColor(Color.BLACK)之类的颜色在画布上着色,但这只会使背景变黑,但仍在原始单元格后面.

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.

文档中提到的另一种可能性是传递范围内的任何视图.为此,我创建了一个图像视图,并向其中添加了一个位图.拖动时,会出现一个空的拖动阴影.调试告诉我,图像视图的高度和宽度为0(因此,视图和画布在拖动阴影生成器中最终为0).但是,如果我要求绘制对象的高度和宽度,它会给我提供预期的尺寸.添加位图后,我需要做些什么使图像视图更新大小吗?

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?

课程

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 );
        }
    }

    ...

推荐答案

不确定您在getIconBitmap()中正在做什么,但是该功能可能无法正常工作.

Not sure what you're doing in getIconBitmap() but maybe that function isn't working.

我使用新的imageview:

I use a new imageview:

shadowbm = Bitmap.createBitmap(thisbutton.getWidth(), thisbutton.getHeight(), Bitmap.Config.ARGB_8888);
Canvas m = new Canvas(shadowbm);
thisbutton.draw(m);
thisbutton.setVisibility(View.GONE);
shadow.setImageBitmap(shadowbm);

shadow.setX(thisbutton.getLeft());
shadow.setY(thisbutton.getTop());
shadow.setVisibility(View.VISIBLE);

只要您可以在新画布上绘制,就可以制作任何想要的阴影.只需记住在位图上调用destroy()即可释放内存.

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.

这篇关于如何显示自定义DragShadow而不是ListViewFragment行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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