大家都知道,很多Android手機帶有重力感應感應器,能夠對手機的翻轉做出響應。比如應用在螢幕的自動翻轉、重力感應遊戲等方面。
只要在androidmanifest.xml中對應的Activity中加入sensor屬性即可實現螢幕自動翻轉,如:
Xml代碼
<
activity android:name=".demo"
android:label="@string/app_name"
android:screenOrientation="sensor"
>
<
activity android:name=".demo"
android:label="@string/app_name"
android:screenOrientation="sensor"
>
但是螢幕自動翻轉也伴隨著一個問題:當表單切換或者布局切換時,Activity中OnCreate方法會被重複調用。一般OnCreate中會初始化一些資料,重複調用可能會產生意想不到的後果。解決方案如下:
在androidmanifest.xml中的activit元素加入configChanges這個屬性,比如
Xml代碼
<
activity android:name="demo"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
>
<
activity android:name="demo"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
>
另外,在Activity的Java檔案中重載onConfigurationChanged(Configuration newConfig)這個方法,這樣就不會在布局切換或視窗切換時重載onCreate等方法。代碼如下:
Java代碼
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//TO-DO
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//TO-DO
}
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//TO-DO
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//TO-DO
}
}
還有介面設計方面的問題,Android手機大部分是HVGA、WVGA的解析度,螢幕視覺上比較“狹長”。往往豎著看很合適的布局,當螢幕橫向翻轉以後顯示會變得很彆扭。當螢幕由豎直方向改變為橫向時,我們可以把介面中的控制項由本來的垂直線性布局修改為橫向線性布局,這樣布局會更合理一些。我們可以自己寫一個布局類整合LinearLayout布局,通過覆蓋onMeasure方法來實現這種自動布局。當螢幕的寬高發生改變時,系統會調用 onMeasure方法。通過這個方法,我們可以獲得改變以後的寬高尺寸,從而來實現螢幕翻轉的自動布局,主要代碼如下:
Java代碼
/**
* 螢幕改變時自動調用
* @param widthMeasureSpec 改變後的寬度
* @param heightMeasureSpec 改變後的高度
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
/*寬度*/
int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
/*高度*/
int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);
/*豎直布局*/
if (screenWith < screenHeight)
{
this.setOrientation(VERTICAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
/*該控制項占布局的2/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 2/ 5
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
/*該控制項占布局的3/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 3/ 5
updateViewLayout(childView, params);
}
}
}
/*橫向布局*/
else
{
this.setOrientation(HORIZONTAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 2/ 5
screenHeight);
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 3/ 5
screenHeight);
updateViewLayout(childView, params);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 螢幕改變時自動調用
* @param widthMeasureSpec 改變後的寬度
* @param heightMeasureSpec 改變後的高度
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
/*寬度*/
int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
/*高度*/
int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);
/*豎直布局*/
if (screenWith < screenHeight)
{
this.setOrientation(VERTICAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
/*該控制項占布局的2/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 2/ 5
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
/*該控制項占布局的3/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 3/ 5
updateViewLayout(childView, params);
}
}
}
/*橫向布局*/
else
{
this.setOrientation(HORIZONTAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 2/ 5
screenHeight);
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 3/ 5
screenHeight);
updateViewLayout(childView, params);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}