Android 11.0 Camera2 默认选择拍照尺寸修改及流程分析

一、前言

最近有一个需求是修改默认的照片尺寸,这篇文章就总结一下Camara 选择默认的拍照尺寸的逻辑吧。

二、定位代码

定位图片

2.1、根据“照片大小” text 可以定位到布局文件 picturesize_preference.xml.

代码路径:
android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\res\xml\picturesize_preference.xml`**

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.mediatek.camera.common.preference.Preference
        android:key="key_picture_size"
        android:title="@string/pref_camera_picturesize_title"
        android:persistent="false"
        android:order="40"/>
</PreferenceScreen>

Java 引用文件为:
代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\src\com\mediatek\camera\feature\setting\picturesize\PictureSizeSettingView.java

三、流程逻辑分析

3.1、PictureSizeSettingView 照片尺寸列表值的初始化

PictureSizeSettingView 的 setEntryValues 是传入照片尺寸列表的,这里是在其他地方调用传入值得。
代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\src\com\mediatek\camera\feature\setting\picturesize\PictureSizeSettingView.java


    /**
     * Set the picture sizes supported.
     *
     * @param entryValues The picture sizes supported.
     */
    public void setEntryValues(List<String> entryValues) {
   
        mEntryValues = entryValues;
    }

3.2、PictureSize.java 设置默认尺寸和可选尺寸

全局搜一下可以发现是在PictureSize.java 中传入EntryValue的
代码路径: vendor/mediatek/proprietary/packages/apps/Camera2/feature/setting/picturesize/src/com/mediatek/camera/feature/setting/picturesize/PictureSize.java

总结一下流程:

1、获取屏幕的尺寸来计算出宽高比作为全屏的宽高比。
2、获取Sensor 传上来的可支持的照片尺寸supportedPictureSize。
3、从supportedPictureSize这里遍历选出可以用的尺寸里面最接近全屏宽高比的尺寸。
4、将全屏宽高比里面尺寸作为默认选中的尺寸。

    /**
     * Invoked after setting's all values are initialized.
     *
     * @param supportedPictureSize Picture sizes which is supported in current platform.
     */
    public void onValueInitialized(List<String> supportedPictureSize) {
   
        LogHelper.d(TAG, "[onValueInitialized], supportedPictureSize:" + supportedPictureSize);

         //1、这里获取屏幕的尺寸来计算出宽高比作为全屏的宽高比。
        double fullRatio = PictureSizeHelper.findFullScreenRatio(mActivity);
        List<Double> desiredAspectRatios = new ArrayList<>();
        desiredAspectRatios.add(fullRatio);
        desiredAspectRatios.add(PictureSizeHelper.RATIO_4_3);
        PictureSizeHelper.setDesiredAspectRatios(desiredAspectRatios);
        PictureSizeHelper.setFilterParameters(DEGRESSIVE_RATIO, MAX_COUNT);
        if (sFilterPictureSize) {
   
            supportedPictureSize = PictureSizeHelper.filterSizes(supportedPictureSize);
            LogHelper.d(TAG, "[onValueInitialized], after filter, supportedPictureSize = "
                    + supportedPictureSize);
        
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mrsongs的心情杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值