Optimized drawing

This commit is contained in:
Alexey 2025-08-11 16:56:43 +03:00
commit be85e92397
4 changed files with 27 additions and 14 deletions

View file

@ -4,6 +4,14 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2025-08-08T10:50:02.571721922Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/secondbeam/.android/avd/Pixel_3.avd" />
</handle>
</Target>
</DropdownSelection>
<DialogSelection />
</SelectionState> </SelectionState>
</selectionStates> </selectionStates>
</component> </component>

View file

@ -12,13 +12,13 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.KtHeightMap" android:theme="@style/Theme.KtHeightMap"
tools:targetApi="31"> tools:targetApi="31">
<profileable android:shell="true" />
<activity <activity
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.KtHeightMap"> android:theme="@style/Theme.KtHeightMap">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View file

@ -30,8 +30,8 @@ fun MapCanvas(
tileContainer: TileContainer, tileContainer: TileContainer,
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
val offsetX = rememberSaveable { mutableFloatStateOf(0F) } val offsetX = rememberSaveable { mutableFloatStateOf(-TILE_SIZE) }
val offsetY = rememberSaveable { mutableFloatStateOf(0F) } val offsetY = rememberSaveable { mutableFloatStateOf(-TILE_SIZE) }
val textMeasurer = rememberTextMeasurer() val textMeasurer = rememberTextMeasurer()
Canvas( Canvas(
modifier = modifier.fillMaxSize() modifier = modifier.fillMaxSize()
@ -50,15 +50,15 @@ fun MapCanvas(
val level = scale.floatValue.toInt() val level = scale.floatValue.toInt()
if (oldLevel > level) { if (oldLevel > level) {
offsetX.floatValue -= size.width / 2F offsetX.floatValue -= size.width / 2F + TILE_SIZE
offsetY.floatValue -= size.height / 2F offsetY.floatValue -= size.height / 2F + TILE_SIZE
offsetX.floatValue /= 2F offsetX.floatValue /= 2F
offsetY.floatValue /= 2F offsetY.floatValue /= 2F
} else if (oldLevel < level) { } else if (oldLevel < level) {
offsetX.floatValue *= 2F offsetX.floatValue *= 2F
offsetY.floatValue *= 2F offsetY.floatValue *= 2F
offsetX.floatValue += size.width / 2F offsetX.floatValue += size.width / 2F + TILE_SIZE
offsetY.floatValue += size.height / 2F offsetY.floatValue += size.height / 2F + TILE_SIZE
} }
val tileOffsetX = (offsetX.floatValue / TILE_SIZE).toInt() val tileOffsetX = (offsetX.floatValue / TILE_SIZE).toInt()
val tileOffsetY = (offsetY.floatValue / TILE_SIZE).toInt() val tileOffsetY = (offsetY.floatValue / TILE_SIZE).toInt()
@ -82,7 +82,7 @@ fun MapCanvas(
val tileY = tileOffsetY + cellY val tileY = tileOffsetY + cellY
val localOffsetY = TILE_SIZE * (cellY - 1) val localOffsetY = TILE_SIZE * (cellY - 1)
val bitmap = tiles.find { it.x == tileX && it.y == tileY && it.level == level }?.getBitmap() val bitmap = tiles.find { it.x == tileX && it.y == tileY && it.level == level }?.toBitmap()
bitmap?.let { bitmap?.let {
val imageBitmap = bitmap.asImageBitmap() val imageBitmap = bitmap.asImageBitmap()
@ -91,7 +91,7 @@ fun MapCanvas(
topLeft = Offset(localOffsetX, localOffsetY) - offset topLeft = Offset(localOffsetX, localOffsetY) - offset
) )
} }
/*
drawRect( drawRect(
color = gridColor, color = gridColor,
size = Size(TILE_SIZE, TILE_SIZE), size = Size(TILE_SIZE, TILE_SIZE),
@ -111,6 +111,7 @@ fun MapCanvas(
topLeft = Offset(localOffsetX, localOffsetY) - offset, topLeft = Offset(localOffsetX, localOffsetY) - offset,
size = Size(TILE_SIZE, TILE_SIZE) size = Size(TILE_SIZE, TILE_SIZE)
) )
*/
} }
} }
} }

View file

@ -4,6 +4,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Ignore
import java.util.Base64 import java.util.Base64
@Entity(tableName = "tiles", primaryKeys = [ "x", "y", "level" ]) @Entity(tableName = "tiles", primaryKeys = [ "x", "y", "level" ])
@ -20,6 +21,9 @@ class Tile {
@ColumnInfo(name = "data") @ColumnInfo(name = "data")
var base64: String? = null var base64: String? = null
@Ignore
var bitmap: Bitmap? = null
constructor() constructor()
constructor(x: Int, y: Int, level: Int) { constructor(x: Int, y: Int, level: Int) {
@ -35,11 +39,11 @@ class Tile {
this.base64 = base64 this.base64 = base64
} }
fun getBitmap(): Bitmap? { fun toBitmap(): Bitmap? {
if (this.base64 == null) if (bitmap != null || base64 == null)
return null return bitmap
val ba = Base64.getDecoder().decode(this.base64) val ba = Base64.getDecoder().decode(this.base64)
val bmp = BitmapFactory.decodeByteArray(ba, 0, ba.size) bitmap = BitmapFactory.decodeByteArray(ba, 0, ba.size)
return bmp return bitmap
} }
} }