在我的项目中,ClipData和Dragshadowbuilder未知(不解析符号clipdata)

时间:2014-07-14 10:50:59

标签: android

ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());


String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
    mimeTypes, item);

// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);

// Starts the drag
view.startDrag(dragData,  // the data to be dragged
    myShadow,  // the drag shadow builder
    null,      // no need to use local data
    0          // flags (not currently used, set to 0)
);

return true;  //To change body of

1 个答案:

答案 0 :(得分:5)

你的问题过于笼统,无法确定你究竟需要什么,但我猜你有点复制了drag and drop tutorial of the official docs,在这种情况下,请注意,当你实例化拖动阴影生成器时,你会这样做一个名为MyDragShadowBuilder的类,其定义如下。

整个班级看起来像这样:

public class MainActivity extends ActionBarActivity {

    // Create a string for the ImageView label
    private static final String IMAGEVIEW_TAG = "icon bitmap";

    // Creates a new ImageView
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_to_be_dragged);
        imageView.setImageResource(R.mipmap.ic_launcher);
        // Sets the tag
        imageView.setTag(IMAGEVIEW_TAG);

        // Sets a long click listener for the ImageView using an anonymous listener object that implements the OnLongClickListener interface
        imageView.setOnLongClickListener(new View.OnLongClickListener() {

        // Defines the one method for the interface, which is called when the View is long-clicked
        public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            ClipData.Item item = new ClipData.Item((String)v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = new ClipData((CharSequence) v.getTag(), new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, item);

            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(imageView);

            // Starts the drag
            v.startDrag(dragData,  // the data to be dragged
                    myShadow,  // the drag shadow builder
                    null,      // no need to use local data
                    0          // flags (not currently used, set to 0)
            );
            return true;
        }
        });
    }

    private static class MyDragShadowBuilder extends View.DragShadowBuilder {

        // The drag shadow image, defined as a drawable thing
        private static Drawable shadow;

        // Defines the constructor for myDragShadowBuilder
        public MyDragShadowBuilder(View v) {

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

            // Creates a draggable image that will fill the Canvas provided by the system.
            shadow = new ColorDrawable(Color.LTGRAY);
        }

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

            // Defines local variables
            int width, height;

            // Sets the width of the shadow to half the width of the original View
            width = getView().getWidth()/2;

            // Sets the height of the shadow to half the height of the original View
            height = getView().getHeight()/2;

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

            // Sets the size parameter's width and height values. These get back to the system
            // through the size parameter.
            size.set(width,height);

            // Sets the touch point's position to be in the middle of the drag shadow
            touch.set(width/2,height/2);
         }

         // 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) {

            // Draws the ColorDrawable in the Canvas passed in from the system.
            shadow.draw(canvas);
        }
    }
}

修改

现在我想,提问者的问题可能更简单,他只需写下以下内容:

View.DragShadowBuilder myShadow = new View.DragShadowBuilder(ima);

因此创建了默认阴影。提问者错过了View.