Android 12 - 部分相机横屏显示方案
1.相机过渡界面方向旋转
Android 10 - 相机过渡界面默认角度
同A10 有些区别,再次增加记录修改。
这个文件没有修改,只是说明
src/com/android/camera/CameraActivity.javaprivate void freezeScreenCommon(boolean async) {long startTime = System.currentTimeMillis();mCameraAppUI.setModeCoverState(1);mStartTime = startTime;if (mCurrentModule.isUseSurfaceView()) {Bitmap screenShot = getSurfaceViewBitmap();if (screenShot == null) {freezeScreenUntilPreviewReady();//SPRD:fix bug1116034} else {//CameraUtil.saveBitmapToFile(screenShot);// 经过注释验证,将下面的两个方法注释,就不会显示过渡画面screenShot = CameraUtil.blurBitmap(CameraUtil.computeScale(screenShot, 0.2f), mAppContext);mCameraAppUI.freezeScreenUntilPreviewReady(screenShot, async);}} else {freezeScreenUntilPreviewReady(async);}Log.i(TAG, "freezeScreenCommon cost: " + (System.currentTimeMillis() - startTime));}
src/com/android/camera/util/CameraUtil.java// 旋转 Bitmap 的辅助方法
private static Bitmap rotateBitmap(Bitmap source, float angle) {if (source == null) {return null;}Log.d(TAG, "Original width: " + source.getWidth() + ", height: " + source.getHeight());Matrix matrix = new Matrix();matrix.postRotate(angle);Bitmap rotatedBitmap = Bitmap.createBitmap(source,0, 0,source.getWidth(),source.getHeight(),matrix,true);// 打印旋转后图片宽高Log.d(TAG, "Rotated width: " + rotatedBitmap.getWidth() + ", height: " + rotatedBitmap.getHeight());return rotatedBitmap;
}public static Bitmap blurBitmap(Bitmap bitmap, Context context) {if (bitmap == null) {return null;}long startMs = System.currentTimeMillis();// 这里是新增加的方法,实现过渡画面的角度,当前项目旋转90度即可bitmap = rotateBitmap(bitmap, 90);// Instantiate a new RenderscriptRenderScript rs = RenderScript.create(context);
src/com/android/camera/app/CameraAppUI.javapublic void freezeScreenUntilPreviewReady(Bitmap bitmap, boolean async) {Log.v(TAG, "freezeScreenUntilPreviewReady bitmap=" + bitmap + "; async=" + async + " getPreviewArea :" + getPreviewAreaLocal());// lichang 修改原有获取预览区域方法为自行获取,原尺寸异常freezeScreen(bitmap, getPreviewAreaLocal(), async);}/*** 根据宽高比获取预览区域*/public RectF getPreviewAreaLocal() {Log.v(TAG, "getPreviewAreaLocal getAspectRation() = " + getAspectRation());float previewWidth = 960f;float screenWidth = 1280f;float aspectRation = getAspectRation();float epsilon = 0.001f; // 允许的误差范围if (Math.abs(aspectRation - (4f / 3f)) < epsilon) {// 4:3 比例(≈1.333)previewWidth = 960f;} else if (Math.abs(aspectRation - (16f / 9f)) < epsilon) {// 16:9 比例(≈1.777)previewWidth = 1280f;} else {// 其他比例,默认值或错误处理previewWidth = 1280f; // 或 throw new IllegalArgumentException("Unsupported aspect ratio");}float left = (screenWidth - previewWidth) / 2f;Log.v(TAG, "getPreviewAreaLocal left = " + left + " right = " + left + previewWidth);return new RectF(left, 0f, left + previewWidth, 720f);}// 新增方法public float getAspectRation() {return mTextureViewHelper.getAspectRation();}
src/com/android/camera/TextureViewHelper.javapublic void setAspectRation(float aspectRation) {mAspectRatio = aspectRation;}// 新增方法,获取当前宽高比public float getAspectRation() {return mAspectRatio;}
2.相机图标位置居中
+++ b/src/com/android/camera/ui/ModeTransitionView.java
@@ -143,6 +143,7 @@ public class ModeTransitionView extends View {mIconDrawable.setAlpha(alpha);}}
+ Log.e(TAG, " updateShade mWidth = " + mWidth + " mHeight = " + mHeight);invalidate();}}
@@ -172,6 +173,7 @@ public class ModeTransitionView extends View {@Overridepublic void onDraw(Canvas canvas) {
+ Log.e(TAG, " lichang onDraw ");if (mAnimationType == PEEP_HOLE_ANIMATION) {canvas.drawColor(mBackgroundColor);if (mPeepHoleAnimator != null) {
@@ -227,8 +229,9 @@ public class ModeTransitionView extends View {mWidth = displaySize.getWidth();mHeight = displaySize.getHeight();boolean landscape = mWidth > mHeight;
- int centerW = landscape ? mHeight : mWidth;
- int centerH = landscape ? mWidth : mHeight;
+ // lichang 横屏也不需要调换位置,修改打开相机显示的图标位置
+ int centerW = !landscape ? mHeight : mWidth;
+ int centerH = !landscape ? mWidth : mHeight;if (CameraUtil.hasCutout()) {centerH -= CameraUtil.getCutoutHeight();
3.预览画面居中
默认4:3,显示的预览画面偏移,需要重新计算位置。
+++ b/src/com/android/camera/SurfaceViewEx.java
@@ -19,6 +19,8 @@package com.android.camera;+import android.util.DisplayMetrics;
+import android.view.WindowManager;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;
@@ -165,8 +167,16 @@ public class SurfaceViewEx extends SurfaceView {params.height = (int) scaledTextureHeight;RectF rect = mCameraAppUI.getPreviewArea();// horizontal direction
- params.setMargins((int) rect.left, (int) rect.top, 0, 0);
-
+ //params.setMargins((int) rect.left, (int) rect.top, 0, 0);
+ // lichang 将预览画面居中
+ /*@start*/
+ DisplayMetrics displayMetrics = new DisplayMetrics();
+ ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
+ int screenWidth = displayMetrics.widthPixels;
+ int screenHeight = displayMetrics.heightPixels;
+ Log.i(TAG, "setTransformMatrix(): screenWidth = " + screenWidth + " screenHeight = " + screenHeight);
+ params.setMargins((int) (screenWidth - scaledTextureWidth) / 2, (int) (screenHeight - scaledTextureHeight) / 2, 0, 0);
+ /*end*/setLayoutParams(params);Log.i(TAG, "setTransformMatrix(): width = " + previewWidth