Skip to content

Commit

Permalink
preview page add.
Browse files Browse the repository at this point in the history
  • Loading branch information
ozcanalasalvar committed Jun 2, 2023
1 parent c61835a commit 7c4af5c
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.os.Bundle
import android.os.CountDownTimer
import android.provider.MediaStore
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.ScaleGestureDetector
Expand All @@ -26,14 +25,17 @@ import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.ozcanalasalvar.camerax.CameraVM
import com.ozcanalasalvar.camerax.R
import com.ozcanalasalvar.camerax.databinding.FragmentCameraBinding
import com.ozcanalasalvar.camerax.utils.DateFormats
import com.ozcanalasalvar.camerax.utils.DateFormatter
import com.ozcanalasalvar.camerax.utils.MediaType
import com.ozcanalasalvar.camerax.utils.getAspectRationString
import com.ozcanalasalvar.camerax.utils.getDimensionRatioString
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -370,6 +372,14 @@ class CameraFragment : Fragment() {
private fun showPreview(uri: Uri) {
binding.previewImage.setImageURI(uri)
binding.pnlPreview.visibility = View.VISIBLE


binding.pnlPreview.setOnClickListener {
val bundle = Bundle()
bundle.putString(ARG_PREVIEW_TYPE, MediaType.IMAGE)
bundle.putString(ARG_MEDIA_PATH, uri.toString())
findNavController().navigate(R.id.previewFragment, bundle)
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.ozcanalasalvar.camerax.fragments

import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.MediaController
import androidx.navigation.fragment.findNavController
import com.ozcanalasalvar.camerax.R
import com.ozcanalasalvar.camerax.databinding.FragmentCameraBinding
import com.ozcanalasalvar.camerax.databinding.FragmentPreviewBinding
import com.ozcanalasalvar.camerax.utils.MediaType
import java.io.File


const val ARG_PREVIEW_TYPE = "previewType"
const val ARG_MEDIA_PATH = "mediaPath"


class PreviewFragment : Fragment() {


private lateinit var binding: FragmentPreviewBinding

private var mediaType: String? = null
private var mediaUri: Uri? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
binding = FragmentPreviewBinding.inflate(inflater, container, false)
return binding.root
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
mediaType = it.getString(ARG_PREVIEW_TYPE)
mediaUri = Uri.parse(it.getString(ARG_MEDIA_PATH))
}

requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

when (mediaType) {
MediaType.IMAGE -> {
binding.videoViewer.visibility = View.GONE
binding.controllerView.visibility = View.GONE
binding.ivPreview.visibility = View.VISIBLE
binding.ivPreview.setImageURI(mediaUri)
}

MediaType.VIDEO -> {
binding.videoViewer.visibility = View.VISIBLE
binding.controllerView.visibility = View.VISIBLE
binding.ivPreview.visibility = View.GONE
val mc = MediaController(requireContext())
val params: FrameLayout.LayoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
)
params.bottomMargin = 200
mc.layoutParams = params


mc.setAnchorView(binding.controllerView)



binding.videoViewer.apply {
setVideoURI(mediaUri)
setMediaController(mc)
requestFocus()
}.start()
}
}

binding.ivBack.setOnClickListener {
findNavController().popBackStack()
}
}

override fun onDestroyView() {
super.onDestroyView()
requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.ozcanalasalvar.camerax.utils.CameraConstant
import com.ozcanalasalvar.camerax.utils.DateFormats
import com.ozcanalasalvar.camerax.utils.DateFormatter
import com.ozcanalasalvar.camerax.utils.FileManager
import com.ozcanalasalvar.camerax.utils.MediaType
import com.ozcanalasalvar.camerax.utils.getDimensionRatioString
import com.ozcanalasalvar.camerax.utils.getNameString
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -436,11 +437,18 @@ class VideoFragment : Fragment() {

binding.previewImage.setImageBitmap(bitmapThumbnail);


binding.pnlPreview.setOnClickListener {
val bundle = Bundle()
bundle.putString(ARG_PREVIEW_TYPE, MediaType.VIDEO)
bundle.putString(ARG_MEDIA_PATH, uri.toString())
findNavController().navigate(R.id.previewFragment, bundle)
}

requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR

}


private fun aspectRatio(width: Int, height: Int): Int {
val previewRatio = max(width, height).toDouble() / min(width, height)
if (abs(previewRatio - CameraConstant.RATIO_4_3_VALUE) <= abs(previewRatio - CameraConstant.RATIO_16_9_VALUE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ozcanalasalvar.camerax.utils

class MediaType {
companion object {
const val VIDEO = "video"
const val IMAGE = "image"
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,11H6.83l3.58,-3.59L9,6l-6,6 6,6 1.41,-1.41L6.83,13H21z"/>
</vector>
63 changes: 63 additions & 0 deletions app/src/main/res/layout/fragment_preview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".fragments.PreviewFragment">


<ImageView
android:id="@+id/ivPreview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="top|center_horizontal"
android:alpha="1.0"
android:scaleType="fitCenter"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<VideoView
android:id="@+id/video_viewer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center|center"
android:layout_margin="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="V,9:16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<FrameLayout
android:id="@+id/controllerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/video_viewer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/ivBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:src="@drawable/back"
android:background="@color/black80"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
7 changes: 7 additions & 0 deletions app/src/main/res/navigation/nav_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
app:popUpTo="@+id/nav_camera"
app:popUpToInclusive="true" />
</fragment>

<fragment
android:id="@+id/previewFragment"
android:name="com.ozcanalasalvar.camerax.fragments.PreviewFragment"
android:label="fragment_preview"
tools:layout="@layout/fragment_preview" />

</navigation>

0 comments on commit 7c4af5c

Please sign in to comment.