SurfaceView、GlSurfaceView、Other
private fun showSimpleInputDialog(context: Activity) { val editText = EditText(context).apply { hint = "请输入内容" inputType = InputType.TYPE_CLASS_TEXT setPadding(50, 30, 50, 30) } container = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL } container.addView(editText) container.addView(Button(context).apply { setOnClickListener { capture(context) } }) container.addView(ImageView(context).apply { visibility = View.GONE setOnClickListener { visibility = View.GONE } }, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)) val dialog = AlertDialog.Builder(context) .setTitle("输入内容") .setView(container) .setPositiveButton("确定") { _, _ -> val input = editText.text.toString() Toast.makeText(context, "你输入了:$input", Toast.LENGTH_SHORT).show() } .setNegativeButton("取消", null) .create() dialog.show() } private fun capture(activity: Activity) { var targetView = GmSpaceViewTreeManager.findTargetView(activity.window.decorView) val rect = Rect(0, 0, DisplayUtils.getScreenWidth(activity), DisplayUtils.getScreenHeight(activity)) Log.d("dqs", ">>>>>>>>>>>>> ${rect.width()} ${rect.height()} $targetView") val bitmap = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888) if (targetView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(targetView is GLSurfaceView) { PixelCopy.request(targetView, bitmap, { res -> if (res == PixelCopy.SUCCESS) { val imageView: ImageView? = container.getChildAt(2) as ImageView? imageView?.visibility = View.VISIBLE imageView?.setImageBitmap(bitmap) Toast.makeText(activity, "操作成功", Toast.LENGTH_SHORT).show() } else { Toast.makeText(activity, "操作失败 $res", Toast.LENGTH_SHORT).show() } }, Handler(Looper.getMainLooper())) } else if(targetView is SurfaceView) { PixelCopy.request(targetView, bitmap, { res -> if (res == PixelCopy.SUCCESS) { val imageView: ImageView? = container.getChildAt(2) as ImageView? imageView?.visibility = View.VISIBLE imageView?.setImageBitmap(bitmap) Toast.makeText(activity, "操作成功", Toast.LENGTH_SHORT).show() } else { Toast.makeText(activity, "操作失败 $res", Toast.LENGTH_SHORT).show() } }, Handler(Looper.getMainLooper())) } else { PixelCopy.request(activity.window, bitmap, { res -> if (res == PixelCopy.SUCCESS) { val imageView: ImageView? = container.getChildAt(2) as ImageView? imageView?.visibility = View.VISIBLE imageView?.setImageBitmap(bitmap) Toast.makeText(activity, "操作成功", Toast.LENGTH_SHORT).show() } else { Toast.makeText(activity, "操作失败 $res", Toast.LENGTH_SHORT).show() } }, Handler(Looper.getMainLooper())) } } }
