From 30b62f2ae3c52fb3e223d5429eb55a7eedbc4426 Mon Sep 17 00:00:00 2001
From: afnanramadhan <13521011@std.stei.itb.ac.id>
Date: Fri, 5 Apr 2024 03:41:11 +0700
Subject: [PATCH] feat: add twibbon landscape layout

---
 .../example/android_hit/FragmentTwibbon.kt    | 19 ++++--
 .../main/res/layout-land/fragment_twibbon.xml | 65 +++++++++++++++++++
 app/src/main/res/layout/fragment_twibbon.xml  |  9 +--
 3 files changed, 85 insertions(+), 8 deletions(-)
 create mode 100644 app/src/main/res/layout-land/fragment_twibbon.xml

diff --git a/app/src/main/java/com/example/android_hit/FragmentTwibbon.kt b/app/src/main/java/com/example/android_hit/FragmentTwibbon.kt
index 57272af..e9ef312 100644
--- a/app/src/main/java/com/example/android_hit/FragmentTwibbon.kt
+++ b/app/src/main/java/com/example/android_hit/FragmentTwibbon.kt
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
 import android.app.Activity
 import android.content.Intent
 import android.content.IntentFilter
+import android.content.res.Configuration
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.graphics.ImageFormat
@@ -95,9 +96,14 @@ class FragmentTwibbon : Fragment() {
                 val buffer = image.planes[0].buffer
                 val bytes = ByteArray(buffer.remaining())
                 buffer.get(bytes)
-                val bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.size)
-                val rotatebitmap = rotateBitmap(bitmap)
-                displayCapturedImage(rotatebitmap)
+                var bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.size)
+                val orientation = resources.configuration.orientation
+                if(orientation == Configuration.ORIENTATION_PORTRAIT){
+                    bitmap = rotateBitmap90(bitmap)
+                }else{
+                    bitmap = rotateBitmap180(bitmap)
+                }
+                displayCapturedImage(bitmap)
                 image.close()
                 cameraProvider.unbindAll()
             }
@@ -131,11 +137,16 @@ class FragmentTwibbon : Fragment() {
         binding.imageView.visibility = View.VISIBLE
     }
 
-    fun rotateBitmap(bitmap: Bitmap): Bitmap {
+    fun rotateBitmap90(bitmap: Bitmap): Bitmap {
         val matrix = Matrix()
         matrix.postRotate(90f)
         return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
     }
+    fun rotateBitmap180(bitmap: Bitmap): Bitmap {
+        val matrix = Matrix()
+        matrix.postRotate(180f)
+        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
+    }
     override fun onDestroyView() {
         super.onDestroyView()
         _binding = null
diff --git a/app/src/main/res/layout-land/fragment_twibbon.xml b/app/src/main/res/layout-land/fragment_twibbon.xml
new file mode 100644
index 0000000..723cced
--- /dev/null
+++ b/app/src/main/res/layout-land/fragment_twibbon.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout 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"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".FragmentTwibbon">
+
+
+    <FrameLayout
+        android:id="@+id/frameLayout"
+        android:layout_width="300dp"
+        android:layout_height="300dp"
+        android:layout_marginEnd="24dp"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toTopOf="parent">
+
+        <androidx.camera.view.PreviewView
+            android:id="@+id/previewView"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+
+        <ImageView
+            android:id="@+id/imageView"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:scaleType="centerCrop"
+            android:visibility="gone" />
+
+        <ImageView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:src="@drawable/twibbon_frame" />
+    </FrameLayout>
+
+    <ImageButton
+        android:id="@+id/capture"
+        android:layout_width="60sp"
+        android:layout_height="60sp"
+        android:layout_alignParentBottom="true"
+        android:layout_centerHorizontal="true"
+        android:background="?attr/selectableItemBackgroundBorderless"
+        android:scaleType="centerCrop"
+        android:src="@drawable/rounded_button"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toStartOf="@+id/frameLayout"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+
+    <ImageButton
+        android:id="@+id/retakeButton"
+        android:layout_width="60sp"
+        android:layout_height="60sp"
+        android:layout_alignParentBottom="true"
+        android:background="?attr/selectableItemBackgroundBorderless"
+        android:src="@drawable/baseline_refresh_24"
+        android:visibility="gone"
+        app:layout_constraintEnd_toStartOf="@+id/frameLayout"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/capture"/>
+
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_twibbon.xml b/app/src/main/res/layout/fragment_twibbon.xml
index 2dd0881..df0b146 100644
--- a/app/src/main/res/layout/fragment_twibbon.xml
+++ b/app/src/main/res/layout/fragment_twibbon.xml
@@ -9,11 +9,12 @@
 
     <FrameLayout
         android:id="@+id/frameLayout"
-        android:layout_width="match_parent"
-        android:layout_height="500dp"
+        android:layout_width="400dp"
+        android:layout_height="400dp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent">
+        app:layout_constraintTop_toTopOf="parent"
+        android:layout_marginTop="30dp">
 
         <androidx.camera.view.PreviewView
             android:id="@+id/previewView"
@@ -30,7 +31,7 @@
         <ImageView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            android:src="@drawable/twibbon" />
+            android:src="@drawable/twibbon_frame" />
     </FrameLayout>
 
     <ImageButton
-- 
GitLab