如何在ä¸é‡æ–°åˆ›å»ºå¸ƒå±€çš„情况下旋转方å‘更改视图?

时间:2016-01-22 21:00:52

标签: android android-layout rotation android-button android-orientation

here之å‰å·²ç»é—®è¿‡è¿™ä¸ªé—®é¢˜ï¼Œä½†ç­”案是错误的。我想在å±å¹•æ–¹å‘更改上旋转一些视图,但我想ä¿æŒå¸ƒå±€ä¸å˜ã€‚它们应根æ®å½“å‰æ–¹å‘(SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAIT)旋转90度,180度,270度或360度。

这就是我想è¦å®žçŽ°çš„目标:

LayoutExample

我æ到的链接中的答案表明我应该在layout-land中创建一个新的ä¸åŒå¸ƒå±€ã€‚显然,这ä¸æ˜¯æˆ‘想è¦çš„。我ä¸æƒ³é‡æ–°åˆ›å»ºæ´»åŠ¨æˆ–更改布局方å‘。我åªæƒ³æ—‹è½¬ä¸€äº›è§†å›¾ï¼Œå¹¶åœ¨æ–¹å‘更改时ä¿æŒå…¶ä»–视图ä¸å˜ã€‚

旋转特定视图与更改或é‡æ–°åˆ›å»ºæ•´ä¸ªå¸ƒå±€ä¹‹é—´å­˜åœ¨å·¨å¤§å·®å¼‚(两者都å–决于方å‘更改)。

使用此link中的答案,我将能够使用此方法获å–当å‰çš„å±å¹•æ–¹å‘:

public static int getScreenOrientation(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int rotation = windowManager.getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0
            || rotation == Surface.ROTATION_180) && height > width ||
            (rotation == Surface.ROTATION_90
                    || rotation == Surface.ROTATION_270) && width > height) {
        switch (rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e("ScreenOrientation", "Unknown screen orientation. Defaulting to " + "portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
        switch (rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e("ScreenOrientation", "Unknown screen orientation. Defaulting to " + "landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
        }
    }

    return orientation;
}

在改å˜æ–¹å‘时,我想åšä¸€äº›ç®€å•çš„事情:

RotateAnimation rotateAnimation = new RotateAnimation(0, getScreenOrientation(getContext()));
rotateAnimation.setDuration(2000);

for (int i = 0; i < 63; i++) {
    Button button = (Button) rootView.findViewById(i);
    button.startAnimation(rotateAnimation);
}

å¦ä¸€ç§é‡æ–°è§£é‡Šæˆ‘的问题的方法是“在没有改å˜å¸ƒå±€çš„情况下,有没有办法在onConfigurationChanged()方法中检测方å‘å˜åŒ–?â€ã€‚问题是如果我已ç»ç¦ç”¨å¸ƒå±€æ–¹å‘更改,它将无法检测到任何方å‘更改。

任何人都知é“它是如何完æˆçš„?我å¯èƒ½å®Œå…¨ç»åŽ†äº†é”™è¯¯çš„步骤,我想我将ä¸å¾—ä¸ä½¿ç”¨Accelerometer传感器或类似的东西æ¥å®žçŽ°æˆ‘想è¦çš„,所以请引导我完æˆã€‚

2 个答案:

答案 0 :(得分:14)

å°è¯•ä½¿ç”¨OrientationEventListener。您ä¸éœ€è¦ä½¿ç”¨onConfigurationChangedå’Œandroid:configChanges="orientation|keyboardHidden|screenSize"。

您需è¦åœ¨AndroidManifest.xml中为活动设置android:screenOrientation="portrait"。以下是OrientationEventListener的解决方案:

public class MyActivity extends Activity{

private ImageButton menuButton;

private Animation toLandAnim, toPortAnim;
private OrientationListener orientationListener;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_image_ruler);

    menuButton=(ImageButton)findViewById(R.id.menu_button);
    toLandAnim= AnimationUtils.loadAnimation(this, R.anim.menubutton_to_landscape);
    toPortAnim= AnimationUtils.loadAnimation(this, R.anim.menubutton_to_portrait);

    orientationListener = new OrientationListener(this);
}

@Override protected void onStart() {
    orientationListener.enable();
    super.onStart();
}

@Override protected void onStop() {
    orientationListener.disable();
    super.onStop();
}

private class OrientationListener extends OrientationEventListener{
    final int ROTATION_O    = 1;
    final int ROTATION_90   = 2;
    final int ROTATION_180  = 3;
    final int ROTATION_270  = 4;

    private int rotation = 0;
    public OrientationListener(Context context) { super(context); }

    @Override public void onOrientationChanged(int orientation) {
        if( (orientation < 35 || orientation > 325) && rotation!= ROTATION_O){ // PORTRAIT
            rotation = ROTATION_O;
            menuButton.startAnimation(toPortAnim);
        }
        else if( orientation > 145 && orientation < 215 && rotation!=ROTATION_180){ // REVERSE PORTRAIT
            rotation = ROTATION_180;
            menuButton.startAnimation(toPortAnim);
        }
        else if(orientation > 55 && orientation < 125 && rotation!=ROTATION_270){ // REVERSE LANDSCAPE
            rotation = ROTATION_270;
            menuButton.startAnimation(toLandAnim);
        }
        else if(orientation > 235 && orientation < 305 && rotation!=ROTATION_90){ //LANDSCAPE
            rotation = ROTATION_90;
            menuButton.startAnimation(toLandAnim);
        }
    }
}
}

