-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK-#] В пример Android Auto добавлен CustomRenderer для демонстрации отрисовки поверх карты
- Loading branch information
v.lazin
committed
Sep 26, 2024
1 parent
e08daec
commit de68dfc
Showing
8 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ru.dgis.sdk.demo.car | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Color | ||
import android.graphics.Paint | ||
import android.os.Build | ||
import android.view.Surface | ||
import androidx.annotation.RequiresApi | ||
|
||
class CustomRenderer { | ||
private var renderThread: RenderThread? = null | ||
|
||
fun start(surface: Surface) { | ||
renderThread = RenderThread(surface) | ||
renderThread?.start() | ||
} | ||
|
||
fun stop() { | ||
renderThread?.interrupt() | ||
renderThread?.join() | ||
renderThread = null | ||
} | ||
|
||
private class RenderThread(private val surface: Surface) : Thread() { | ||
private val renderIntervalMs = 1000L | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
override fun run() { | ||
var value = 0 | ||
|
||
while (!isInterrupted) { | ||
var canvas: Canvas? = null | ||
try { | ||
canvas = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
surface.lockHardwareCanvas() | ||
} else { | ||
surface.lockCanvas(null) | ||
} | ||
draw(value++, canvas) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} finally { | ||
if (canvas != null) { | ||
surface.unlockCanvasAndPost(canvas) | ||
} | ||
} | ||
|
||
try { | ||
sleep(renderIntervalMs) | ||
} catch (e: InterruptedException) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
private fun draw(value: Int, canvas: Canvas) { | ||
val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | ||
color = Color.BLACK | ||
style = Paint.Style.FILL | ||
} | ||
|
||
// Draw a black circle | ||
val centerX = 100f | ||
val centerY = 100f | ||
val radius = 50f | ||
canvas.drawCircle(centerX, centerY, radius, paint) | ||
|
||
// Draw the white text inside the circle | ||
paint.apply { | ||
color = Color.WHITE | ||
textSize = 30f | ||
textAlign = Paint.Align.CENTER | ||
} | ||
canvas.drawText("$value", centerX, centerY + paint.textSize / 3, paint) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ru.dgis.sdk.demo.car | ||
|
||
import ru.dgis.sdk.map.CameraBehaviour | ||
import ru.dgis.sdk.map.FollowPosition | ||
import ru.dgis.sdk.map.Map | ||
import ru.dgis.sdk.map.ZoomControlButton | ||
import ru.dgis.sdk.map.ZoomControlModel | ||
|
||
class MainScreenModel(private val map: Map) { | ||
private val zoomControlModel = ZoomControlModel(map) | ||
|
||
fun zoomIn() { | ||
zoomControlModel.setPressed(ZoomControlButton.ZOOM_IN, true) | ||
zoomControlModel.setPressed(ZoomControlButton.ZOOM_IN, false) | ||
} | ||
|
||
fun zoomOut() { | ||
zoomControlModel.setPressed(ZoomControlButton.ZOOM_OUT, true) | ||
zoomControlModel.setPressed(ZoomControlButton.ZOOM_OUT, false) | ||
} | ||
|
||
fun recenter() { | ||
map.camera.setBehaviour(CameraBehaviour(position = FollowPosition())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,65 @@ | ||
package ru.dgis.sdk.demo.car | ||
|
||
import android.content.Intent | ||
import android.view.Gravity | ||
import android.view.Surface | ||
import androidx.car.app.AppManager | ||
import androidx.car.app.CarToast | ||
import androidx.car.app.Screen | ||
import ru.dgis.sdk.Future | ||
import ru.dgis.sdk.androidauto.AndroidAutoMapSession | ||
import ru.dgis.sdk.androidauto.CopyrightMargins | ||
import ru.dgis.sdk.androidauto.CopyrightPosition | ||
import ru.dgis.sdk.map.Map | ||
import ru.dgis.sdk.map.MapOptions | ||
import ru.dgis.sdk.map.RenderedObjectInfo | ||
import ru.dgis.sdk.map.ScreenPoint | ||
|
||
class MapSession : AndroidAutoMapSession(MapOptions()) { | ||
class MapSession(mapOptions: MapOptions) : AndroidAutoMapSession(mapOptions) { | ||
private val mainScreen = MainScreen(carContext) | ||
private var map: Map? = null | ||
private var customRenderer = CustomRenderer() | ||
private var getRenderedObjectFuture: Future<List<RenderedObjectInfo>>? = null | ||
|
||
private fun showToast(message: String) { | ||
carContext.getCarService(AppManager::class.java) | ||
.showToast(message, CarToast.LENGTH_SHORT) | ||
carContext.getCarService(AppManager::class.java).showToast(message, CarToast.LENGTH_SHORT) | ||
} | ||
|
||
override fun onCreateScreen(intent: Intent): Screen { | ||
return MainScreen(carContext) | ||
return mainScreen | ||
} | ||
|
||
override fun onSurfaceAvailable(surface: Surface, width: Int, height: Int) { | ||
customRenderer.start(surface) | ||
} | ||
|
||
override fun onSurfaceClicked(x: Float, y: Float) { | ||
getRenderedObjectFuture = map?.getRenderedObjects(centerPoint = ScreenPoint(x = x, y = y))?.apply { | ||
onResult { | ||
val objectInfo = it.firstOrNull() | ||
showToast("$objectInfo") | ||
} | ||
} | ||
} | ||
|
||
override fun onSurfaceDestroyed(surface: Surface) { | ||
customRenderer.stop() | ||
} | ||
|
||
override fun onMapReady(map: Map) { | ||
this.map = map | ||
|
||
mainScreen.setMap(map) | ||
|
||
setCopyrightPosition( | ||
CopyrightPosition( | ||
gravity = Gravity.BOTTOM or Gravity.START, | ||
margins = CopyrightMargins(left = 20, bottom = 20) | ||
) | ||
) | ||
} | ||
|
||
override fun onMapReadyException(exception: Exception) { | ||
exception.message?.let(::showToast) | ||
} | ||
|
||
override fun onMapClicked(x: Float, y: Float) { | ||
map?.getRenderedObjects(centerPoint = ScreenPoint(x = x, y = y))?.onResult { | ||
val objectInfo = it.firstOrNull() | ||
showToast("$objectInfo") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="#fff" | ||
android:fillType="nonZero" | ||
android:pathData="M13,11h3v2h-3v3h-2v-3H8v-2h3V8h2v3z" /> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="#fff" | ||
android:fillType="nonZero" | ||
android:pathData="M8,11h8v2H8z" /> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters