android 11源码修改屏幕方向

本文详细介绍了如何在基于rk3566芯片的设备上,针对Android11.0系统,通过修改BoardConfig.mk文件来改变屏幕方向,以及通过调整buildinfo.sh和TouchInputMapper.cpp文件来调整触摸屏的方向。具体步骤包括设置旋转角度属性并处理不同旋转情况下的坐标转换。
摘要由CSDN通过智能技术生成

实战记录,我的芯片是rk3566。
1.修改屏幕方向:
打开 android-11.0/device/rockchip/rk356x/BoardConfig.mk配置文件,修改配置:
SF_PRIMARY_DISPLAY_ORIENTATION := 180
2.修改触摸的方向:
1)配置触摸方向:
在android-11.0/build/tools/buildinfo.sh的最后一行增加:echo “persist.sys.touch.rotation=90”
2)打开文件frameworks/native/services/inputflinger/reader/mapper/TouchInputMapper.cpp,找到方法:void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded)

if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
            // Convert rotated viewport to natural surface coordinates.
            int32_t naturalLogicalWidth, naturalLogicalHeight;
            int32_t naturalPhysicalWidth, naturalPhysicalHeight;
            int32_t naturalPhysicalLeft, naturalPhysicalTop;
            int32_t naturalDeviceWidth, naturalDeviceHeight;
			/新增代码
			char value[PROPERTY_VALUE_MAX];// 使用PROPERTY_VALUE_MAX宏,需要引入头文件#include <cutils/properties.h>
            int orientation = 0;
            // 关键在这里,需要自己先行配置该属性
            property_get("persist.sys.touch.rotation", value, "180");// 这里面的默认值,建议填自己需要旋转的角度
            switch(atoi(value)){
                case 90:
                    orientation = DISPLAY_ORIENTATION_90;
                    break;
                case 180:
                    orientation = DISPLAY_ORIENTATION_180;
                    break;
                case 270:
                    orientation = DISPLAY_ORIENTATION_270;
                    break;
                case 0:
                default:
                    orientation = DISPLAY_ORIENTATION_0;
                    break;
            }
            mViewport.orientation = (mViewport.orientation + orientation) % 4;
			//

            switch (mViewport.orientation) {
                case DISPLAY_ORIENTATION_90:
                    naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
                    naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
                    naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
                    naturalPhysicalTop = mViewport.physicalLeft;
                    naturalDeviceWidth = mViewport.deviceHeight;
                    naturalDeviceHeight = mViewport.deviceWidth;
                    break;
                case DISPLAY_ORIENTATION_180:
                    naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
                    naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
                    naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
                    naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
                    naturalDeviceWidth = mViewport.deviceWidth;
                    naturalDeviceHeight = mViewport.deviceHeight;
                    break;
                case DISPLAY_ORIENTATION_270:
                    naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
                    naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
                    naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalLeft = mViewport.physicalTop;
                    naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
                    naturalDeviceWidth = mViewport.deviceHeight;
                    naturalDeviceHeight = mViewport.deviceWidth;
                    break;
                case DISPLAY_ORIENTATION_0:
                default:
                    naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
                    naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
                    naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalLeft = mViewport.physicalLeft;
                    naturalPhysicalTop = mViewport.physicalTop;
                    naturalDeviceWidth = mViewport.deviceWidth;
                    naturalDeviceHeight = mViewport.deviceHeight;
                    break;
            }

            if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
                ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
                naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
                naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
            }

            mPhysicalWidth = naturalPhysicalWidth;
            mPhysicalHeight = naturalPhysicalHeight;
            mPhysicalLeft = naturalPhysicalLeft;
            mPhysicalTop = naturalPhysicalTop;

            mRawSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
            mRawSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
            mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
            mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
            mSurfaceRight = mSurfaceLeft + naturalLogicalWidth;
            mSurfaceBottom = mSurfaceTop + naturalLogicalHeight;

            mSurfaceOrientation =
                    mParameters.orientationAware ? mViewport.orientation : DISPLAY_ORIENTATION_0;
        } else {
            mPhysicalWidth = rawWidth;
            mPhysicalHeight = rawHeight;
            mPhysicalLeft = 0;
            mPhysicalTop = 0;

            mRawSurfaceWidth = rawWidth;
            mRawSurfaceHeight = rawHeight;
            mSurfaceLeft = 0;
            mSurfaceTop = 0;
            mSurfaceOrientation = DISPLAY_ORIENTATION_0;
        }

直接在设置触摸方向的switch前面设置旋转变量mViewport.orientation = DISPLAY_ORIENTATION_180;
结束:
这个方法,本人是亲自在公司的实际项目中验证通过的,希望能对阅读的你,有所帮助。

Android 11屏幕适配方面引入了新的特性,可以更好地适配不同尺寸和密度的屏幕。下面是动态适配屏幕的一些步骤: 1. 使用最新的 Android Studio 创建一个新的 Android 项目,然后选择 Android 11 作为目标版本。 2. 在 AndroidManifest.xml 文件中,添加以下代码: ``` <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" /> ``` 其中,minSdkVersion 是最低支持的 Android 版本,targetSdkVersion 是目标 Android 版本。 3. 在 build.gradle 文件中添加以下依赖项: ``` dependencies { implementation 'androidx.core:core-ktx:1.3.2' implementation 'com.google.android.material:material:1.3.0-alpha02' } ``` 4. 在 res/values 文件夹中创建一个新的 dimens.xml 文件,然后添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="dp_320">320dp</dimen> <dimen name="dp_480">480dp</dimen> <dimen name="dp_600">600dp</dimen> <dimen name="dp_720">720dp</dimen> <dimen name="dp_800">800dp</dimen> </resources> ``` 这里定义了一些常见的屏幕尺寸,可以根据自己的需求添加或修改。 5. 在 res/layout 文件夹中创建一个新的 layout.xml 文件,然后添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这里使用了 ConstraintLayout 来布局,TextView 的宽度设置为 0dp,通过约束来适配不同的屏幕尺寸。 6. 在 MainActivity.java 文件中添加以下代码: ``` import android.content.res.Configuration; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.WindowCompat; import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsControllerCompat; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取屏幕密度 float density = getResources().getDisplayMetrics().density; // 获取屏幕宽度 int screenWidth = getResources().getDisplayMetrics().widthPixels; // 获取屏幕高度 int screenHeight = getResources().getDisplayMetrics().heightPixels; // 获取屏幕方向 int orientation = getResources().getConfiguration().orientation; // 获取屏幕尺寸 int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; // 设置状态栏和导航栏颜色 WindowCompat.setDecorFitsSystemWindows(getWindow(), false); WindowInsetsControllerCompat insetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView()); insetsController.setSystemBarsAppearance(WindowInsetsControllerCompat.APPEARANCE_LIGHT_NAVIGATION_BARS, WindowInsetsControllerCompat.APPEARANCE_LIGHT_NAVIGATION_BARS); // 根据屏幕尺寸动态适配布局 if (screenSize >= Configuration.SCREENLAYOUT_SIZE_XLARGE) { // 大屏幕 if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏 findViewById(R.id.textView).setMinimumWidth((int) (density * 600)); } else { // 竖屏 findViewById(R.id.textView).setMinimumWidth((int) (density * 320)); } } else { // 小屏幕 findViewById(R.id.textView).setMinimumWidth(screenWidth); } } } ``` 这里获取了屏幕的一些信息,包括屏幕密度、宽度、高度、方向和尺寸,然后根据不同的屏幕尺寸动态适配布局。 7. 运行程序,可以看到 TextView 的宽度根据屏幕尺寸动态适配了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值