android – 如何在方位更改时旋转视图,而不重新创建布局?

前端之家收集整理的这篇文章主要介绍了android – 如何在方位更改时旋转视图,而不重新创建布局?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题在 here以前被问到,但是答案是错误的.我想旋转屏幕方向改变的一些视图,但是我想保持布局不变.根据当前方向(SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAIT),它们应旋转90,180,270或360度.

这是我想要实现的:

我提到的链接中的答案说,我应该在布局区域中创建一个新的不同布局.显然,这不是我想要的.我不想重新创建活动或更改布局方向.我只想旋转一些视图,并在方向更改时保持其他视图不变.

旋转特定视图和更改或重新创建整个布局(无论是方向更改)都有很大差异.

使用这个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()方法中的方向更改,而不更改布局?”问题是如果我已经禁用布局方向更改,它将无法检测到任何方向更改.

任何人都知道如何完成我可能完全遇到错误的步骤,我想我将不得不使用加速度传感器或类似的东西来实现我想要的,所以请指导我.

解决方法

尝试使用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);
        }
    }
}
}

这也可防止当定向为45°,135°等时太频繁的旋转希望它有帮助.

原文链接:https://www.f2er.com/android/311559.html

猜你在找的Android相关文章