下面列出了android.view.DragEvent#getClipDescription ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onDrag(View view, DragEvent event) {
// DragTarget is peeking into the MIME types of the dragged event in order to ignore
// non-image drags completely.
// DragSource does not do that but rejects non-image content once a drop has happened.
ClipDescription clipDescription = event.getClipDescription();
if (clipDescription != null && !clipDescription.hasMimeType("image/*")) {
return false;
}
// Callback received when image is being dragged.
return super.onDrag(view, event);
}
/**
* DragEvents can contain additional data packaged in a {@link PersistableBundle}.
* Extract the extras from the event and return the String stored for the
* {@link #EXTRA_IMAGE_INFO} entry.
*/
private String getExtra(DragEvent event) {
// The extras are contained in the ClipDescription in the DragEvent.
ClipDescription clipDescription = event.getClipDescription();
if (clipDescription != null) {
PersistableBundle extras = clipDescription.getExtras();
if (extras != null) {
return extras.getString(EXTRA_IMAGE_INFO);
}
}
return null;
}
@Override
public boolean onDrag(View view, DragEvent event) {
// DragTarget is peeking into the MIME types of the dragged event in order to ignore
// non-image drags completely.
// DragSource does not do that but rejects non-image content once a drop has happened.
ClipDescription clipDescription = event.getClipDescription();
if (clipDescription != null && !clipDescription.hasMimeType("image/*")) {
return false;
}
// Callback received when image is being dragged.
return super.onDrag(view, event);
}
/**
* DragEvents can contain additional data packaged in a {@link PersistableBundle}.
* Extract the extras from the event and return the String stored for the
* {@link #EXTRA_IMAGE_INFO} entry.
*/
private String getExtra(DragEvent event) {
// The extras are contained in the ClipDescription in the DragEvent.
ClipDescription clipDescription = event.getClipDescription();
if (clipDescription != null) {
PersistableBundle extras = clipDescription.getExtras();
if (extras != null) {
return extras.getString(EXTRA_IMAGE_INFO);
}
}
return null;
}
boolean processDragStarted(DragEvent event) {
// Determine whether to continue processing drag and drop based on the
// plain text mime type.
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
boolean processDragStarted(DragEvent event) {
// Determine whether to continue processing drag and drop based on the
// plain text mime type.
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
boolean processDragStarted(DragEvent event) {
// Determine whether to continue processing drag and drop based on the
// plain text mime type.
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
boolean processDragStarted(DragEvent event) {
// Determine whether to continue processing drag and drop based on the
// plain text mime type.
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
boolean processDragStarted(DragEvent event) {
// Determine whether to continue processing drag and drop based on the
// plain text mime type.
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
private boolean onDragStart(DragEvent event) {
if (!mRequest.isValid()) {
return false;
}
ClipDescription desc = event.getClipDescription();
if (desc == null || !desc.hasMimeType(getMimeType())) {
return false;
}
final PendingAddItemInfo item;
if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
item = new PendingAddShortcutInfo(
new PinShortcutRequestActivityInfo(mRequest, mLauncher));
} else {
// mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET
LauncherAppWidgetProviderInfo providerInfo =
LauncherAppWidgetProviderInfo.fromProviderInfo(
mLauncher, mRequest.getAppWidgetProviderInfo(mLauncher));
final PinWidgetFlowHandler flowHandler =
new PinWidgetFlowHandler(providerInfo, mRequest);
item = new PendingAddWidgetInfo(providerInfo) {
@Override
public WidgetAddFlowHandler getHandler() {
return flowHandler;
}
};
}
View view = new View(mLauncher);
view.setTag(item);
Point downPos = new Point((int) event.getX(), (int) event.getY());
DragOptions options = new DragOptions();
options.systemDndStartPoint = downPos;
options.preDragCondition = this;
// We use drag event position as the screenPos for the preview image. Since mPreviewRect
// already includes the view position relative to the drag event on the source window,
// and the absolute position (position relative to the screen) of drag event is same
// across windows, using drag position here give a good estimate for relative position
// to source window.
PendingItemDragHelper dragHelper = new PendingItemDragHelper(view);
if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET) {
dragHelper.setPreview(getPreview(mRequest));
}
dragHelper.startDrag(new Rect(mPreviewRect),
mPreviewBitmapWidth, mPreviewViewWidth, downPos, this, options);
mDragStartTime = SystemClock.uptimeMillis();
return true;
}
/**
* @see View#onDragEvent(DragEvent)
*/
@TargetApi(Build.VERSION_CODES.N)
public boolean onDragEvent(DragEvent event) {
if (mNativeContentViewCore == 0 || Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
return false;
}
ClipDescription clipDescription = event.getClipDescription();
// text/* will match text/uri-list, text/html, text/plain.
String[] mimeTypes =
clipDescription == null ? new String[0] : clipDescription.filterMimeTypes("text/*");
if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
// TODO(hush): support dragging more than just text.
return mimeTypes != null && mimeTypes.length > 0
&& nativeIsTouchDragDropEnabled(mNativeContentViewCore);
}
StringBuilder content = new StringBuilder("");
if (event.getAction() == DragEvent.ACTION_DROP) {
// TODO(hush): obtain dragdrop permissions, when dragging files into Chrome/WebView is
// supported. Not necessary to do so for now, because only text dragging is supported.
ClipData clipData = event.getClipData();
final int itemCount = clipData.getItemCount();
for (int i = 0; i < itemCount; i++) {
ClipData.Item item = clipData.getItemAt(i);
content.append(item.coerceToStyledText(mContainerView.getContext()));
}
}
int[] locationOnScreen = new int[2];
mContainerView.getLocationOnScreen(locationOnScreen);
float xPix = event.getX() + mCurrentTouchOffsetX;
float yPix = event.getY() + mCurrentTouchOffsetY;
int xCss = (int) mRenderCoordinates.fromPixToDip(xPix);
int yCss = (int) mRenderCoordinates.fromPixToDip(yPix);
int screenXCss = (int) mRenderCoordinates.fromPixToDip(xPix + locationOnScreen[0]);
int screenYCss = (int) mRenderCoordinates.fromPixToDip(yPix + locationOnScreen[1]);
nativeOnDragEvent(mNativeContentViewCore, event.getAction(), xCss, yCss, screenXCss,
screenYCss, mimeTypes, content.toString());
return true;
}