Skip to content

Commit 0e454c0

Browse files
chengoakoak
andauthored
修复低版本 Android 显示二维码闪退 (#284)
* 修复低版本 Android 显示二维码闪退 * 修复低版本 Android 显示二维码闪退 --------- Co-authored-by: oak <zzoak@gmail.com>
1 parent 4eb8216 commit 0e454c0

2 files changed

Lines changed: 33 additions & 34 deletions

File tree

app/shared/src/main/kotlin/dev/aaa1115910/m3qrcode/MaterialShapeQrState.kt

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.graphics.Paint
66
import android.graphics.PorterDuff
77
import android.graphics.PorterDuffColorFilter
88
import android.graphics.RectF
9-
import android.graphics.drawable.VectorDrawable
9+
import android.graphics.drawable.Drawable
1010
import androidx.appcompat.content.res.AppCompatResources
1111
import androidx.compose.material3.ColorScheme
1212
import androidx.compose.runtime.Composable
@@ -37,15 +37,15 @@ import kotlin.random.Random
3737

3838
internal data class MaterialShapeQrState(
3939
private val context: Context,
40-
private val arrayOf1x1Shapes: List<VectorDrawable>,
41-
private val arrayOf1x1SemiCircleShapes: List<VectorDrawable>,
42-
private val arrayOf2x2Shapes: List<VectorDrawable>,
43-
private val arrayOf3x3Shapes: List<VectorDrawable>,
44-
private val arrayOf7x7Shapes: List<VectorDrawable>,
45-
private val arrayOfHorizontalBarShapes: List<VectorDrawable>,
46-
private val arrayOfHorizontalHalfCapsuleBarShapes: List<VectorDrawable>,
47-
private val arrayOfVerticalBarShapes: List<VectorDrawable>,
48-
private val arrayOfFinderPatternCenterShapes: List<VectorDrawable>,
40+
private val arrayOf1x1Shapes: List<Drawable>,
41+
private val arrayOf1x1SemiCircleShapes: List<Drawable>,
42+
private val arrayOf2x2Shapes: List<Drawable>,
43+
private val arrayOf3x3Shapes: List<Drawable>,
44+
private val arrayOf7x7Shapes: List<Drawable>,
45+
private val arrayOfHorizontalBarShapes: List<Drawable>,
46+
private val arrayOfHorizontalHalfCapsuleBarShapes: List<Drawable>,
47+
private val arrayOfVerticalBarShapes: List<Drawable>,
48+
private val arrayOfFinderPatternCenterShapes: List<Drawable>,
4949
private val foregroundColorPrimary: Int,
5050
private val foregroundColorSecondary: Int,
5151
private val foregroundColorAccent: Int,
@@ -116,7 +116,7 @@ internal data class MaterialShapeQrState(
116116
}
117117
}
118118

119-
fun randomSquare(shapeSize: Int): VectorDrawable {
119+
fun randomSquare(shapeSize: Int): Drawable {
120120
return when (shapeSize) {
121121
1 -> arrayOf1x1Shapes.random()
122122
2 -> arrayOf2x2Shapes.random()
@@ -126,23 +126,23 @@ internal data class MaterialShapeQrState(
126126
}
127127
}
128128

129-
fun randomHorizontalBar(barWidth: Int): VectorDrawable {
129+
fun randomHorizontalBar(barWidth: Int): Drawable {
130130
return arrayOfHorizontalBarShapes[barWidth - 2]
131131
}
132132

133-
fun randomHorizontalHalfCapsuleBar(barWidth: Int): VectorDrawable {
133+
fun randomHorizontalHalfCapsuleBar(barWidth: Int): Drawable {
134134
return arrayOfHorizontalHalfCapsuleBarShapes[barWidth - 2]
135135
}
136136

137-
fun randomVerticalBar(barHeight: Int): VectorDrawable {
137+
fun randomVerticalBar(barHeight: Int): Drawable {
138138
return arrayOfVerticalBarShapes[barHeight - 2]
139139
}
140140

141-
fun getSemiCircle(): VectorDrawable {
141+
fun getSemiCircle(): Drawable {
142142
return arrayOf1x1SemiCircleShapes.first()
143143
}
144144

145-
fun nextFinderPatternCenter(): VectorDrawable {
145+
fun nextFinderPatternCenter(): Drawable {
146146
val vectorDrawableArr = arrayOfFinderPatternCenterShapes
147147
val index = finderPatternCenterShapeIndex
148148
finderPatternCenterShapeIndex = index + 1
@@ -307,7 +307,7 @@ internal data class MaterialShapeQrState(
307307
width: Int,
308308
height: Int,
309309
list: MutableList<MaterialShapeRenderer>,
310-
vectorDrawable: VectorDrawable,
310+
vectorDrawable: Drawable,
311311
foregroundColor: Int,
312312
rendererConfigs: (MaterialShapeRenderer) -> Unit
313313
) {
@@ -332,7 +332,7 @@ internal data class MaterialShapeQrState(
332332
width: Int,
333333
height: Int,
334334
list: MutableList<MaterialShapeRenderer>,
335-
vectorDrawable: VectorDrawable,
335+
vectorDrawable: Drawable,
336336
rendererConfigs: (MaterialShapeRenderer) -> Unit
337337
) {
338338
createRendererForShape(
@@ -594,22 +594,21 @@ internal fun rememberMaterialShapeQrState(
594594
}
595595
}
596596

597-
val arrayOf1x1Shapes = remember { mutableStateListOf<VectorDrawable>() }
598-
val arrayOf1x1SemiCircleShapes = remember { mutableStateListOf<VectorDrawable>() }
599-
val arrayOf2x2Shapes = remember { mutableStateListOf<VectorDrawable>() }
600-
val arrayOf3x3Shapes = remember { mutableStateListOf<VectorDrawable>() }
601-
val arrayOf7x7Shapes = remember { mutableStateListOf<VectorDrawable>() }
597+
val arrayOf1x1Shapes = remember { mutableStateListOf<Drawable>() }
598+
val arrayOf1x1SemiCircleShapes = remember { mutableStateListOf<Drawable>() }
599+
val arrayOf2x2Shapes = remember { mutableStateListOf<Drawable>() }
600+
val arrayOf3x3Shapes = remember { mutableStateListOf<Drawable>() }
601+
val arrayOf7x7Shapes = remember { mutableStateListOf<Drawable>() }
602602

603-
val arrayOfHorizontalBarShapes = remember { mutableStateListOf<VectorDrawable>() }
604-
val arrayOfHorizontalHalfCapsuleBarShapes = remember { mutableStateListOf<VectorDrawable>() }
605-
val arrayOfVerticalBarShapes = remember { mutableStateListOf<VectorDrawable>() }
606-
val arrayOfFinderPatternCenterShapes = remember { mutableStateListOf<VectorDrawable>() }
603+
val arrayOfHorizontalBarShapes = remember { mutableStateListOf<Drawable>() }
604+
val arrayOfHorizontalHalfCapsuleBarShapes = remember { mutableStateListOf<Drawable>() }
605+
val arrayOfVerticalBarShapes = remember { mutableStateListOf<Drawable>() }
606+
val arrayOfFinderPatternCenterShapes = remember { mutableStateListOf<Drawable>() }
607607

608608
val colorMap = remember { mutableStateMapOf<String, Int>() }
609609

610-
fun loadVectorDrawable(resId: Int): VectorDrawable {
611-
val drawable = AppCompatResources.getDrawable(context, resId)
612-
return drawable as VectorDrawable
610+
fun loadVectorDrawable(resId: Int): Drawable {
611+
return AppCompatResources.getDrawable(context, resId)!!
613612
}
614613

615614
fun loadDrawables() {

app/shared/src/main/kotlin/dev/aaa1115910/m3qrcode/MaterialShapeRenderer.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.graphics.Canvas
44
import android.graphics.Paint
55
import android.graphics.Rect
66
import android.graphics.RectF
7-
import android.graphics.drawable.VectorDrawable
7+
import android.graphics.drawable.Drawable
88
import android.os.Build
99
import androidx.core.graphics.withRotation
1010
import androidx.core.graphics.withSave
@@ -18,7 +18,7 @@ class MaterialShapeRenderer {
1818
var isMotionPaused: Boolean = false
1919
var paint: Paint
2020
var skipStartProgress: Float = 0f
21-
var srcImgSvg: VectorDrawable
21+
var srcImgSvg: Drawable
2222
var startDelay: Long = 0
2323

2424
companion object {
@@ -35,7 +35,7 @@ class MaterialShapeRenderer {
3535
}
3636
}
3737

38-
constructor(srcImgSvg: VectorDrawable, destRect: RectF, paint: Paint) {
38+
constructor(srcImgSvg: Drawable, destRect: RectF, paint: Paint) {
3939
this.srcImgSvg = srcImgSvg
4040
this.destRect = destRect
4141
this.paint = paint
@@ -167,4 +167,4 @@ class MaterialShapeRenderer {
167167
}
168168
}
169169
}
170-
}
170+
}

0 commit comments

Comments
 (0)