Describe the bug
Background: I want use Android native view like PDFView in Android Compose, so I should use a AndroidView to wrap it. And also I want my parent compose component set a scale factor by Modifier.scale().
Here is my code :
Box(
Modifier
.fillMaxSize()
.background(Color.Yellow),
contentAlignment = Alignment.Center
) {
Box(
modifier = Modifier
.size(250F.dp, 80F.dp)
.scale(1.5F)
.background(Color.Gray),
) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = {
val pdfView = generatePDFView()
pdfView
}) {
}
}
}
After that, I found the touch area of PDFView is not match the display area after scaling, but the touch area of compose component is right.
So, I guess that the scale modifier of Android Compose just handle the display effect of Android native view but does not handle its touch interaction properly
Affected platforms Select one of the platforms below:
- Android
- Other
Versions
- Jetpack Compose version: Using composeBom, it version is : platform('androidx.compose:compose-bom:2022.10.00')
- Android Studio Build: Android Studio Dolphin | 2021.3.1
- Kotlin version: 1.7.20
- JDK (for desktop issues): 11
To Reproduce Steps and/or the code snippet to reproduce the behavior, let's reproduce by a TextView:
- Create a very simple Activity and setContent { // Compose content } in onCreate method
- The compose content also very simple , Box -> Box -> AndroidView -> TextView
- Scale the second Box to 1.5F and the set the text attribute of TextView with a long long string
- TextView which nested in the scaled Box, has a complete wrong touch area!Only some left top area can click or scroll
class DemoScaleActivity : AppCompatActivity() {
val TAG = "DemoScaleActivity"
private var scope = CoroutineScope(Dispatchers.Main)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeStandardDoubleBox()
}
}
@Composable
private fun ComposeStandardDoubleBox() {
Box(
Modifier
.fillMaxSize()
.background(Color.Yellow),
contentAlignment = Alignment.Center
) {
Box(
modifier = Modifier
.size(250F.dp, 80F.dp)
.scale(1.5F)
.background(Color.Gray),
) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = {
val pdfView = generatePDFView()
pdfView
}) {
}
}
}
}
private fun generateTextView(): TextView {
val textView = TextView(this).apply {
movementMethod = ScrollingMovementMethod.getInstance()
//必须,垂直滚动,与布局中的scrollbars="vertical"等价
isVerticalScrollBarEnabled = true;
// 非必须,滚动条的位置样式
scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY;
// 非必须,滚动条的隐藏时长
scrollBarFadeDuration = 1000;
// 非必须
isSingleLine = false;
// 非必须,是否允许滚动条隐藏
isVerticalFadingEdgeEnabled = true;
// 必须
movementMethod = ScrollingMovementMethod.getInstance();
setBackgroundColor(android.graphics.Color.RED)
text = generateTextString()
setOnClickListener {
Log.d("zzz", " !!!!! ")
}
}
return textView
}
private fun generateTextString(): String {
val aStr = "a".repeat(20)
val bStr = "b".repeat(20)
val cStr = "c".repeat(20)
val dStr = "d".repeat(20)
val result = StringBuilder()
repeat(40) {
result
.append(aStr).append("\n")
.append(bStr).append("\n")
.append(cStr).append("\n")
.append(dStr).append("\n")
}
return result.toString()
}
}
Screenshots
The correct touch interaction area without Scale modifier: https://user-images.githubusercontent.com/3962054/232718577-24fa0aec-6061-449a-80b8-d34b1c2d7b01.png
The wrong touch interaction area with Scale modifier: https://user-images.githubusercontent.com/3962054/232718306-f38facda-b08b-469f-9806-01a38302570f.png
I hope the TextView / PDFView has a correct touch interaction area after scaling the Box