改Demo用于测试双屏输出的Android主板验证支不支持双屏双触摸
MainActivity.Java
public class MainActivity extends AppCompatActivity {
DisplayManager mDisplayManager;
SecondDisplay mPresentation;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Settings.canDrawOverlays(this)){
showSecondDisplay();
}else {
showPermissionDialog();
}
}
private void showPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择允许出现在其他应用上");
builder.setMessage("请选择允许出现在其他应用上");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:"+ getPackageName()));
startActivityForResult(intent, 100);
}
});
builder.create().show();
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100){
if (Settings.canDrawOverlays(this)){
showSecondDisplay();
}else {
showPermissionDialog();
}
}
}
private void showSecondDisplay() {
mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
assert mDisplayManager != null;
Display[] displays = mDisplayManager.getDisplays();
Log.d("Dispalys:", Arrays.toString(displays));
if (mPresentation == null){
mPresentation = new SecondDisplay(this,displays[displays.length-1]);
mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mPresentation.show();
}
}
}
第二个屏幕弹框
SecondDisplay.java
package com.senjucao.doublescreendoubletouch;
import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
public class SecondDisplay extends Presentation {
Context context;
public SecondDisplay(Context outerContext, Display display) {
super(outerContext, display);
this.context = outerContext;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_display);
}
}
自定义的DrawView,按下画线:
public class DrawView extends View {
private static final String TAG = "OnDraw";
private Canvas canvas;
private Paint paint;
private Bitmap bitmap;
private float[] startX = new float[20];
private float[] startY = new float[20];
private float[] endX = new float[20];
private float[] endY = new float[20];
private int width;
private int height;
public DrawView(Context context) {
super(context);
paint = new Paint(Paint.DITHER_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context);
}
public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context);
}
public DrawView(Context context, @Nullable AttributeSet attrs) {
this(context);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap == null) return;
canvas.drawBitmap(bitmap, 0, 0, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (bitmap == null || canvas == null){
if (width==0||height ==0) return false;
bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
}
int action = event.getAction() & MotionEvent.ACTION_MASK;
int index = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "第一个手指按下");
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int i = 0; i < event.getPointerCount(); i++) {
startX[i] = event.getX(i);
startY[i] = event.getY(i);
canvas.drawPoint(startX[i], startY[i], paint);
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG, "第" + (index + 1) + "个手指按下");
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int i = 0; i < event.getPointerCount(); i++) {
startX[i] = event.getX(i);
startY[i] = event.getY(i);
canvas.drawPoint(startX[i], startY[i], paint);
}
break;
case MotionEvent.ACTION_MOVE:
for (int i = 0; i < event.getPointerCount(); i++) {
int pointerId = event.getPointerId(i);
Log.d(TAG, "pointerIndex=" + i + ",pointerId = " + pointerId);
endX[pointerId] = event.getX(i);
endY[pointerId] = event.getY(i);
canvas.drawLine(startX[pointerId], startY[pointerId], endX[pointerId]
, endY[pointerId], paint);
startX[pointerId] = endX[pointerId];
startY[pointerId] = endY[pointerId];
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "MotionEvent.ACTION_UP");
break;
}
invalidate();
return true;
}
}
添加View到两个布局文件。
<com.senjucao.doublescreendoubletouch.DrawView
android:id="@+id/iv_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>
测试截图:
另外一个屏幕截不了,只能拍照
device-2019-12-20-161726.png
网友评论