螢幕自轉

      在〈螢幕自轉〉中尚無留言

todo

AndroidManifest.xml

todo

<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

todo

activity_main_landscape.xml

todo

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/menu_landscape"
android:orientation="vertical"
android:layout_weight="3"
android:background="#a0a0ff"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:background="#a0a0a0"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#ffff00"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>

todo

activity_main_portrait.xml

todo

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/menu_portrait"
android:orientation="vertical"
android:layout_weight="3"
android:background="#f0f000"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:background="#a0a0a0"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#ffff00"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>

todo

MainActivity.kt

todo

package com.asuscomm.mahaljsp.gallery
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.asuscomm.mahaljsp.gallery.databinding.ActivityMainLandscapeBinding
import com.asuscomm.mahaljsp.gallery.databinding.ActivityMainPortraitBinding

class MainActivity : AppCompatActivity() {
val mainCcontext:Context=this
object ScreenOrientationHelper {
val ORIENTATION_TYPE_0 = 0
val ORIENTATION_TYPE_90 = 90
val ORIENTATION_TYPE_180 = 180
val ORIENTATION_TYPE_270 = 270
private var mOrientationEventListener: OrientationEventListener? = null
private var mScreenOrientationChangeListener:
ScreenOrientationChangeListener? = null
private var currentType = ORIENTATION_TYPE_0

fun init(context: Context, listener: ScreenOrientationChangeListener) {
mScreenOrientationChangeListener = listener
mOrientationEventListener = object :
OrientationEventListener(context) {
override fun onOrientationChanged(orientation: Int) {
if (mScreenOrientationChangeListener == null) {
return
}
if (orientation > 340 || orientation < 20) {
//0
if (currentType == 0) {
return
}
if (getScreenRotation(context) == Surface.ROTATION_0) {
mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_0)
currentType = ORIENTATION_TYPE_0
}
} else if (orientation in 71..109) {
//90
if (currentType == 90) {
return
}
val angle = getScreenRotation(context)
if (angle == Surface.ROTATION_270) {
mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_90)
currentType = ORIENTATION_TYPE_90
}
} else if (orientation in 161..199) {
//180
if (currentType == 180) {
return
}
val angle = getScreenRotation(context)
if (angle == Surface.ROTATION_180) {
mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_180)
currentType = ORIENTATION_TYPE_180
}
} else if (orientation in 251..289) {
//270
if (currentType == 270) {
return
}
val angle = getScreenRotation(context)
if (angle == Surface.ROTATION_90) {
mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_270)
currentType = ORIENTATION_TYPE_270
}
}
}
}
register()
}

private fun getScreenRotation(context: Context): Int {
val windowManager =
context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
return windowManager.defaultDisplay?.rotation ?: 0
}

fun register() {
if (mOrientationEventListener != null) {
mOrientationEventListener!!.enable()
}
}

fun unRegister() {
if (mOrientationEventListener != null) {
mOrientationEventListener!!.disable()
}
}

interface ScreenOrientationChangeListener {
/**
*
* @param orientation
*/
fun onChange(orientation: Int)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(ActivityMainPortraitBinding.inflate(layoutInflater).root)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
var controller: WindowInsetsController? = getWindow().insetsController
controller?.hide(WindowInsets.Type.statusBars())
}
else{
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}

ScreenOrientationHelper.init(this, object :
ScreenOrientationHelper.ScreenOrientationChangeListener {
override fun onChange(orientation: Int) {
when(orientation) {
ScreenOrientationHelper.ORIENTATION_TYPE_0 -> {
val ui= ActivityMainPortraitBinding.inflate(layoutInflater)
setContentView(ui.root)
val ly=ui.menuPortrait
var tv:TextView
for (i in 1..10){
tv= TextView(mainCcontext)
tv.text="${i}"
ly.addView(tv)
}
}
ScreenOrientationHelper.ORIENTATION_TYPE_90 -> {
val ui= ActivityMainLandscapeBinding.inflate(layoutInflater)
setContentView(ui.root)
val ly=ui.menuLandscape
var tv:TextView
for (i in 1..10){
tv= TextView(mainCcontext)
tv.text="${i}"
ly.addView(tv)
}
}
ScreenOrientationHelper.ORIENTATION_TYPE_180 -> {
setContentView(ActivityMainPortraitBinding.inflate(layoutInflater).root)
}
ScreenOrientationHelper.ORIENTATION_TYPE_270 -> {
setContentView(ActivityMainLandscapeBinding.inflate(layoutInflater).root)
}
}
}
})
}
}

todo

 AndroidManifest.xml 檔,在 <activity> </activity> 中,添加一條屬性資訊

android:configChanges="orientation|screenSize"

todo

底下在 if、else 中,使用了 setContentView(R.layout.xxxx) 函數,那麼就可以實現:每次螢幕旋轉時,調用不同的佈局。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 什麼都不用寫
    }
    else {
        // 什麼都不用寫
    }
}

 

todo

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *