Android SensorEvent是否具有初始值设定项?

Android SensorEvent是否具有初始值设定项?,android,kotlin,Android,Kotlin,所以我被一个小问题绊倒了。我计划实现一个位图文本,其中包括gps位置和it传感器。位置检测不到任何问题,即使我将要共享的代码没有得到任何位置坐标,但这就是它的目的。但是,我的传感器确实有问题。我尝试在传感器事件上使用空序列,但没有覆盖fun onSensorChanged(事件:SensorEvent?,一切正常,但显示空结果,这是合理的原因。虽然我试图将其更改为非null,但这确实会导致null指针错误。甚至我尝试将SensorEvent设置为lateinit,但它显示了未正确初始化的错误。有

所以我被一个小问题绊倒了。我计划实现一个位图文本,其中包括gps位置和it传感器。位置检测不到任何问题,即使我将要共享的代码没有得到任何位置坐标,但这就是它的目的。但是,我的传感器确实有问题。我尝试在传感器事件上使用空序列,但没有覆盖fun onSensorChanged(事件:SensorEvent?,一切正常,但显示空结果,这是合理的原因。虽然我试图将其更改为非null,但这确实会导致null指针错误。甚至我尝试将SensorEvent设置为lateinit,但它显示了未正确初始化的错误。有没有办法只固定传感器的值0、1和2(方位角、俯仰和滚动)来显示?先谢谢你

private lateinit var mSensorManager: SensorManager
private var mSensorEvent: SensorEvent? = null

val listener: SensorEventListener? = null
                    mSensorManager.registerListener(listener,
                            mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                            SensorManager.SENSOR_DELAY_GAME
                    )

                    val azimuthWMText = mSensorEvent?.values?.get(0).toString()
                    val pitchWMText = mSensorEvent?.values?.get(1).toString()
                    val rollWMText = mSensorEvent?.values?.get(2).toString()

                    //Bitmap that contains the addWatermark method and detecting the new photo path that is been taken and implements the watermark
                    val originalBitmap = BitmapFactory.decodeFile(path.toString(), BitmapFactory.Options())?.let { it ->
                        addWatermark(
                                it,
                                firstWatermarkText = "Дължина: $longitudeWM${resources.getString(R.string.degrees)}, Ширина: $latitudeWM${resources.getString(R.string.degrees)}",
                                secondWatermarkText = "Височина: ${altitudeWM}м, Ориентация: $orientationWM",
                                thirdWatermarkText = "Точност: Хоризонтална: ${hozAccuracyWM}м, Вертикална: ${verAccuracyWM}м",
                                fourthWatermarkText = "Посока: where $azimuthWMText${resources.getString(R.string.degrees)}",
                                fifthWatermarkText = "Наклон: pitchTilt $pitchWMText${resources.getString(R.string.degrees)}, rollTilt $rollWMText${resources.getString(R.string.degrees)}",
                                WatermarkOptions(
                                        Corner.TOP_LEFT,
                                        textSizeToWidthRation = 0.017f,
                                        paddingToWidthRatio = 0.03f,
                                        Color.parseColor("#FFFB00"),
                                        shadowColor = Color.BLACK,
                                        strokeOutline = Color.BLACK,
                                        typeface = null
                                )
                        )
                    }
                    previewView.bitmap.let { originalBitmap }

//Adding watermark method is here for declaring on to the top
    private fun addWatermark(bitmap: Bitmap, firstWatermarkText: String, secondWatermarkText: String, thirdWatermarkText: String, fourthWatermarkText: String, fifthWatermarkText: String, options: WatermarkOptions = WatermarkOptions()): Bitmap {
        val result = bitmap.copy(bitmap.config, true)
        val canvas = Canvas(result)
        val paint = Paint(ANTI_ALIAS_FLAG or DITHER_FLAG)
        val strokePaint = Paint()

        //Whe are including the Enum class and connecting with the data class WatermarkOptions variable
        paint.textAlign = when (options.corner) {
            //We include the alignment LEFT from Enum class and connecting with Paint variable
            Corner.TOP_LEFT,
            Corner.BOTTOM_LEFT -> Paint.Align.LEFT

            //We include the alignment RIGHT from Enum class and connecting with Paint variable
            Corner.TOP_RIGHT,
            Corner.BOTTOM_RIGHT -> Paint.Align.RIGHT
        }

        strokePaint.textAlign = when (options.corner) {
            Corner.TOP_LEFT,
            Corner.BOTTOM_LEFT -> Paint.Align.LEFT

            Corner.TOP_RIGHT,
            Corner.BOTTOM_RIGHT -> Paint.Align.RIGHT
        }

        //We connect the new textSize variable with the bitmap width(default is 0) and we multiply with the WatermarkOption's textSize
        val textSize = result.width * options.textSizeToWidthRation
        //Connecting the Paint textSize variable with the new textSize variable
        paint.textSize = textSize//70.5f
        //Connecting the Paint color variable with the WatermarkOptions textColor
        paint.color = options.textColor
        //If the shadowColor of the WMOptions is not null, then we make it as a Paint shadowLayer variable
        if (options.shadowColor != null) {
            paint.setShadowLayer( 2.5f, 0f, 0f, options.shadowColor)
        }

        if (options.strokeOutline != null) {
            strokePaint.textSize = textSize//72f
            strokePaint.color = options.strokeOutline
            strokePaint.style = Paint.Style.STROKE
            strokePaint.strokeWidth = 5.17f
        }

        //If typeface of the WMOptions is not null,we make paint typeface variable and connecting with the WMOptions variable
        if (options.typeface != null) {
            paint.typeface = options.typeface
        }
        //We connect the new padding variable with the bitmap width(default is 0) and multiply with WMOptions padding
        val padding = result.width * options.paddingToWidthRatio

        //Create a variable that has something to do with the coordinates method
        val coordinates = calculateCoordinates(firstWatermarkText, secondWatermarkText, thirdWatermarkText, fourthWatermarkText, fifthWatermarkText, paint, options, canvas.width, canvas.height, padding)
        /**drawText text and stroke**/
        canvas.drawText(firstWatermarkText, coordinates.x, coordinates.y, strokePaint)
        canvas.drawText(firstWatermarkText, coordinates.x, coordinates.y, paint)

        canvas.drawText(secondWatermarkText, coordinates.x, 240f, strokePaint) //We change the Y horizontal coordinates by typing the float number
        canvas.drawText(secondWatermarkText, coordinates.x, 240f, paint)

        canvas.drawText(thirdWatermarkText, coordinates.x, 310f, strokePaint)
        canvas.drawText(thirdWatermarkText, coordinates.x, 310f, paint)

        canvas.drawText(fourthWatermarkText, coordinates.x, 380f, strokePaint)
        canvas.drawText(fourthWatermarkText, coordinates.x, 380f, paint)

        canvas.drawText(fifthWatermarkText, coordinates.x, 450f, strokePaint)
        canvas.drawText(fifthWatermarkText, coordinates.x, 450f, paint)

        return result
    }

    //This it he corner alignment calculation method and using it on the drawText
    private fun calculateCoordinates(
            firstWatermarkText: String,
            secondWatermarkText: String,
            thirdWatermarkText: String,
            fourthWatermarkText: String,
            fifthWatermarkText: String,
            paint: Paint,
            options: WatermarkOptions,
            width: Int,
            height: Int,
            padding: Float
    ): PointF {
        val x = when (options.corner) {
            Corner.TOP_LEFT,
                Corner.BOTTOM_LEFT -> {
                    padding
                }
            Corner.TOP_RIGHT,
                Corner.BOTTOM_RIGHT -> {
                    width - padding
                }
        }

        val y = when (options.corner) {
            Corner.BOTTOM_LEFT,
                Corner.BOTTOM_RIGHT -> {
                    height - padding
                }
            Corner.TOP_LEFT,
                Corner.TOP_RIGHT -> {
                    val bounds = Rect()

                    paint.getTextBounds(firstWatermarkText, 0, firstWatermarkText.length, bounds)
                    paint.getTextBounds(secondWatermarkText, 0, secondWatermarkText.length, bounds)
                    paint.getTextBounds(thirdWatermarkText, 0, thirdWatermarkText.length, bounds)
                    paint.getTextBounds(fourthWatermarkText, 0, fourthWatermarkText.length, bounds)
                    paint.getTextBounds(fifthWatermarkText, 0, fifthWatermarkText.length, bounds)

                    val textHeight = bounds.height()
                    textHeight + padding
                }
        }
        return PointF(x, y)
    }


enum class Corner {
        TOP_LEFT,
        TOP_RIGHT,
        BOTTOM_LEFT,
        BOTTOM_RIGHT
    }

    data class WatermarkOptions(
            val corner: Corner = Corner.BOTTOM_RIGHT,
            val textSizeToWidthRation: Float = 0.04f,
            val paddingToWidthRatio: Float = 0.03f,
            @ColorInt val textColor: Int = Color.parseColor("#FFC800"),
            @ColorInt val shadowColor: Int? = Color.BLACK,
            @ColorInt val strokeOutline: Int? = Color.BLACK,
            val typeface: Typeface? = null
    )

通过注册
SensorEventListener
,并在其
onSensorChanged(event:SensorEvent!)
方法中处理事件,可以对
SensorEvent
作出反应。你所有的
mSensorEvent
东西都需要放进去。()