有关Android中getRotation的探讨。

oldmtn 2014-09-04 01:03:50
我们大家都知道,通常情况下,使用int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();可以获取屏幕旋转的方向。

但是这个rotation的返回值到底有和含义,在网上搜索了很多帖子,大多是很含糊的描述了一下获知就是不负责任的转载。

我们看getRotation的API描述,其中有“Returns the rotation of the screen from its "natural" orientation.”

这里GOOGLE自己给"natural"加了引号,意思就是正常是人为定义的,是可变的。

对于一个默认为横屏的pad而言,如何定义默认为横屏?经过我的实验,发现这个横屏或者竖屏是根据camera的方向定的。
对于竖屏而言,同样如此。

如下图:


...全文
679 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
youyoufruit 2014-11-07
  • 打赏
  • 举报
回复
原来是CameraInfo cameraInfo = new CameraInfo; cameraInfo.oriention啊
youyoufruit 2014-11-06
  • 打赏
  • 举报
回复
你好,我也遇到这样的问题,用不同的平板测,int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();得到的rotation是不一样的,宽的一边有的是0,2,有的是1,3。好像分默认水平和默认竖直的,不知道改怎么判断系统默认方向(这个值应该是固定的)?求解答
是歐陽公子啊 2014-10-08
  • 打赏
  • 举报
回复
lz能针对这幅图说的再详细点吗?谢谢了
lionfresh 2014-09-04
  • 打赏
  • 举报
回复
可以将你的研究写篇博文了。
oldmtn 2014-09-04
  • 打赏
  • 举报
回复

第一幅图不对,现在修正。
oldmtn 2014-09-04
  • 打赏
  • 举报
回复
汗,我怎么还没编辑完,就自动提交了呢? 在做Camera的PreviewSize时,必须考虑包含Camera的SurfaceView的父窗口的 “ 宽高比“。 但是getPictureSize返回的总是"宽 > 高"一个尺寸。 同时我们还要考虑摄像头默认方向;旋转角度;系统预定义的预览尺寸;父窗口大小。。。 已经旋转切换。 这些很复杂了。
oldmtn 2014-09-04
  • 打赏
  • 举报
回复
/** * A layout which handles the preview aspect ratio and the position of * the gripper. */ public class PreviewFrameLayout extends ViewGroup { private static final int MIN_HORIZONTAL_MARGIN = 0; // 10dp /** A callback to be invoked when the preview frame's size changes. */ public interface OnSizeChangedListener { public void onSizeChanged(); } private double mAspectRatio = 3.0 / 4.0; // 宽 : 高 private FrameLayout mFrame; private OnSizeChangedListener mSizeListener; private final DisplayMetrics mMetrics = new DisplayMetrics(); public PreviewFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); ((Activity) context).getWindowManager() .getDefaultDisplay().getMetrics(mMetrics); } public void setOnSizeChangedListener(OnSizeChangedListener listener) { mSizeListener = listener; } @Override protected void onFinishInflate() { mFrame = (FrameLayout) findViewById(R.id.frame); if (mFrame == null) { throw new IllegalStateException( "must provide child with id as \"frame\""); } } public void setAspectRatio(double ratio) { if (ratio <= 0.0) throw new IllegalArgumentException(); if (mAspectRatio != ratio) { mAspectRatio = ratio; requestLayout(); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // Try to layout the "frame" in the center of the area, and put // "gripper" just to the left of it. If there is no enough space for // the gripper, the "frame" will be moved a little right so that // they won't overlap with each other. int frameWidth = getWidth(); int frameHeight = getHeight(); FrameLayout f = mFrame; int horizontalPadding = f.getPaddingLeft() + f.getPaddingRight(); int verticalPadding = f.getPaddingTop() + f.getPaddingBottom(); // Ignore the vertical paddings, so that we won't draw the frame on the // top and bottom sides int previewHeight = frameHeight; int previewWidth = frameWidth; // resize frame and preview for aspect ratio int idealPreviewWidth = (int)(previewHeight * mAspectRatio); if (previewWidth > idealPreviewWidth ) { previewWidth = (int) (idealPreviewWidth + .5); } else { previewHeight = (int) (previewWidth / mAspectRatio + .5); } frameWidth = previewWidth + horizontalPadding; frameHeight = previewHeight + verticalPadding; int hSpace = ((r - l) - frameWidth) / 2; int vSpace = ((b - t) - frameHeight) / 2; mFrame.measure( MeasureSpec.makeMeasureSpec(frameWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(frameHeight, MeasureSpec.EXACTLY)); mFrame.layout(l + hSpace, t + vSpace, r - hSpace, b - vSpace); if (mSizeListener != null) { mSizeListener.onSizeChanged(); } } }
		Size size0 = parameters.getPictureSize();
		if( rotation == 0 || rotation == 2 ) {
			mPreviewFL.setAspectRatio((double) size0.height / size0.width);
			
			nWidth = width;
			nHeight = height;
		} else if( rotation == 1 || rotation == 3) {
			mPreviewFL.setAspectRatio((double) size0.width / size0.height);
			
			nWidth = width;
			nHeight = height;
		}

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