From 02f4cc49a56447bbc7bcc0e37e215464a52e4393 Mon Sep 17 00:00:00 2001 From: kklyoon Date: Wed, 15 Jan 2020 23:21:45 +0900 Subject: [PATCH] =?UTF-8?q?image=20rotation=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sabujak/pickle/data/PickleDataSource.kt | 3 +- .../life/sabujak/pickle/data/entity/Image.kt | 3 +- .../sabujak/pickle/ui/insta/CropLayout.kt | 9 ++-- .../sabujak/pickle/ui/insta/InstaFragment.kt | 50 +++++++++++-------- .../sabujak/pickle/ui/insta/InstaViewModel.kt | 5 ++ .../sabujak/pickle/util/PhotoVideoCursor.kt | 3 +- pickle/src/main/res/layout/dialog_result.xml | 2 +- 7 files changed, 45 insertions(+), 30 deletions(-) diff --git a/pickle/src/main/java/life/sabujak/pickle/data/PickleDataSource.kt b/pickle/src/main/java/life/sabujak/pickle/data/PickleDataSource.kt index 99805e1..383602c 100644 --- a/pickle/src/main/java/life/sabujak/pickle/data/PickleDataSource.kt +++ b/pickle/src/main/java/life/sabujak/pickle/data/PickleDataSource.kt @@ -83,13 +83,14 @@ class PickleDataSource(val context: Context) : PositionalDataSource val dateModified = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)) val fileSize = cursor.getInt(cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE)) val mimeType = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)) + val orientation = cursor.getFloat(cursor.getColumnIndex(MediaStore.MediaColumns.ORIENTATION)) logger.d("id = $id bucketId = $bucketId contentUri = $contentUri data = $data mediaType = $mediaType isVideo = $isVideo dateModified = $dateModified fileSize = $fileSize mimeType = $mimeType") return if(isVideo){ val duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION)) Video(id,contentUri, data, dateModified, fileSize,duration) }else{ - Image(id, contentUri, data, dateModified, fileSize) + Image(id, contentUri, data, dateModified, fileSize, orientation) } } diff --git a/pickle/src/main/java/life/sabujak/pickle/data/entity/Image.kt b/pickle/src/main/java/life/sabujak/pickle/data/entity/Image.kt index b29b0a5..dd0adce 100644 --- a/pickle/src/main/java/life/sabujak/pickle/data/entity/Image.kt +++ b/pickle/src/main/java/life/sabujak/pickle/data/entity/Image.kt @@ -7,7 +7,8 @@ data class Image( private val uri: Uri?, private val data: String?, val dateModified: Long, - val fileSize: Int) :PickleMedia { + val fileSize: Int, + val orientation: Float) :PickleMedia { override fun getUri() =uri override fun getData()=data diff --git a/pickle/src/main/java/life/sabujak/pickle/ui/insta/CropLayout.kt b/pickle/src/main/java/life/sabujak/pickle/ui/insta/CropLayout.kt index f38f692..2605b90 100644 --- a/pickle/src/main/java/life/sabujak/pickle/ui/insta/CropLayout.kt +++ b/pickle/src/main/java/life/sabujak/pickle/ui/insta/CropLayout.kt @@ -119,7 +119,7 @@ class CropLayout @JvmOverloads constructor( } fun removeOnCropListener(listener: OnCropListener) { - listeners.addIfAbsent(listener) + listeners.remove(listener) } /** @@ -178,7 +178,7 @@ class CropLayout @JvmOverloads constructor( } } - fun setCropScale(uri: Uri) { + fun setCropScale(uri: Uri, orientation: Float) { cropOverlay.visibility = View.VISIBLE cropImageView.top = top cropImageView.left = left @@ -188,6 +188,7 @@ class CropLayout @JvmOverloads constructor( cropImageView.adjustViewBounds = true cropImageView.setImageURI(uri) cropImageView.layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT, Gravity.CENTER) + cropImageView.rotation = orientation cropImageView.requestLayout() animator = GestureAnimator.of(cropImageView, frame, scale) animation = GestureAnimation(cropOverlay, animator) @@ -196,7 +197,7 @@ class CropLayout @JvmOverloads constructor( logger.d("setCropScale() : cropImageView" + "(" + position[0] + ", " + position[1] + ") " + cropImageView.width + ", " + cropImageView.height) } - fun setAspectRatio(uri: Uri) { + fun setAspectRatio(uri: Uri, orientation: Float) { if (::animation.isInitialized) animation.stop() cropOverlay.visibility = View.GONE cropImageView.layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.CENTER) @@ -212,6 +213,7 @@ class CropLayout @JvmOverloads constructor( cropImageView.maxHeight = height cropImageView.scaleX = 1f cropImageView.scaleY = 1f + cropImageView.rotation = orientation cropImageView.requestLayout() } @@ -220,7 +222,6 @@ class CropLayout @JvmOverloads constructor( return false } - companion object { private const val DEFAULT_MAX_SCALE = 2f diff --git a/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaFragment.kt b/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaFragment.kt index e08a0cd..4226845 100644 --- a/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaFragment.kt +++ b/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaFragment.kt @@ -2,7 +2,6 @@ package life.sabujak.pickle.ui.insta import android.content.Context import android.graphics.Bitmap -import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -18,6 +17,7 @@ import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.GridLayoutManager import com.google.android.material.appbar.AppBarLayout import life.sabujak.pickle.R +import life.sabujak.pickle.data.entity.Image import life.sabujak.pickle.data.entity.PickleMedia import life.sabujak.pickle.databinding.FragmentInstaBinding import life.sabujak.pickle.ui.common.OptionMenuViewModel @@ -36,8 +36,6 @@ class InstaFragment : Fragment(), OnInstaEventListener { GridLayoutManager(context, 3) } - var selectedUri: Uri? = null - override fun onAttach(context: Context) { super.onAttach(context) activity?.let { @@ -78,9 +76,12 @@ class InstaFragment : Fragment(), OnInstaEventListener { (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar) ivPreview.addOnCropListener(object : OnCropListener { override fun onSuccess(bitmap: Bitmap) { - val view = layoutInflater.inflate(R.layout.dialog_result, null) - view.findViewById(R.id.iv_image).setImageBitmap(bitmap) - AlertDialog.Builder(ivPreview.context).setView(view).show() + val dialogLayout = layoutInflater.inflate(R.layout.dialog_result, null) + val dialogImageView = dialogLayout.findViewById(R.id.iv_image) + dialogImageView.setImageBitmap(bitmap) + dialogImageView.rotation = (instaViewModel.selectedPickleMedia as Image).orientation + dialogImageView.scaleType = ImageView.ScaleType.FIT_CENTER + AlertDialog.Builder(ivPreview.context).setView(dialogLayout).show() } override fun onFailure(e: Exception) { @@ -99,7 +100,7 @@ class InstaFragment : Fragment(), OnInstaEventListener { instaAdapter.submitList(pagedList) }) instaViewModel.isAspectRatio.observe(viewLifecycleOwner, Observer { - if (!binding.ivPreview.isEmpty()) loadUri(selectedUri) + if (!binding.ivPreview.isEmpty()) loadPickleMedia(instaViewModel.selectedPickleMedia) }) instaViewModel.initialLoadState.observe(viewLifecycleOwner, Observer { logger.d("initialLoadState = $it") @@ -113,30 +114,35 @@ class InstaFragment : Fragment(), OnInstaEventListener { if (!binding.ivPreview.isOffFrame() && instaViewModel.isAspectRatio.value == false) { binding.ivPreview.crop() } else if (instaViewModel.isAspectRatio.value == true) { - val dialogContentView = layoutInflater.inflate(R.layout.dialog_result, null) - dialogContentView.findViewById(R.id.iv_image) - .setImageURI(selectedUri) - AlertDialog.Builder(it).setView(dialogContentView).show() + val dialogLayout = layoutInflater.inflate(R.layout.dialog_result, null) + val dialogImageView = dialogLayout.findViewById(R.id.iv_image) + dialogImageView.setImageURI(instaViewModel.selectedPickleMedia.getUri()) + dialogImageView.rotation = (instaViewModel.selectedPickleMedia as Image).orientation + dialogImageView.scaleType = ImageView.ScaleType.FIT_CENTER + AlertDialog.Builder(it).setView(dialogLayout).show() } } }) } - private fun loadUri(uri: Uri?){ - uri?.let{ - if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio(it) - else binding.ivPreview.setCropScale(it) + private fun loadPickleMedia(pickleMedia: PickleMedia) { + if (pickleMedia.getType() == PickleMedia.Type.PHOTO) { + val orientation = (pickleMedia as Image).orientation + pickleMedia.getUri()?.let { + if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio( + it, + orientation + ) + else binding.ivPreview.setCropScale(it, orientation) + } } + // TODO : VIDEO 에 대한 처리 } override fun onItemClick(view: View?, pickleMedia: PickleMedia) { - pickleMedia.getUri()?.let{ - selectedUri = it - if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio(it) - else binding.ivPreview.setCropScale(it) - } - - view?.let{ + instaViewModel.setSelected(pickleMedia) + loadPickleMedia(pickleMedia) + view?.let { binding.recyclerView.smoothScrollBy(0, it.top) binding.previewAppbarLayout.setExpanded(true) } diff --git a/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaViewModel.kt b/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaViewModel.kt index 5b9b358..2864888 100644 --- a/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaViewModel.kt +++ b/pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaViewModel.kt @@ -12,6 +12,7 @@ class InstaViewModel(application: Application) : AndroidViewModel(application) { private val _isAspectRatio = MutableLiveData().apply { postValue(false) } var isAspectRatio: LiveData = _isAspectRatio + lateinit var selectedPickleMedia: PickleMedia val selectionManager = InstaSelectionManager() @@ -43,4 +44,8 @@ class InstaViewModel(application: Application) : AndroidViewModel(application) { if (_isAspectRatio.value == true) _isAspectRatio.postValue(false) else _isAspectRatio.postValue(true) } + + fun setSelected(selected: PickleMedia){ + selectedPickleMedia = selected + } } diff --git a/pickle/src/main/java/life/sabujak/pickle/util/PhotoVideoCursor.kt b/pickle/src/main/java/life/sabujak/pickle/util/PhotoVideoCursor.kt index 1940ab2..ae7651f 100644 --- a/pickle/src/main/java/life/sabujak/pickle/util/PhotoVideoCursor.kt +++ b/pickle/src/main/java/life/sabujak/pickle/util/PhotoVideoCursor.kt @@ -18,7 +18,8 @@ class PhotoVideoCursor(context:Context):PickleCursor(context){ MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.MIME_TYPE, - MediaStore.Video.VideoColumns.DURATION + MediaStore.Video.VideoColumns.DURATION, + MediaStore.MediaColumns.ORIENTATION ) val selection = "" + diff --git a/pickle/src/main/res/layout/dialog_result.xml b/pickle/src/main/res/layout/dialog_result.xml index eb2f4d3..936021c 100644 --- a/pickle/src/main/res/layout/dialog_result.xml +++ b/pickle/src/main/res/layout/dialog_result.xml @@ -32,7 +32,7 @@ app:layout_constraintDimensionRatio="h,1:1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/title" + app:layout_constraintTop_toBottomOf="@id/tv_title" /> \ No newline at end of file