Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Switch Height and Width when View change orientation

I do use PhotoView library to ZoomIn-Out. now when user click a button i prepared to rotate the PhotoView, Height and Width would rotate. so width which is smaller than the screen Height. and this result in i can't get the full screen zoom like usual before Rotating the Imageview.

so any solution to make the new width or height after rotation to take full screen.

Example : this is Zoomed Image it doesn't zoom in the entire screen like before the rotation

this is Zoomed Image

like image 421
MiniDev99 Avatar asked Dec 03 '21 23:12

MiniDev99


People also ask

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I manage screen orientation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What is adapting to screen orientation in Android?

As with almost all smartphones, Android supports two screen orientations: portrait and landscape. When the screen orientation of an Android device is changed, the current activity being displayed is destroyed and re-created automatically to redraw its content in the new orientation.


1 Answers

I had to work with the same type of feature on my project and here was my design and implementation for it.

Basic XML design for photo view

<?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=".MainActivity">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/image_main"
        android:layout_width="0dp"
        android:scaleType="centerCrop"
        android:layout_height="400dp"
        android:src="@drawable/gull_portrait_ca_usa"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <ImageView
        android:id="@+id/image_rotate"
        android:src="@android:drawable/ic_menu_rotate"
        android:layout_margin="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="40dp"
        android:layout_height="40dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Then I just update the orientation onClick event.

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.imageRotate.setOnClickListener {
            binding.imageMain.rotation = binding.imageMain.rotation+90
        }

    }
}

Output

like image 148
aslamconsole Avatar answered Oct 27 '22 06:10

aslamconsole