here之å‰å·²ç»é—®è¿‡è¿™ä¸ªé—®é¢˜ï¼Œä½†ç”案是错误的。我想在å±å¹•æ–¹å‘更改上旋转一些视图,但我想ä¿æŒå¸ƒå±€ä¸å˜ã€‚å®ƒä»¬åº”æ ¹æ®å½“å‰æ–¹å‘(SCREEN_ORIENTATION_LANDSCAPE
,SCREEN_ORIENTATION_PORTRAIT
,SCREEN_ORIENTATION_REVERSE_LANDSCAPE
,SCREEN_ORIENTATION_REVERSE_PORTRAIT
)旋转90度,180度,270度或360度。
这就是我想è¦å®žçŽ°çš„ç›®æ ‡ï¼š
我æ到的链接ä¸çš„ç”案表明我应该在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ä¼ æ„Ÿå™¨æˆ–ç±»ä¼¼çš„ä¸œè¥¿æ¥å®žçŽ°æˆ‘想è¦çš„,所以请引导我完æˆã€‚
ç”案 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;
ç†è®ºä¸Šå¯ä»¥åˆ›å»ºè‡ªå·±çš„布局并处ç†å视图的绘制。我åªæ˜¯è¯•äº†ä¸€ä¸‹ï¼Œä½†åœ¨é€‚å½“çš„æ—¶å€™æ— æ³•äº§ç”Ÿä»»ä½•ç»“æžœï¼Œä½†ä½ éœ€è¦åšä»€ä¹ˆï¼š
æˆ‘å°±æ˜¯è¿™æ ·åšçš„。