-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c61835a
commit 7c4af5c
Showing
7 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
app/src/main/java/com/ozcanalasalvar/camerax/fragments/PreviewFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/ozcanalasalvar/camerax/utils/MediaType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters