Skip to content

Commit

Permalink
Fullscreen graphic view in landscape mode
Browse files Browse the repository at this point in the history
  • Loading branch information
svetter committed Jul 3, 2024
1 parent aba9964 commit 33790a7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package com.gmail.simetist.stereomikingcalculator

import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ProgressBar
import android.widget.SeekBar
import android.widget.Switch
Expand All @@ -20,7 +25,6 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat



private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
Expand All @@ -46,6 +50,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var recAngleSlider: SeekBar
private lateinit var recAngleSliderTickLabels: Array<Pair<Int, TextView>>

private lateinit var graphicsFrameLayout: FrameLayout
private lateinit var graphicsView: StereoConfigView

private lateinit var micDistanceValueLabel: TextView
Expand Down Expand Up @@ -86,6 +91,7 @@ class MainActivity : AppCompatActivity() {
180 to findViewById(R.id.recAngleSliderTick180Label)
)

graphicsFrameLayout = findViewById(R.id.graphicsFrameLayout)
graphicsView = findViewById(R.id.graphicsView)

micDistanceValueLabel = findViewById(R.id.micDistanceValueLabel)
Expand Down Expand Up @@ -394,6 +400,8 @@ class MainActivity : AppCompatActivity() {
})
}



override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean("useInches", useInches)
Expand All @@ -404,4 +412,50 @@ class MainActivity : AppCompatActivity() {
outState.putInt("micDistance", micDistanceSlider.progress)
outState.putInt("micAngle", micAngleSlider.progress)
}

// Saved layout states during landscape orientation
private var portraitGraphicsFrameHeight = 0
private var portraitGraphicsFrameTopToBottom = 0
private var portraitGraphicsFrameStartToStart = 0
private var portraitGraphicsFrameEndToEnd = 0
private var portraitGraphicsFrameTopMargin = 0
private var portraitGraphicsFrameLeftMargin = 0
private var portraitGraphicsFrameRightMargin = 0

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
// Restore graphicsFrame's constraints
(graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).apply {
height = portraitGraphicsFrameHeight
topToBottom = portraitGraphicsFrameTopToBottom
startToStart = portraitGraphicsFrameStartToStart
endToEnd = portraitGraphicsFrameEndToEnd
topMargin = portraitGraphicsFrameTopMargin
leftMargin = portraitGraphicsFrameLeftMargin
rightMargin = portraitGraphicsFrameRightMargin
}
// Remove background color for graphicsFrameLayout
graphicsFrameLayout.setBackgroundColor(Color.TRANSPARENT)
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Save layout values
portraitGraphicsFrameHeight = graphicsFrameLayout.height
portraitGraphicsFrameTopToBottom = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).topToBottom
portraitGraphicsFrameStartToStart = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).startToStart
portraitGraphicsFrameEndToEnd = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).endToEnd
portraitGraphicsFrameTopMargin = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).topMargin
portraitGraphicsFrameLeftMargin = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).leftMargin
portraitGraphicsFrameRightMargin = (graphicsFrameLayout.layoutParams as ConstraintLayout.LayoutParams).rightMargin

// Make graphicsFrame take up the whole layout
graphicsFrameLayout.layoutParams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// Set background color for graphicsFrameLayout
graphicsFrameLayout.setBackgroundColor(Color.BLACK)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import android.graphics.Paint
import android.graphics.Path
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import kotlin.math.min
import kotlin.math.tan


Expand All @@ -19,6 +21,11 @@ class StereoConfigView(context: Context?, attrs: AttributeSet?) : View(context,
private val cardioidMicVector: Drawable? = ContextCompat.getDrawable(context!!, R.drawable.microphone_cardioid)
private val omniMicVector: Drawable? = ContextCompat.getDrawable(context!!, R.drawable.microphone_omni)

private val micHeightCm = 8f
private val micWidthCm = 2f
private val maxMicDistanceCm = 50f
private val halfCircleRadiusCm = maxMicDistanceCm / 2f

private var useOmni = false
private var micVector: Drawable? = cardioidMicVector

Expand All @@ -45,11 +52,14 @@ class StereoConfigView(context: Context?, attrs: AttributeSet?) : View(context,
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

val picturedWidth = 50 + 2 * 1.5f // 50cm max capsule distance + sqrt(2) for each mic when at max distance and 45°
val picturedWidth = maxMicDistanceCm + micWidthCm * 1.5f // Max mic distance (center to center) + sqrt(2) for each mic when at max distance and 45°
val picturedHeight = halfCircleRadiusCm + micHeightCm - 1 + 1 // Circle radius + mic length - 1cm for the top of the mic + 1cm buffer
// val pixelPerCm = min(width / picturedWidth, height / picturedHeight)
val pixelPerCm = width / picturedWidth
val micWidth = 2 * pixelPerCm
val micLength = 8 * pixelPerCm
val halfMicDistance = micDistance * pixelPerCm / 2.0f

val micWidth = micWidthCm * pixelPerCm
val micLength = micHeightCm * pixelPerCm
val halfMicDistance = micDistance * pixelPerCm / 2f

val centerX = width / 2f
val centerY = height - 8.5f * pixelPerCm
Expand All @@ -74,7 +84,7 @@ class StereoConfigView(context: Context?, attrs: AttributeSet?) : View(context,
val halfAngleWithXAxisRad = halfAngleWithXAxisDeg * Math.PI.toFloat() / 180
val halfRecAngleLineY = centerY - tan(halfAngleWithXAxisRad) * centerX

val circleRadius = 25 * pixelPerCm
val circleRadius = halfCircleRadiusCm * pixelPerCm

canvas.drawPath(Path().apply {
moveTo(centerX, centerY)
Expand All @@ -95,16 +105,20 @@ class StereoConfigView(context: Context?, attrs: AttributeSet?) : View(context,
canvas.drawLine(x, centerY, x, if (i % 2 == 0) longTickBottomY else shortTickBottomY, linesPaint)
}

// Angle lines
canvas.drawLine(centerX, centerY, left, recAngleLineY, linesPaint)
canvas.drawLine(centerX, centerY, right, recAngleLineY, linesPaint)
canvas.drawLine(centerX, centerY, left, halfRecAngleLineY, linesPaint)
canvas.drawLine(centerX, centerY, right, halfRecAngleLineY, linesPaint)

canvas.drawArc(
centerX - circleRadius, centerY - circleRadius,
centerX + circleRadius, centerY + circleRadius,
180f, 180f, false, linesPaint
)
if (height >= picturedHeight * pixelPerCm) {
// Half circle
canvas.drawArc(
centerX - circleRadius, centerY - circleRadius,
centerX + circleRadius, centerY + circleRadius,
180f, 180f, false, linesPaint
)
}

micVector?.let {
canvas.save()
Expand All @@ -121,6 +135,20 @@ class StereoConfigView(context: Context?, attrs: AttributeSet?) : View(context,
}
}

override fun onTouchEvent(event: android.view.MotionEvent): Boolean {
performClick()
return true
}

override fun performClick(): Boolean {
Log.i("StereoConfigView", "performClick")
// TODO

return super.performClick()
}



fun setUseOmni(useOmni: Boolean) {
this.useOmni = useOmni
if (useOmni) micAngle = 0f
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@

<FrameLayout
android:id="@+id/graphicsFrameLayout"
android:translationZ="1dp"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_marginStart="16dp"
Expand All @@ -581,8 +582,9 @@

<com.gmail.simetist.stereomikingcalculator.StereoConfigView
android:id="@+id/graphicsView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:translationZ="1dp" />
</FrameLayout>

<Switch
Expand Down

0 comments on commit 33790a7

Please sign in to comment.