Getting Current Device Orientation for CameraX UI Rotation

79 views
Skip to first unread message

Ali safwat - علي صفوت

unread,
Jul 21, 2025, 5:13:18 AMJul 21
to Android CameraX Discussion Group
When using CameraX, rotating my device recreates the activity, risking memory leaks. I want to disable this behavior and lock the screen's orientation. How can I then listen for the physical device rotation so I can animate UI elements like buttons to match the new orientation?

Leo Huang

unread,
Jul 21, 2025, 6:28:46 AMJul 21
to Android CameraX Discussion Group, alisa...@gmail.com
Hi, 

You can listener to OrientationEventListener.

Sample code

```
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.OrientationEventListener
import android.view.Surface

class YourActivity : AppCompatActivity() {

    private lateinit var orientationEventListener: OrientationEventListener
    private var lastKnownRotation = 0 // To keep track of the last known rotation

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_your)

        orientationEventListener = object : OrientationEventListener(this) {
            override fun onOrientationChanged(orientation: Int) {
                if (orientation == ORIENTATION_UNKNOWN) {
                    return // Device is flat, do nothing
                }

                // Determine the rotation based on the orientation degrees
                val rotation = when (orientation) {
                    in 45..134 -> Surface.ROTATION_270 // Reverse Landscape
                    in 135..224 -> Surface.ROTATION_180 // Reverse Portrait
                    in 225..314 -> Surface.ROTATION_90  // Landscape
                    else -> Surface.ROTATION_0 // Portrait
                }

                // Animate only if the rotation has changed
                if (rotation != lastKnownRotation) {
                    lastKnownRotation = rotation
                    animateUiElements(rotation)
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (orientationEventListener.canDetectOrientation()) {
            orientationEventListener.enable()
        }
    }

    override fun onPause() {
        super.onPause()
        orientationEventListener.disable()
    }
}

alisa...@gmail.com 在 2025年7月21日 星期一下午5:13:18 [UTC+8] 的信中寫道:

Scott Nien

unread,
Jul 21, 2025, 10:34:47 AMJul 21
to Leo Huang, Android CameraX Discussion Group, alisa...@gmail.com
FYI, If you run into memory leak issues where the activity instance is held within CameraX codes and you are using CameraX 1.5.0,  we will soon have a beta03 release that fixes the issue. 

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/57ffbe6b-2d60-4cad-970b-fc31f0413589n%40android.com.

Ali safwat - علي صفوت

unread,
Jul 21, 2025, 12:15:50 PMJul 21
to Android CameraX Discussion Group, scot...@google.com, Android CameraX Discussion Group, Ali safwat - علي صفوت‎, leoh...@google.com
Thanks for all reply I am really appreciate it thank you 

I need to ask one more question 
What is optimal or the best practice here 
Rotate all activity or just listen on orientation event Listener and get rotation angle to rotate my ui with graphic layer 

Charcoal Chen

unread,
Jul 21, 2025, 9:29:34 PMJul 21
to Ali safwat - علي صفوت, Android CameraX Discussion Group, scot...@google.com, leoh...@google.com
Hi,

Please also refer to ImageCapture/ImageAnalysis target rotation guidelines. You can check the scenarios matching your app's requirements to handle the rotation related settings.

Ali safwat - علي صفوت

unread,
Jul 23, 2025, 4:44:16 AMJul 23
to Android CameraX Discussion Group, charco...@google.com, Android CameraX Discussion Group, scot...@google.com, leoh...@google.com, Ali safwat - علي صفوت‎
when i pick image in landcapse mode and save it in my gallery it appears
as portait 

when i use my device camera the image in landscape appear as accully landscape WhatsApp Image 2025-07-23 at 11.41.48 AM (1).jpeg 
this form my device camera and saved in gallery 


WhatsApp Image 2025-07-23 at 11.41.48 AM.jpeg
this form camera x and also saved in gallery  

Reply all
Reply to author
Forward
0 new messages