publicclassMainActivityextendsAppCompatActivity {private ImageView imageView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("test", "One:" + "onCreate");
setImageViewEvent();
}
//监听是不是进入了分屏模式@OverridepublicvoidonMultiWindowModeChanged(boolean isInMultiWindowMode) {
super.onMultiWindowModeChanged(isInMultiWindowMode);
Log.i("test", "One:" + "onMultiWindowModeChanged" + isInMultiWindowMode);
}
//当Activity进入画中画模式时,系统会回调这个方法@OverridepublicvoidonPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);
Log.i("test", "One:" + "onPictureInPictureModeChanged" + isInPictureInPictureMode);
}
//如果用户重新调整窗口的大小,系统在必要的时候也可能触发@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("test", "One:" + "onConfigurationChanged"+newConfig.toString());
}
@OverrideprotectedvoidonStart() {
super.onStart();
Log.i("test", "One:" + "onStart");
}
@OverrideprotectedvoidonRestart() {
super.onRestart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.i("test", "One:" + "onRestart" + ",判断是否处于分屏模式:" + this.isInMultiWindowMode() + ",判断是否处于画中画模式" + this.isInPictureInPictureMode());
} else {
Log.i("test", "One:" + "onRestart");
}
}
//分屏模式从失去焦点——》得到焦点,执行@OverrideprotectedvoidonResume() {
super.onResume();
Log.i("test", "One:" + "onResume");
}
//分屏模式下失去焦点,执行@OverrideprotectedvoidonPause() {
super.onPause();
Log.i("test", "One:" + "onPause");
}
@OverrideprotectedvoidonStop() {
super.onStop();
Log.i("test", "One:" + "onStop");
}
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
Log.i("test", "One:" + "onDestroy");
}
//打开第二个activitypublicvoidopenSencondActivity(View view) {
//1、当前MainActivity已经进入到分屏模式。//2、新打开的Main2Activity支持分屏浏览(即android:resizeableActivity=true)。//满足1和2,打开的Main2Activity将与MainActivity实现分屏
startActivity(new Intent(this, Main2Activity.class).addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK));
/**
设置FLAG_ACTIVITY_NEW_TASK的原因:
1、在同一个Activity返回栈中,打开一个新的Activity时,这个Activity将会继承上一个Activity所有和分屏模式有关的属性。
如果你想要在一个独立的窗口以分屏模式打开一个新的Activity,那么必须新建一个Activity返回栈。
*//**
此外,如果你的设备支持自由模式(官方名字叫freeform,暂且就这么翻译它,其实我认为这算也是一种尺寸更自由的分屏模式,
如果设备厂商支持用户可以自由改变Activity的尺寸,那么就相当于支持自由模式,这将比普通的分屏模式更加自由),
打开一个Activity时,还可通过ActivityOptions.setLaunchBounds()来指定新的Activity的尺寸和在屏幕中的位置。
同样,这个方法也需要你的Activity已经处于分屏模式时,调用它才会生效。
*/
}
/**
* 支持拖放
* 用户可以在两个 Activity 共享屏幕的同时在这两个 Activity 之间拖放数据 (在此之前,用户只能在一个 Activity 内部拖放数据)。
*/publicvoidsetImageViewEvent() {
imageView = (ImageView) findViewById(R.id.imageView);
/** 拖拽的发送方Activity和ImageView */
imageView.setTag("I'm a ImageView from MainActivity");
imageView.setOnTouchListener(new View.OnTouchListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
publicbooleanonTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/** 构造一个ClipData,将需要传递的数据放在里面 */
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(view.getTag().toString(), mimeTypes, item);
MyDragShadowBuilder shadow = new MyDragShadowBuilder(imageView);//替换当前正在进行的拖动操作的拖动阴影。只能由发起拖动操作的应用调用/** startDragAndDrop是Android N SDK中的新方法,替代了以前的startDrag(),
*flag需要设置为DRAG_FLAG_GLOBAL ( 需要传递View.DRAG_FLAG_GLOBAL来实现跨Activity拖拽。)
* 如果需要将URI权限传递给接收方Activity,还可以根据需要设置View.DRAG_FLAG_GLOBAL_URI_READ或者View.DRAG_FLAG_GLOBAL_URI_WRITE。
* */
view.startDragAndDrop(dragData, shadow, null, View.DRAG_FLAG_GLOBAL);
/**
* Activity.requestDropPermissions()
请求使用 DragEvent 中包含的 ClipData 传递的内容 URI 的权限。
*/returntrue;
} else {
returnfalse;
}
}
});
}
class MyDragShadowBuilder extends View.DragShadowBuilder {
// The drag shadow image, defined as a drawable thingprivate Drawable shadow;
// Defines the constructor for myDragShadowBuilderpublicMyDragShadowBuilder(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.@OverridepublicvoidonProvideShadowMetrics(Point size, Point touch) {
// Defines local variablesint 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().@OverridepublicvoidonDrawShadow(Canvas canvas) {
// Draws the ColorDrawable in the Canvas passed in from the system.
shadow.draw(canvas);
}
}
}