需求背景,在开机动画结束后弹出触屏校验 :
效果图 :

实现方法 :使用WindowManager在launcher上面添加View
具体代码:

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager/*** @ClassName ScreenTestSimple.java* @author youdianxiaohuai* @Description 触屏校验* @createTime 2022年03月01日*/
@SuppressLint("StaticFieldLeak")
object ScreenTestSimple {//监听触屏校验完成var onScreenTestOverListener: (() -> Unit)? = nullprivate lateinit var context: Contextprivate var touchPassed = falseprivate var content: View? = null@Suppress("DEPRECATION")private val params: WindowManager.LayoutParams by lazy {val params = WindowManager.LayoutParams()params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERRORparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE orWindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREENparams.format = PixelFormat.TRANSLUCENTparams.width = WindowManager.LayoutParams.MATCH_PARENTparams.height = WindowManager.LayoutParams.MATCH_PARENTparams.x = 0params.y = 0params.gravity = Gravity.CENTERparams}private val windowManager: WindowManager by lazy {context.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager}/*** 使用时调用start方法*/fun start(context: Context) {this.context = contextcontent = TouchView(context)//防止第二次调用时touchPassed = truetouchPassed = falsetry {//防止view没有被remove掉windowManager.addView(content, params)} catch (e: RuntimeException) {windowManager.removeView(content)}}/***触屏校验完成时调用**/private fun onFinish() {windowManager.updateViewLayout(content, params)onScreenTestOverListener?.invoke()windowManager.removeView(content)content = null}/*** 触屏校验的VIEW*/class TouchView(context: Context) : View(context) {private val COL_WIDTH_HEIGHT = 31private val screenDisplay: Point by lazy {val point = Point()windowManager.defaultDisplay.getRealSize(point)point}private val screenWidth by lazy { screenDisplay.x }private val screenHeight by lazy { screenDisplay.y }private val matrixBitmap: Bitmap by lazy {Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.RGB_565)}private val matrixCanvas: Canvas by lazy {val canvas = Canvas(matrixBitmap)canvas.drawColor(-1)canvas}private val widthBasis: Int by lazy { screenWidth / COL_WIDTH_HEIGHT }private val heightBasis: Int by lazy { screenHeight / COL_WIDTH_HEIGHT }private val widthBasisCross: Int by lazy { 2 * (widthBasis - 2) }private val heightBasisCross: Int by lazy { widthBasisCross }private val colWidth: Float by lazy { screenWidth.toFloat() / widthBasis }private val colHeight: Float by lazy { screenHeight.toFloat() / heightBasis }private val widthCross: Float by lazy { colWidth / 2.0f }private val heightCross: Float by lazy { (screenHeight - colHeight) / (4 + (widthBasisCross - 1)) }private val draw: Array<IntArray> by lazy { Array(heightBasis) { IntArray(widthBasis) } }private val isDrawArea: Array<IntArray> by lazy { Array(heightBasis) { IntArray(widthBasis) } }private val drawCross = IntArray(2 * heightBasisCross)private var touchedX = 0.0fprivate var touchedY = 0.0fprivate var preTouchedX = 0.0fprivate var preTouchedY = 0.0fprivate var isTouchDown = falseprivate val linePaint: Paint by lazy {val linePaint = Paint()linePaint.isAntiAlias = truelinePaint.isDither = truelinePaint.style = Paint.Style.STROKElinePaint.strokeJoin = Paint.Join.ROUNDlinePaint.strokeCap = Paint.Cap.SQUARElinePaint.strokeWidth = 5.0fval localDashPathEffect = DashPathEffect(floatArrayOf(5.0f, 5.0f), 1.0f)linePaint.pathEffect = localDashPathEffectlinePaint.color = -16777216linePaint}private val clickPaint: Paint by lazy {val clickPaint = Paint()clickPaint.isAntiAlias = falseclickPaint.style = Paint.Style.FILLclickPaint.color = -16711936clickPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)clickPaint}private val emptyPaint: Paint by lazy {val emptyPaint = Paint()emptyPaint.isAntiAlias = falseemptyPaint.color = -1emptyPaint}/*** 处理滑动点击事件*/@SuppressLint("ClickableViewAccessibility")override fun onTouchEvent(event: MotionEvent): Boolean {if (event.getToolType(0) == 2 || touchPassed) {return if (touchPassed) {super.onTouchEvent(event)} else {true}}when (event.action) {MotionEvent.ACTION_DOWN -> drawDown(event)MotionEvent.ACTION_UP -> drawUp(event)MotionEvent.ACTION_MOVE -> drawMove(event)else -> {}}return true}override fun onDraw(canvas: Canvas) {canvas.drawBitmap(matrixBitmap, 0.0f, 0.0f, null)}init {val paint1 = Paint()val paint2 = Paint()paint1.color = -16777216paint2.color = -16777216paint2.style = Paint.Style.STROKE//先全部画格子for (i in 0..heightBasis) {val y = (colHeight * i)matrixCanvas.drawLine(0.0f, y, screenWidth.toFloat(), y, paint1)}for (i in 0..widthBasis) {val x = (colWidth * i)matrixCanvas.drawLine(x, 0.0f, x, screenHeight.toFloat(), paint1)}//把中间涂白  480时的涂白高度范围如果不上下缩小1会少点线,但是600的不会,应该是精度问题,其他也会,全部都这样好了matrixCanvas.drawRect(1 + colWidth, 1 + colHeight,colWidth * (widthBasis - 1) - 1, colHeight * (heightBasis - 1) - 1, emptyPaint)//记录进数组for (i in 0 until heightBasis) {for (j in 0 until widthBasis) {draw[i][j] = 0if (i == 0 || i == heightBasis - 1 || j == 0 || j == widthBasis - 1) {isDrawArea[i][j] = 1}}}//再画斜向和记录进数组for (i in 0 until heightBasisCross) {val x1 = (widthCross * (i + 2))val y1 = heightCross * (i + 2)matrixCanvas.drawRect(x1, y1, x1 + colWidth / 2, y1 + colHeight, paint2)drawCross[i] = 0val x3 = (widthCross * (i + 2))val y3 = (screenHeight - (colHeight + heightCross * (i + 2)))matrixCanvas.drawRect(x3, y3, x3 + colWidth / 2, y3 + colHeight, paint2)drawCross[i + heightBasisCross] = 0}}private fun isPass(): Boolean {for (i in 0 until heightBasis) {for (j in 0 until widthBasis) {if (isDrawArea[i][j] == 1) {if (draw[i][j] == 0) {return false}}}}return true}private fun isPassCross(): Boolean {for (i in 0 until 2 * heightBasisCross) {if (drawCross[i] == 0) {return false}}return true}private fun drawDown(event: MotionEvent) {touchedX = event.xtouchedY = event.yisTouchDown = drawRect(touchedX, touchedY, clickPaint)}private fun drawRect(x: Float, y: Float, paint: Paint): Boolean {val xIndex = (x / colWidth).toInt()val yIndex = (y / colHeight).toInt()if (xIndex >= 0 && yIndex >= 0 && xIndex < widthBasis && yIndex < heightBasis) {if (draw[yIndex][xIndex] == 0) {draw[yIndex][xIndex] = 1if (isDrawArea[yIndex][xIndex] != 0) {val rectX = colWidth * xIndexval rectY = colHeight * yIndexmatrixCanvas.drawRect(rectX,colHeight * yIndex,rectX + colWidth,rectY + colHeight,paint)invalidate()}}if (xIndex > 0 && xIndex < widthBasis - 1) {checkCrossRectRegion(x, y, paint)}if (isPass() && isPassCross()) {touchPassed = trueonFinish()return false}}return true}private fun checkCrossRectRegion(x: Float, y: Float, paint: Paint) {val crossIndex = ((x - colWidth) / widthCross).toInt()val crossX = (widthCross * (crossIndex + 2))val crossY = (heightCross * (crossIndex + 2))if (y > crossY && y < crossY + colHeight) {if (drawCross[crossIndex] == 0) {matrixCanvas.drawRect(crossX,crossY,crossX + widthCross,crossY + colHeight,paint)invalidate()drawCross[crossIndex] = 1}}val crossY2 = screenHeight - colHeight - crossYif (y > crossY2 && y < crossY2 + colHeight) {val index = 2 * widthBasisCross - 1 - crossIndexif (drawCross[index] == 0) {matrixCanvas.drawRect(crossX,crossY2,(crossX + colWidth / 2.0f),(crossY2 + colHeight),paint)invalidate()drawCross[index] = 1}}}private fun drawMove(event: MotionEvent) {if (isTouchDown) {preTouchedX = touchedXpreTouchedY = touchedYtouchedX = event.xtouchedY = event.ymatrixCanvas.drawLine(preTouchedX, preTouchedY, touchedX, touchedY, linePaint)invalidate()isTouchDown = drawRect(touchedX, touchedY, clickPaint)}}private fun drawUp(paramMotionEvent: MotionEvent) {if (isTouchDown) {preTouchedX = touchedXpreTouchedY = touchedYtouchedX = paramMotionEvent.xtouchedY = paramMotionEvent.yif (preTouchedX == touchedX && preTouchedY == touchedY) {matrixCanvas.drawPoint(touchedX, touchedY, linePaint)invalidate()}isTouchDown = false}}}
}

