android旋转屏幕 简册,[Android][旋转屏幕]
1.落筆緣由
最近在研究旋轉(zhuǎn)屏幕,網(wǎng)上可以找到資料,發(fā)現(xiàn)他們基本都是在Activity的基礎(chǔ)上進行旋轉(zhuǎn)。自己也想研究一下,能不能實現(xiàn)只旋轉(zhuǎn)屏幕的內(nèi)容,而不旋轉(zhuǎn)屏幕上的菜單。例如,我點擊屏幕上的按鈕,頁面的內(nèi)容旋轉(zhuǎn),而按鈕不跟隨旋轉(zhuǎn)。
2.具體實踐
下面的代碼是每調(diào)用一次就旋轉(zhuǎn)90度,它是在Ac。
public class Test extends Activity implements OnClickListener {
private LinearLayout body = null;
private LinearLayout.LayoutParams params = null;
private LinearLayout innerBody = null;
private TextView tv = null;
private Button btn1 = null;
private SharedPreferences preferences = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
setContentView(body);
preferences = getSharedPreferences("test",Context.MODE_PRIVATE);
Editor editor=preferences.edit();
editor.putFloat("degree", 0);
editor.commit();
}
private void initView() {
try {
body = new LinearLayout(this);
if (body != null) {
body.setOrientation(LinearLayout.VERTICAL);
params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
if (params != null) {
body.setLayoutParams(params);
}
innerBody = new LinearLayout(this);
if (innerBody != null) {
innerBody.setOrientation(LinearLayout.VERTICAL);
params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
if (params != null) {
innerBody.setLayoutParams(params);
}
body.addView(innerBody);
tv = new TextView(this);
if (tv != null) {
tv.setTextSize(40);
tv.setText("測試一下");
innerBody.addView(tv);
}
btn1 = new Button(this);
if (btn1 != null) {
btn1.setTextSize(40);
btn1.setText("Button1");
innerBody.addView(btn1);
btn1.setOnClickListener(this);
}
}
}
} catch (Exception e) {
}
}
/**
* 本來想通過Property Animation動畫來實現(xiàn)旋轉(zhuǎn)屏幕,但是發(fā)現(xiàn)有好多事情要處理。
* 例如旋轉(zhuǎn)屏幕后,你要重新計算body的大小;還要注意當打開設(shè)置里的自動選擇按鈕后,當界面隨平板旋轉(zhuǎn)的時候,我們要重新設(shè)置保存在
* SharedPreferences里的值
* @param view
*/
private void animRoateScreen(View view)
{
float startDegree = 0;
if (preferences.getFloat("degree", 0)==0) {
startDegree = 90f;
}
else if (preferences.getFloat("degree", 0)==90f) {
startDegree = 180f;
}
else if (preferences.getFloat("degree", 0)==180f) {
startDegree = 270f;
}
else if (preferences.getFloat("degree", 0)==270f) {
startDegree = 0f;
}
Editor editor=preferences.edit();
editor.putFloat("degree", startDegree);
editor.commit();
ObjectAnimator animatorx = ObjectAnimator
.ofFloat(view, "rotation", startDegree,startDegree+90f);
animatorx.start();
}
/*
* 在一些特殊的情況中,你可能希望當一種或者多種配置改變時避免重新啟動你的activity。你可以通過在manifest中設(shè)置android
* :configChanges屬性來實現(xiàn)這點。
* 你可以在這里聲明activity可以處理的任何配置改變,當這些配置改變時不會重新啟動activity,而會調(diào)用activity的
* onConfigurationChanged
* (Resources.Configuration)方法。如果改變的配置中包含了你所無法處理的配置(在android
* :configChanges并未聲明),
* 你的activity仍然要被重新啟動,而onConfigurationChanged(Resources.
* Configuration)將不會被調(diào)用。
*
* 其次:android:configChanges=""中可以用的值:keyboard|mcc|mnc|locale|touchscreen|
* keyboardHidden|navigation|orientation…… Configuration
* 類中包含了很多種信息,例如系統(tǒng)字體大小,orientation,輸入設(shè)備類型等等.(如上圖)
* 比如:android:configChanges="orientation|keyboard|keyboardHidden"
*
*
* 1.需要在AndroidMenifast對應(yīng)的Activity配置android:configChanges=
* "orientation|screenSize" 2.需要在AndroidMenifast配置權(quán)限
* android:name="android.permission.CHANGE_CONFIGURATION" >
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("lgy", "onConfigurationChanged========");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else {
}
}
private void rotationScreen(Context mContext) {
Activity activity = null;
if (mContext instanceof Activity) {
activity = (Activity) mContext;
}
if (0 == getDisplayRotation(activity)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (90 == getDisplayRotation(activity)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else if (180 == getDisplayRotation(activity)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else if (270 == getDisplayRotation(activity)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
/**
* 獲取當前屏幕旋轉(zhuǎn)角度
*
* @param activity
* @return 0表示是豎屏; 90表示是左橫屏; 180表示是反向豎屏; 270表示是右橫屏
*/
private int getDisplayRotation(Activity activity) {
if (activity == null)
return 0;
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}
@Override
public void onClick(View v) {
if (v == btn1) {
// rotationScreen(this);
animRoateScreen(body);
}
}
}
3.總結(jié)
經(jīng)過粗糙的研究,發(fā)現(xiàn)沒那么簡單實現(xiàn),還是等到有時間或者有其他思路再研究。
4.參考文章
5.源碼地址
總結(jié)
以上是生活随笔為你收集整理的android旋转屏幕 简册,[Android][旋转屏幕]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python:进阶操作(1)
- 下一篇: android 代码设置textview