In my project, ClipData and Dragshadowbuilder are unknown (do not allow symbolic clicks)

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
+4
source share
1 answer

Your question is too general to be sure what you need, but I think you copied this from a drag-and-drop white paper tutorial , and in this case, note that when you create an instance of the shadow drag builder, you execute it with the class MyDragShadowBuilderthat defined below.

The whole class may look like this:

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 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 width and height values. These get back to the system
            // through the size parameter.
            size.set(width,height);

            // Sets the touch point 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);
        }
    }
}

Edit:

Now I think that perhaps the problem for the seeker was simpler, and he just needed to write the following:

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

therefore, a default shadow is created. I apologizeView.

+4
source

Source: https://habr.com/ru/post/1548337/


All Articles