I have an App that loads a Bitmap in a ImageView using the Android Studio Emulator with a Small Device.
When I execute a Zoom function by Matrix setScale it brakes without giving any Exception. I tried using a Try/catch coding and I have a default catch Thread in the app but it does not work.
I use the exact same code with a Medium Device and others but they work nicely, only the Small Device brakes.
The suspect error is the one immediately after the last Zoom code [Unable to match the desired swap behavior.] in the Logcat. Maybe something with the Matrix operation generates an internal error but is not making a catchable Exception.
How do I solve this?
Here is my Zoom function:
slider.addOnChangeListener { slider, value, _ ->
// Responds to when slider's value is changed
try {
tv = "Zoom Value:\n${value.format(2)}%"
zoom.text = tv
scaleX = 1 + value / 100
scaleY = 1 + value / 100
slidervalue = value
val f = FloatArray(9)
m.getValues(f)
scrollX = f[Matrix.MTRANS_X]
scrollY = f[Matrix.MTRANS_Y]
pscrollX = scrollX
pscrollY = scrollY
Log.d("Before scale", "Before scale")
m.setScale(scaleX, scaleY, centerX, centerY)
Log.d("Before scale", "After matrix scale")
val bounds: RectF = getImageBounds(pdfview, m)
pdfview.imageMatrix = m
Log.d("Before scale", "After view set")
val pbounds: RectF = getImageBounds(pdfview)
Log.d("Slider p bounds", pbounds.toString())
} catch (e: Exception) {
Log.d("Slider Error", e.message.toString())
}
}
Default exception Tread:
super.onCreate(savedInstanceState)
Thread.setDefaultUncaughtExceptionHandler { _, e ->
//Log.d("Force Exit", e.stackTrace.toString())
Toast.makeText(
this@MainActivity,
"Application Terminated ${e.message.toString()}",
Toast.LENGTH_SHORT
).show()
exitProcess(2)
}
Layout:
<FrameLayout
android:id="@+id/frame"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="20dp"
android:background="@drawable/image_border"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/linear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_layout">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:contentDescription="@string/hidden" />
<ImageView
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/pdf"
android:cropToPadding="true"
app:layout_constraintDimensionRatio="1:1"
app:srcCompat="@color/material_dynamic_neutral80" />
</FrameLayout>
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frame"
app:layout_constraintVertical_bias="0.0">
<com.google.android.material.slider.Slider
android:id="@+id/slider"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="@string/zoomvalue"
android:valueFrom="0.0"
android:valueTo="300.0"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="30dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:paddingEnd="2dp"
android:text="@string/zoom_value"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintTop_toBottomOf="@+id/slider"
tools:ignore="RtlSymmetry" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/autoCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/switch_border"
android:checked="false"
android:enabled="false"
android:paddingStart="4dp"
android:tag="true"
android:text="@string/pan_center"
android:textSize="8sp"
android:tooltipText="@string/move_image_to_the_center"
android:visibility="gone"
tools:checked="false"
tools:ignore="RtlSymmetry" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_weight="1"
android:background="@drawable/switch_border"
android:checked="false"
android:enabled="false"
android:paddingStart="4dp"
android:tag="true"
android:text="@string/reset"
android:textSize="12sp"
android:textStyle="bold"
android:tooltipText="@string/undo_last_pan"
android:visibility="gone"
tools:checked="false"
tools:ignore="RtlSymmetry" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/clearpdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_weight="1"
android:background="@drawable/switch_border"
android:checked="false"
android:enabled="false"
android:paddingStart="4dp"
android:tag="true"
android:text="@string/clear_pdf"
android:textSize="12sp"
android:textStyle="bold"
android:tooltipText="@string/remove_pdf_to_look_for_another"
android:visibility="visible"
tools:checked="false"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>
The View looks:
When breaks notice the App is still running according to the debugger but the Emulator no longer shows the View:
Logcat:
Before scale com.mastering.pdfactivity D
Before scale
Before scale com.mastering.pdfactivity D
After matrix scale
Before scale com.mastering.pdfactivity D
After view set
Slider m bounds com.mastering.pdfactivity D RectF(-137.2351, -167.19812, 737.2351, 898.1981), cX=300.0, cY=365.5, msx=-118.85547, msy=-144.8056
Slider p bounds com.mastering.pdfactivity D RectF(-137.2351, -167.19812, 737.2351, 898.1981)
3956-3956 A com....android.googlequicksearchbox I onApplyWindowInsets: systemWindowInsets=Insets{left=0, top=48, right=0, bottom=48}
3956-3956 A com....android.googlequicksearchbox I onApplyWindowInsets: systemWindowInsets=Insets{left=0, top=48, right=0, bottom=48}
3956-4118 OpenGLRenderer com....android.googlequicksearchbox E Unable to match the desired swap behavior.
1131-1921 OpenGLRenderer com...le.android.apps.nexuslauncher E Unable to match the desired swap behavior.

