dat-ng / ar-location-based-android

This AR app generally show where things are in the real-world by indicating where the app thinks they are over the camera view when the user holds the phone up and moves it about.
MIT License
206 stars 85 forks source link

Not able to add TextView using Relative Layout. #6

Open parita-detroja opened 7 years ago

parita-detroja commented 7 years ago

I am creating location based AR app using Kotlin, in which i used RelativeLayout and added TextView in that RelativeLayout as below code.

class AROverlayByViewGroup(context: Context) : RelativeLayout(context) { val TAG: String = "AROverlayByViewGroup" var rotatedProjectionMatrix: FloatArray = kotlin.FloatArray(16) private var currentLocation: Location? = null var arPoints: MutableList = ArrayList() val cameraCoordinateVector: FloatArray = kotlin.FloatArray(4)

var parentHeight: Int? = null
var parentWidth: Int? = null

fun updateARPoints(arPoints: MutableList<ARPoint>)
{
    this.arPoints = arPoints
}

fun updateRotatedProjectionMatrix(rotatedProjectionMatrix: FloatArray)
{
    this.rotatedProjectionMatrix = rotatedProjectionMatrix
}

fun updateCurrentLocation(location: Location)
{
    this.currentLocation = location
    this.invalidate()
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

    if (currentLocation == null) {
        return
    }

    for ((location, name) in arPoints) {
        val currentLocationInECEF: FloatArray = LocationHelper.WSG84toECEF(currentLocation!!)
        val pointInECEF: FloatArray = LocationHelper.WSG84toECEF(location)
        val pointInENU: FloatArray = LocationHelper.ECEFtoENU(currentLocation!!, currentLocationInECEF, pointInECEF)

        Matrix.multiplyMV(cameraCoordinateVector, 0, rotatedProjectionMatrix, 0, pointInENU, 0)

        if (cameraCoordinateVector[2] < 0) {

            val x: Float = (0.5f + cameraCoordinateVector[0] / cameraCoordinateVector[3]) * parentWidth!!
            val y: Float = (0.5f - cameraCoordinateVector[1] / cameraCoordinateVector[3]) * parentHeight!!

            val mTextView: TextView = TextView(this.context)
            mTextView.text = name
            mTextView.setTextColor(Color.BLUE)
            mTextView.textSize = 50F

            val params = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT)
            params.leftMargin = x.toInt()
            params.topMargin = y.toInt()

            this.addView(mTextView, params)

        }
    }
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)

    parentWidth = MeasureSpec.getSize(widthMeasureSpec)
    parentHeight = MeasureSpec.getSize(heightMeasureSpec)

    Log.e(TAG, "width: " + parentWidth.toString())
    Log.e(TAG, "height: " + parentHeight.toString())
}

}

I am not getting any textview. What do i need to change for this?