Skip to content

Commit

Permalink
Merge pull request #9 from BlueBazze/master
Browse files Browse the repository at this point in the history
Zoomable android
  • Loading branch information
ThibaultBee committed Aug 18, 2022
2 parents a1fcbae + 9241bf4 commit 36124e5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
targetSdkVersion = 31

// StreamPack
streamPackVersion = "2.3.3"
streamPackVersion = "2.4.0"
}
repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ScaleGestureDetector
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
Expand All @@ -17,6 +18,45 @@ class PreviewFragment : Fragment() {
private val viewModel: PreviewViewModel by viewModels()
private lateinit var binding: FragmentPreviewBinding

/**
* Zooming gesture
*
* scaleFactor > 1 == Zooming in
* scaleFactor < 1 == Zooming out
*
* scaleFactor will start at a value of 1 when the gesture is begun.
* Then its value will persist until the gesture has ended.
* If we save the zoomRatio in savedScale when the gesture has begun,
* we can easily add a relative scale to the zoom.
*
* If we are zooming out, the scale is between 0-1.
* Meaning we can use this as a percentage from the savedScale
*
* Zooming in is linear zoom
* Zooming out is percentage zoom between 1f & savedScale
*/
private val pinchGesture: ScaleGestureDetector by lazy {
ScaleGestureDetector(
binding.apiVideoView.context,
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
private var savedZoomRatio: Float = 1f
override fun onScale(detector: ScaleGestureDetector): Boolean {
viewModel.zoomRatio = if (detector.scaleFactor < 1) {
savedZoomRatio * detector.scaleFactor
} else {
savedZoomRatio + ((detector.scaleFactor - 1))
}
return super.onScale(detector)
}

override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
detector.currentSpan
savedZoomRatio = viewModel.zoomRatio
return super.onScaleBegin(detector)
}
})
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -26,10 +66,15 @@ class PreviewFragment : Fragment() {
return binding.root
}

@SuppressLint("MissingPermission")
@SuppressLint("MissingPermission", "ClickableViewAccessibility")
override fun onResume() {
super.onResume()

// Listen to touch for zoom
binding.apiVideoView.setOnTouchListener { _, event ->
pinchGesture.onTouchEvent(event)
}

viewModel.buildLiveStream(binding.apiVideoView)
binding.liveButton.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
Expand All @@ -51,7 +96,6 @@ class PreviewFragment : Fragment() {
viewModel.switchCamera()
}


binding.muteButton.setOnClickListener {
viewModel.toggleMute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import androidx.annotation.RequiresPermission
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import video.api.livestream.ApiVideoLiveStream
import video.api.livestream.example.ui.utils.Configuration
import video.api.livestream.enums.CameraFacingDirection
import video.api.livestream.enums.Resolution
import video.api.livestream.example.ui.utils.Configuration
import video.api.livestream.interfaces.IConnectionChecker
import video.api.livestream.models.AudioConfig
import video.api.livestream.models.VideoConfig
Expand All @@ -22,6 +22,12 @@ class PreviewViewModel(application: Application) : AndroidViewModel(application)
val onError = MutableLiveData<String>()
val onDisconnect = MutableLiveData<Boolean>()

var zoomRatio: Float
get() = liveStream.zoomRatio
set(value) {
liveStream.zoomRatio = value
}

@RequiresPermission(allOf = [Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA])
fun buildLiveStream(apiVideoView: ApiVideoView) {
val audioConfig = AudioConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ constructor(
streamer.settings.audio.isMuted = value
}


/**
* Set/get the zoom ratio.
*/
var zoomRatio: Float
/**
* Get the zoom ratio.
*
* @return the zoom ratio
*/
get() = streamer.settings.camera.zoom.zoomRatio
/**
* Set the zoom ratio.
*
* @param value the zoom ratio
*/
set(value) {
streamer.settings.camera.zoom.zoomRatio = value
}

/**
* Start a new RTMP stream.
*
Expand Down

0 comments on commit 36124e5

Please sign in to comment.