。。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImageView = (ImageView)findViewById(R.id.image);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
mImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (Build.VERSION.SDK_INT >= 24) {
//通过Uri创建ClipData.Item对象,Uri是文件地址
final File folder = new File(Environment.getDataDirectory(), "system");
ClipData.Item item = new ClipData.Item(Uri.fromFile(new File(folder, "haha.jpg")));
//通过两个参数的构造函数创建ClipData
String[] mimeTypes = {"image/png"};
ClipData clipData = new ClipData(new ClipDescription("iamge drag", mimeTypes), item);
//建立shaldow
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDragAndDrop(clipData, shadowBuilder, null, View.DRAG_FLAG_GLOBAL);
}
return true;
} else {
return false;
}
}
});
}
|