Android P上应用显示高宽比

  • android:maxAspectRatio属性使用
    • maxAspectRatio原理

android:maxAspectRatio属性使用

default 情况 maxAspectRatio = 1.86f;
在AndroidManiferst.xml中使用

<activity android:name=".MainActivity"            android:maxAspectRatio="1.98">

注意:此属性生效的前提是

android:resizeableActivity="false"//如果为true会根据手机屏幕自动的调整显示大小。

maxAspectRatio原理

在PMS解析AndroidManifest.xml的时候对maxAspectRatio处理如下:

// 解析activity时候如设置了maxAspectRatio就将设置好的传输进去。 private Activity parseActivity(Package owner, Resources res,            XmlResourceParser parser, int flags, String[] outError, CachedComponentArgs cachedArgs,            boolean receiver, boolean hardwareAccelerated)            throws XmlPullParserException, IOException {if (sa.hasValue(R.styleable.AndroidManifestActivity_maxAspectRatio)                    && sa.getType(R.styleable.AndroidManifestActivity_maxAspectRatio)                    == TypedValue.TYPE_FLOAT) {                a.setMaxAspectRatio(sa.getFloat(R.styleable.AndroidManifestActivity_maxAspectRatio,                        0 /*default*/));            }            ....//接着看一下setMaxAspectRatio,先判断是否可以resize,也就是前面的android:resizeableActivity="false"。private void setMaxAspectRatio(float maxAspectRatio) {            if (info.resizeMode == RESIZE_MODE_RESIZEABLE                    || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {                // Resizeable activities can be put in any aspect ratio.                return;            }            if (maxAspectRatio < 1.0f && maxAspectRatio != 0) {                // Ignore any value lesser than 1.0.                return;            }            info.maxAspectRatio = maxAspectRatio;            mHasMaxAspectRatio = true;        }//如果没有该属性 再设置默认的。    private static final float DEFAULT_PRE_O_MAX_ASPECT_RATIO = 1.86f;    ....    /**     * Sets every the max aspect ratio of every child activity that doesn't already have an aspect     * ratio set.     */    private void setMaxAspectRatio(Package owner) {        // Default to (1.86) 16.7:9 aspect ratio for pre-O apps and unset for O and greater.        // NOTE: 16.7:9 was the max aspect ratio Android devices can support pre-O per the CDD.        float maxAspectRatio = owner.applicationInfo.targetSdkVersion < O                ? DEFAULT_PRE_O_MAX_ASPECT_RATIO : 0;                //android o 之前的用默认1.86,之后的就根据手机;屏幕。        if (owner.applicationInfo.maxAspectRatio != 0) {            // Use the application max aspect ration as default if set.            maxAspectRatio = owner.applicationInfo.maxAspectRatio;        } else if (owner.mAppMetaData != null                && owner.mAppMetaData.containsKey(METADATA_MAX_ASPECT_RATIO)) {            maxAspectRatio = owner.mAppMetaData.getFloat(METADATA_MAX_ASPECT_RATIO, maxAspectRatio);        }        for (Activity activity : owner.activities) {            // If the max aspect ratio for the activity has already been set, skip.            if (activity.hasMaxAspectRatio()) {                continue;                //如果activity已经有这个属性了就不在重新设置,如果没有在设置一个默认的。            }            // By default we prefer to use a values defined on the activity directly than values            // defined on the application. We do not check the styled attributes on the activity            // as it would have already been set when we processed the activity. We wait to process            // the meta data here since this method is called at the end of processing the            // application and all meta data is guaranteed.            final float activityAspectRatio = activity.metaData != null                    ? activity.metaData.getFloat(METADATA_MAX_ASPECT_RATIO, maxAspectRatio)                    : maxAspectRatio;            activity.setMaxAspectRatio(activityAspectRatio);        }    }

这个属性的使用:

// frameworks/base/services/core/java/com/android/server/am/ActivityRecord.java/**     * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.     */    // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.    private void computeBounds(Rect outBounds) {        outBounds.setEmpty();        final float maxAspectRatio = info.maxAspectRatio;        final ActivityStack stack = getStack();        if (task == null || stack == null || !task.mFullscreen || maxAspectRatio == 0                || isInVrUiMode(getConfiguration())) {            // We don't set override configuration if that activity task isn't fullscreen. I.e. the            // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for            // the activity. This is indicated by an empty {@link outBounds}. We also don't set it            // if we are in VR mode.            return;            //如果是0直接就pass了没有后面计算过程了。        }        ...

更多相关文章

  1. 我的Android心得(7)--TabActivity
  2. Android(安卓)日期控件属性
  3. Android(安卓)布局 & 一些控件
  4. android EditText 全面阐述
  5. Android(安卓)支持多屏幕机制
  6. Android(安卓)SAX API XmlResourceParser及其扩展应用
  7. Android中style的使用
  8. Android(安卓)TextView 文字居中 .
  9. Android(安卓)Fresco属性大全,中文说明

随机推荐

  1. android RecyclerView布局真的只是那么简
  2. 处女男学Android(十三)---Android(安卓)轻
  3. Android(安卓)DiskLruCache完全解析,硬盘
  4. Android Studio无法真机调试
  5. Android APIDemos 研读之一:android.graph
  6. [Android]Thread线程入门3--多线程
  7. 启动一个没有界面的Activity(且没有焦点)
  8. Android studio APP开发 单选框和复选框
  9. Android常用面向对象设计模式
  10. Android如何让EditText不自动获取焦点