Android触屏校验相关推荐

  1. android 代码功能测试,Android触屏测试实例代码

    本文实例详细描述了Android触屏测试代码,可实现对触屏的点击.移动.离开等事件的处理,对于Android初学者有很好的借鉴价值. 具体功能代码如下: package com.test; impor ...

  2. android 触摸 事件,Android触屏事件和MotionEvent详解

    Android屏幕操作 屏幕是用户和Android设备交互的主要媒介,屏幕分为触屏和非触屏.Android设备目前有四种类型:Android Phone,Android Tablet,Android ...

  3. android 触摸时震动,android触屏震动以及震动强度批改

    android触屏震动以及震动强度修改 PhoneWindowManager.java: 在interceptKeyBeforeQueueing函数中 performHapticFeedbackLw( ...

  4. [Android] 触屏setOnTouchListener实现图片缩放、移动、绘制和添加水印

        前一篇文章讲述了Android实现图片Matrix矩阵类缩放.旋转.对比度.亮度.饱和度处理,但是真正的图片软件都是使用触屏实现图片缩放.移动.添加水印等功能,所以该篇文章主要通过setOnT ...

  5. android触屏设备event模拟,android 使用命令模拟点击 滑动

    adb shell getevent -p 出现上述截图的就是屏幕触摸输入设备 带上 -l adb shell getevent -p-l . EV_ABS      ABS_MT_TRACKING_ ...

  6. android触屏压力方案,触摸屏实现原理与在android上实现.doc

    PAGE 6 武汉工程大学邮电与信息工程学院毕业设计(论文) 武汉工程大学邮电与信息工程学院 毕业设计(论文) 触摸屏的实现原理及在android上的实现 The Principle of the T ...

  7. android触屏压力方案,如何捕捉压力和面积超过触摸屏android

    我正在使用Android 3.2.1宏基A500(电容屏)10"平板电脑我试图捕捉触摸屏幕的手指区域,压力在屏幕上,我尝试过Motionevent.getsize(),我总是得到0的值.另外 ...

  8. android触屏音文件地址,Android音视频-音频采集

    Android的音视频开发是我暂定的一个职业发展的一个方向,通过自学记录一些记了又忘记的知识. 音频基础知识 采样率(samplerate) 蓝色代表模拟音频信号,红色的点代表采样得到的量化数值. 采 ...

  9. unity android 触屏,Unity 移动端触摸屏操作

    using UnityEngine; using System.Collections; public class AndroidTouch : MonoBehaviour { private int ...

最新文章

  1. linux grep命令详解
  2. jsp/servlet学习笔记(核心编程)mysql部分
  3. 查询 oracle_关于oracle和mysql数据库的查询问题
  4. 【动画技巧】在Flash中自定义鼠标外观
  5. JAVA方法 字符串与unicode的相互转换
  6. 2017年10月2日日志
  7. 随想录(编写简单资源管理代码)
  8. matlab在化学中的应用举例,MATLAB在化学中的应用
  9. winserve2016 万能驱动网卡_万能网卡驱动win10
  10. JS 在线预览Word,Excel
  11. 什么是TPS,什么是QPS?
  12. R语言25-Prosper 贷款数据分析1
  13. 川大博士生被华为以200万年薪录用!分享以下科研及论文写作经验
  14. 已解决,软件V2报错 failed to read response header > websocket: close 1005 (no status)问题
  15. 急!程序员夫妻结婚了,婚戒上刻什么字好?
  16. 论文阅读:CTF:Anomaly Detection in High-Dimensional Time Series with Coarse-to-Fine Model Transfer
  17. windows 命令行查找字符串 和 文件(find findstr for)
  18. 《易经》与计算机科学技术的关系
  19. 2021-2027全球与中国汽车CMOS图像传感器市场现状及未来发展趋势
  20. Docker安装Mysql8、并打成镜像在其他内网环境部署

热门文章

  1. 【Java】Assert.assertEquals断言
  2. 1 --> Flexbuild 在 LS1046 中应用记录
  3. 乘号的计算机语言,我用易语言编了个计算器,乘号和除号都没用,帮我找下原因...
  4. Android 4.0发布了
  5. 慕课网python就业_python全栈开发慕课网
  6. PPC版QQ掉线问题解决方法
  7. Delphi文件读写操作常用的方法
  8. 静态代码扫描工具—— TScanCode
  9. python getattribute方法_Python:避免getattribute中的无限循环__
  10. 工作用用到的字符串转时间工具--后续会继续总结更新!