mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Removed unnecessary object allocations during onDraw in WorldMapFragment
This commit is contained in:
1 parent
384613d155
commit
4f5ce50c56
1 file changed
+48
-54
@@ -21,7 +21,6 @@ package com.rtbishop.lookingsat.ui
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.graphics.Matrix
|
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Path
|
import android.graphics.Path
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
@@ -121,6 +120,8 @@ class WorldMapFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inner class TrackView(context: Context) : View(context) {
|
inner class TrackView(context: Context) : View(context) {
|
||||||
|
private val gsp = viewModel.gsp.value!!
|
||||||
|
private val scale = resources.displayMetrics.density
|
||||||
private val groundTrackPaint = Paint().apply {
|
private val groundTrackPaint = Paint().apply {
|
||||||
isAntiAlias = true
|
isAntiAlias = true
|
||||||
color = resources.getColor(R.color.satTrack, mainActivity.theme)
|
color = resources.getColor(R.color.satTrack, mainActivity.theme)
|
||||||
@@ -140,84 +141,77 @@ class WorldMapFragment : Fragment() {
|
|||||||
strokeWidth = scale
|
strokeWidth = scale
|
||||||
textSize = 16f
|
textSize = 16f
|
||||||
}
|
}
|
||||||
private val scale = resources.displayMetrics.density
|
|
||||||
private val gsp = viewModel.gsp.value!!
|
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
canvas.translate(width / 2f, height / 2f)
|
||||||
val frameWidth = mapFrame.measuredWidth
|
val degLon = width / 360f
|
||||||
val frameHeight = mapFrame.measuredHeight
|
val degLat = height / 180f
|
||||||
|
drawHomeLoc(canvas, degLon, degLat)
|
||||||
|
|
||||||
val currentTime = getDateFor(System.currentTimeMillis())
|
val currentTime = getDateFor(System.currentTimeMillis())
|
||||||
val orbitalPeriod = (24 * 60 / selectedSat.meanmo).toInt()
|
val orbitalPeriod = (24 * 60 / selectedSat.meanmo).toInt()
|
||||||
val satPosNow = predictor.getSatPos(currentTime)
|
|
||||||
val satPosList = predictor.getPositions(currentTime, 60, 0, orbitalPeriod * 3)
|
val satPosList = predictor.getPositions(currentTime, 60, 0, orbitalPeriod * 3)
|
||||||
|
drawGroundTrack(canvas, degLon, degLat, satPosList)
|
||||||
|
|
||||||
|
val satPosNow = predictor.getSatPos(currentTime)
|
||||||
val footprintPosList = satPosNow.rangeCircle
|
val footprintPosList = satPosNow.rangeCircle
|
||||||
drawFootprint(canvas, frameWidth, frameHeight, footprintPosList)
|
drawFootprint(canvas, degLon, degLat, footprintPosList)
|
||||||
drawGroundTrack(canvas, frameWidth, frameHeight, satPosList)
|
|
||||||
drawHomeLoc(canvas, frameWidth, frameHeight)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawGroundTrack(canvas: Canvas, width: Int, height: Int, list: List<SatPos>) {
|
private fun drawHomeLoc(cvs: Canvas, degLon: Float, degLat: Float) {
|
||||||
canvas.setMatrix(Matrix().apply {
|
val lon = gsp.longitude.toFloat()
|
||||||
postTranslate(width / 2f, height / 2f)
|
val lat = gsp.latitude.toFloat() * -1
|
||||||
})
|
val cx = lon * degLon
|
||||||
val trackPath = Path()
|
val cy = lat * degLat
|
||||||
var trackX: Float
|
cvs.drawCircle(cx, cy, scale * 2, txtPaint)
|
||||||
var trackY: Float
|
cvs.drawText("GSP", cx - txtPaint.textSize, cy - txtPaint.textSize, txtPaint)
|
||||||
var prevX = 181f
|
}
|
||||||
|
|
||||||
|
private fun drawGroundTrack(cvs: Canvas, degLon: Float, degLat: Float, list: List<SatPos>) {
|
||||||
|
val path = Path()
|
||||||
|
var lon: Float
|
||||||
|
var lat: Float
|
||||||
|
var lastLon = 181f
|
||||||
|
|
||||||
list.withIndex().forEach { (index, satPos) ->
|
list.withIndex().forEach { (index, satPos) ->
|
||||||
trackX = rad2Deg(satPos.longitude).toFloat()
|
lon = rad2Deg(satPos.longitude).toFloat()
|
||||||
trackY = rad2Deg(satPos.latitude).toFloat() * -1
|
lat = rad2Deg(satPos.latitude).toFloat() * -1
|
||||||
|
|
||||||
if (trackX > 180f) trackX -= 360f
|
if (lon > 180f) lon -= 360f
|
||||||
|
|
||||||
trackX *= width / 360f
|
lon *= degLon
|
||||||
trackY *= height / 180f
|
lat *= degLat
|
||||||
|
|
||||||
if (index == 0 || abs(trackX - prevX) > 180) trackPath.moveTo(trackX, trackY)
|
if (index == 0 || abs(lon - lastLon) > 180) path.moveTo(lon, lat)
|
||||||
else trackPath.lineTo(trackX, trackY)
|
else path.lineTo(lon, lat)
|
||||||
|
|
||||||
prevX = trackX
|
lastLon = lon
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas.drawPath(trackPath, groundTrackPaint)
|
cvs.drawPath(path, groundTrackPaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawFootprint(canvas: Canvas, width: Int, height: Int, list: List<Position>) {
|
private fun drawFootprint(cvs: Canvas, degLon: Float, degLat: Float, list: List<Position>) {
|
||||||
canvas.setMatrix(Matrix().apply {
|
val path = Path()
|
||||||
postTranslate(width / 2f, height / 2f)
|
var lon: Float
|
||||||
})
|
var lat: Float
|
||||||
val printPath = Path()
|
var lastLon = 181f
|
||||||
var printX: Float
|
|
||||||
var printY: Float
|
|
||||||
var prevX = 181f
|
|
||||||
|
|
||||||
list.withIndex().forEach { (index, position) ->
|
list.withIndex().forEach { (index, position) ->
|
||||||
printX = position.lon.toFloat()
|
lon = position.lon.toFloat()
|
||||||
printY = position.lat.toFloat() * -1
|
lat = position.lat.toFloat() * -1
|
||||||
|
|
||||||
if (printX > 180f) printX -= 360f
|
if (lon > 180f) lon -= 360f
|
||||||
|
|
||||||
printX *= width / 360f
|
lon *= degLon
|
||||||
printY *= height / 180f
|
lat *= degLat
|
||||||
|
|
||||||
if (index == 0 || abs(printX - prevX) > 180) printPath.moveTo(printX, printY)
|
if (index == 0 || abs(lon - lastLon) > 180) path.moveTo(lon, lat)
|
||||||
else printPath.lineTo(printX, printY)
|
else path.lineTo(lon, lat)
|
||||||
|
|
||||||
prevX = printX
|
lastLon = lon
|
||||||
}
|
}
|
||||||
canvas.drawPath(printPath, footprintPaint)
|
cvs.drawPath(path, footprintPaint)
|
||||||
}
|
|
||||||
|
|
||||||
private fun drawHomeLoc(canvas: Canvas, frameWidth: Int, frameHeight: Int) {
|
|
||||||
canvas.setMatrix(Matrix().apply {
|
|
||||||
postTranslate(frameWidth / 2f, frameHeight / 2f)
|
|
||||||
})
|
|
||||||
val cx = frameWidth / 360f * gsp.longitude.toFloat()
|
|
||||||
val cy = frameHeight / 180f * gsp.latitude.toFloat() * -1
|
|
||||||
canvas.drawCircle(cx, cy, scale * 2, txtPaint)
|
|
||||||
canvas.drawText("GSP", cx - txtPaint.textSize, cy - txtPaint.textSize, txtPaint)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDateFor(value: Long): Date {
|
private fun getDateFor(value: Long): Date {
|
||||||
|
|||||||
Reference in new issue
Block a user