日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android旋转屏幕 简册,[Android][旋转屏幕]

發布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android旋转屏幕 简册,[Android][旋转屏幕] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.落筆緣由

最近在研究旋轉屏幕,網上可以找到資料,發現他們基本都是在Activity的基礎上進行旋轉。自己也想研究一下,能不能實現只旋轉屏幕的內容,而不旋轉屏幕上的菜單。例如,我點擊屏幕上的按鈕,頁面的內容旋轉,而按鈕不跟隨旋轉。

2.具體實踐

下面的代碼是每調用一次就旋轉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動畫來實現旋轉屏幕,但是發現有好多事情要處理。

* 例如旋轉屏幕后,你要重新計算body的大小;還要注意當打開設置里的自動選擇按鈕后,當界面隨平板旋轉的時候,我們要重新設置保存在

* 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中設置android

* :configChanges屬性來實現這點。

* 你可以在這里聲明activity可以處理的任何配置改變,當這些配置改變時不會重新啟動activity,而會調用activity的

* onConfigurationChanged

* (Resources.Configuration)方法。如果改變的配置中包含了你所無法處理的配置(在android

* :configChanges并未聲明),

* 你的activity仍然要被重新啟動,而onConfigurationChanged(Resources.

* Configuration)將不會被調用。

*

* 其次:android:configChanges=""中可以用的值:keyboard|mcc|mnc|locale|touchscreen|

* keyboardHidden|navigation|orientation…… Configuration

* 類中包含了很多種信息,例如系統字體大小,orientation,輸入設備類型等等.(如上圖)

* 比如:android:configChanges="orientation|keyboard|keyboardHidden"

*

*

* 1.需要在AndroidMenifast對應的Activity配置android:configChanges=

* "orientation|screenSize" 2.需要在AndroidMenifast配置權限

* 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);

}

}

/**

* 獲取當前屏幕旋轉角度

*

* @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.總結

經過粗糙的研究,發現沒那么簡單實現,還是等到有時間或者有其他思路再研究。

4.參考文章

5.源碼地址

總結

以上是生活随笔為你收集整理的android旋转屏幕 简册,[Android][旋转屏幕]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。