当orientation约为45,135 ......等时,这也å¯ä»¥é˜²æ­¢è¿‡äºŽé¢‘ç¹çš„旋转。 希望它有所帮助。

答案 1 :(得分:1)

基础知识实际上è¦å®¹æ˜“得多。看看Handling Runtime Changes。

首先,通过设置

android:configChanges="orientation|keyboardHidden|screenSize"

在您的活动标签的清å•ä¸­ï¼Œæ‚¨å¯ä»¥è‡ªè¡Œå¤„ç†æ–¹å‘更改。 (orientation应该足够了,但有时会出现事件ä¸ä¼šå•ç‹¬è§¦å‘的问题。)

然åŽï¼Œæ‚¨è·³è¿‡onCreate,然åŽè°ƒç”¨onConfigurationChanged。覆盖此方法并在此处应用布局更改。您是在这里更改linearLayoutsæ–¹å‘还是为ä¸åŒçš„å±å¹•è‡ªå®šä¹‰è§†å›¾å¤„ç†å¸ƒå±€å–决于您的具体情况。

如果å¯èƒ½çš„è¯ï¼ŒåŠ¨ç”»ä¼šæœ‰ç‚¹æ£˜æ‰‹ã€‚快速æœç´¢it is not。

更新以å‘表评论&#34;我åªæƒ³è‡ªå·±è½®æ¢ä¸€äº›è§†å›¾è€Œä¸æ˜¯æ—‹è½¬å¸ƒå±€ï¼†ï¼ƒ34;

ç†è®ºä¸Šå¯ä»¥åˆ›å»ºè‡ªå·±çš„布局并处ç†å­è§†å›¾çš„绘制。我åªæ˜¯è¯•äº†ä¸€ä¸‹ï¼Œä½†åœ¨é€‚当的时候无法产生任何结果,但你需è¦åšä»€ä¹ˆï¼š

  • ä¿ç•™æ‚¨çš„上次测é‡å€¼åœ¨è§†å›¾æˆ–类似方法上使用标记以ä¿ç•™æœ€åŽçš„测é‡å’Œå¸ƒå±€ï¼Œä»¥ä¾¿åœ¨æ–¹å‘更改åŽå¯ä»¥è¿›è¡Œå·®å¼‚
  • 等待方å‘更改:触å‘旋转绘图 - 旋转画布,使用之å‰çš„尺寸布局视图,并绘制å­è§†å›¾ä»–们本æ¥æ˜¯ä¹‹å‰çš„å’Œ
  • å¯åŠ¨åŠ¨ç”»ä»Ž last æ’值到 new 值,将画布从最åŽä¸€ä¸ªæ—‹è½¬åˆ°æ–°å¸ƒå±€

我就是这样åšçš„。