Camera 图标加载(flash)

本文详细介绍了相机应用中快速切换器(quick_switcher)的加载过程,包括从布局加载、图标初始化到点击事件处理。同时,深入探讨了闪光灯功能的实现,包括布局初始化、图标加载、点击事件触发以及状态更新。整个流程展示了从界面构建到功能交互的完整逻辑。
摘要由CSDN通过智能技术生成

1.加载初始桌面布局camera_ui_root,对应 quick_switcher
2.加载quick_switcher相关布局QuickSwitcherManager  setVisibility
3.加载flash相关图标布局FlashViewController,mMainHandler.sendEmptyMessage(FLASH_VIEW_INIT);
4.初始化:initFlashEntryView,flash_icon.xml
5.通过mSettingController.addViewEntry();将其加入到 quick_switcher 中。
6.点击相应图标触发 mFlashEntryListener 事件,加载布局
7.触发 mFlashChoiceViewListener 点击事件重新加载布局


host/res/layout/camera_ui_root.xml
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/top_bar"
                android:layoutDirection="ltr"
                android:clickable="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <!-- 设置图标--!>
                <com.mediatek.camera.common.widget.RotateImageView
                    android:id="@+id/mode"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="@dimen/settings_margin_left"
                    android:layout_marginTop="@dimen/settings_margin_top"
                    android:clickable="true"
                    android:contentDescription="@string/accessibility_mode"
                    android:focusable="false"
                    android:scaleType="fitCenter"
                    android:visibility="visible" />
                <!-- switch 图标--!>
                <LinearLayout
                    android:id="@+id/quick_switcher"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="@dimen/quick_switcher_margin_right_dp"
                    android:layout_marginTop="@dimen/quick_switcher_margin_top"
                    android:layout_toRightOf="@+id/mode"
                    android:gravity="center_vertical"
                    android:layoutDirection="rtl"
                    android:orientation="horizontal" />

            </LinearLayout>
        </RelativeLayout>


    quick_switcher 加载
host/src/com/mediatek/camera/ui/CameraAppUI.java
    mQuickSwitcherManager = new QuickSwitcherManager(mApp, parentView);
    mQuickSwitcherManager.setVisibility(View.VISIBLE);//1111
    mQuickSwitcherManager.setModeChangeListener(new OnQuickModeChangedListenerImpl());
    mViewManagers.add(mQuickSwitcherManager);

host/src/com/mediatek/camera/ui/AbstractViewManager.java
    protected abstract View getView();

    @Override
    public void setVisibility(int visibility) {
        if (visibility == View.VISIBLE) {
            show();
        } else if (visibility == View.INVISIBLE) {
            hide(View.INVISIBLE);
        } else if (visibility == View.GONE) {
            hide(View.GONE);
        }
    }


    @Override
    protected View getView() {

        LogHelper.d(TAG,"[getView]");
        RuntimeException e = new RuntimeException("print stacktrace");
        Stream.of(e.getStackTrace()).forEach(System.out::println);
        
        mQuickSwitcherLayout = (LinearLayout) mParentView.findViewById(R.id.quick_switcher);
        updateQuickItems();
        return mQuickSwitcherLayout;
    }

feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/Flash.java
        @Override
        public void init(IApp app,
                        ICameraContext cameraContext,
                        ISettingManager.SettingController settingController) {
            super.init(app, cameraContext, settingController);
            ...
            mFlashViewController = new FlashViewController(this, app);
            ...
        }
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java

        public FlashViewController(Flash flash, IApp app) {
            mFlash = flash;
            mApp = app;
            mMainHandler = new MainHandler(app.getActivity().getMainLooper());
            mMainHandler.sendEmptyMessage(FLASH_VIEW_INIT);
        }

        @Override
        public void handleMessage(Message msg) {
            LogHelper.d(TAG, "view handleMessage: " + msg.what);
            switch (msg.what) {
                case FLASH_VIEW_INIT:
                    mFlashEntryView = initFlashEntryView();
                    break;
            }
        }

        private ImageView initFlashEntryView() {
            Activity activity = mApp.getActivity();
            
            RotateImageView view = (RotateImageView) activity.getLayoutInflater().inflate(R.layout.flash_icon, null);

            view.setOnClickListener(mFlashEntryListener);
            mFlashIndicatorView = (RotateImageView) activity.getLayoutInflater().inflate(
                    R.layout.flash_indicator, null);
            return view;
        }

以拍照模式为例:
        common/src/com/mediatek/camera/common/mode/photo/device/PhotoDevice2Controller.java
        mSettingController.addViewEntry();

        common/src/com/mediatek/camera/common/setting/SettingManager.java
        @Override
        public void addViewEntry() {
            ...
            List<ICameraSetting> settingsRelatedToMode = getSettingByModeType(mModeType);
            for (ICameraSetting setting : settingsRelatedToMode) {
                if (!access.isValid()) {
                    break;
                }
                LogHelper.d(mTag, "zzj_[addViewEntry]  111");
                setting.addViewEntry();
            }
            ...
            mAppUi.registerQuickIconDone();
            ...
        }

        feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/Flash.java
        public void addQuickSwitchIcon() {
            mMainHandler.sendEmptyMessage(FLASH_VIEW_ADD_QUICK_SWITCH);
        }

        public void handleMessage(Message msg) {
            LogHelper.d(TAG, "view handleMessage: " + msg.what);
                    switch (msg.what) {
                        case FLASH_VIEW_INIT:
                            mFlashEntryView = initFlashEntryView();
                            break;

                        case FLASH_VIEW_ADD_QUICK_SWITCH:
                            mApp.getAppUi().addToQuickSwitcher(mFlashEntryView, FLASH_PRIORITY);
                            updateFlashEntryView(mFlash.getValue());
                            mApp.getAppUi().registerOnShutterButtonListener(mShutterListener,
                                    FLASH_SHUTTER_PRIORITY);
                            break;
                    }
            }

        host/src/com/mediatek/camera/ui/CameraAppUI.java
        @Override
        public void addToQuickSwitcher(View view, int priority) {
            mQuickSwitcherManager.addToQuickSwitcher(view, priority);
        }
        
        host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
        public void addToQuickSwitcher(View view, int priority) {
            LogHelper.d(TAG, "[registerToQuickSwitcher] priority = " + priority);
            if (mQuickItems.size() > ITEM_LIMIT) {
                LogHelper.w(TAG, "already reach to limit number : " + ITEM_LIMIT);
                return;
            }
            if (!mQuickItems.containsValue(view)) {
                mQuickItems.put(priority, view);
            }
        }


        mFlashViewController.showQuickSwitchIcon(getEntryValues().size() > 1);
        
        feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java
        public void showQuickSwitchIcon(boolean isShow) {
            mMainHandler.obtainMessage(FLASH_VIEW_UPDATE_QUICK_SWITCH_ICON, isShow).sendToTarget();
        }

        public void handleMessage(Message msg) {
            LogHelper.d(TAG, "view handleMessage: " + msg.what);
                    switch (msg.what) {
                        case FLASH_VIEW_UPDATE_QUICK_SWITCH_ICON:
                        if ((boolean) msg.obj) {
                            mFlashEntryView.setVisibility(View.VISIBLE);
                            updateFlashEntryView(mFlash.getValue());
                        } else {
                            mFlashEntryView.setVisibility(View.GONE);
                        }
                        break;
                    }
        }

        public void updateFlashEntryView(final String value) {
            LogHelper.d(TAG, "[updateFlashView] currentValue = " + mFlash.getValue());
            if (FLASH_ON_VALUE.equals(value)) {
                mFlashEntryView.setImageResource(R.drawable.ic_flash_status_on);
                mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
                        R.string.accessibility_flash_on));
            } else if (FLASH_AUTO_VALUE.equals(value)) {
                mFlashEntryView.setImageResource(R.drawable.ic_flash_status_auto);
                mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
                        R.string.accessibility_flash_auto));
            } else {
                mFlashEntryView.setImageResource(R.drawable.ic_flash_status_off);
                mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
                        R.string.accessibility_flash_off));
            }
            // Flash indicator no need to show now,would be enable later
            // updateFlashIndicator(value);
        }


        mAppUi.registerQuickIconDone();
        host/src/com/mediatek/camera/ui/CameraAppUI.java
        @Override
        public void registerQuickIconDone() {
            mApp.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mQuickSwitcherManager.registerQuickIconDone();
                }
            });
        }

        host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
        public void registerQuickIconDone() {
            LogHelper.d(TAG,"[registerQuickIconDone]");
            updateQuickItems();
        }

        //加载小图标
        private void updateQuickItems() {
            LogHelper.d(TAG,"[updateQuickItems]");
            float density = mApp.getActivity().getResources().getDisplayMetrics().density;
            int marginInPix = (int) (MARGIN_IN_DP * density);
            if (mQuickSwitcherLayout != null && mQuickSwitcherLayout.getChildCount() != 0) {
                mQuickSwitcherLayout.removeAllViews();
            }
            if (mQuickSwitcherLayout != null) {
                Iterator iterator = mQuickItems.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry map = (Map.Entry) iterator.next();
                    View view = (View) map.getValue();
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.setMargins(marginInPix, 0, 0, 0);
                    view.setLayoutParams(params);
                    mQuickSwitcherLayout.addView(view);
                }
                updateViewOrientation();
            }
        }

        mQuickSwitcherLayout.addView(view);


//点击事件
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java

    private final View.OnClickListener mFlashEntryListener = new View.OnClickListener() {
        public void onClick(View view) {
            if (mFlash.getEntryValues().size() <= 1) {
                return;
            }
            if (mFlash.getEntryValues().size() > FLASH_ENTRY_LIST_SWITCH_SIZE) {
                initializeFlashChoiceView();
                updateChoiceView();
                mApp.getAppUi().showQuickSwitcherOption(mOptionLayout);
            } else {
                String value = mFlash.getEntryValues().get(FLASH_ENTRY_LIST_INDEX_0);
                if (value.equals(mFlash.getValue())) {
                    value = mFlash.getEntryValues().get(FLASH_ENTRY_LIST_INDEX_1);
                }
                updateFlashEntryView(value);
                // Flash indicator no need to show now,would be enable later
                // updateFlashIndicator(value);
                mFlash.onFlashValueChanged(value);
            }
        }
    };


    private void initializeFlashChoiceView() {
        if (mFlashChoiceView == null || mOptionLayout == null) {
            ViewGroup viewGroup =  mApp.getAppUi().getModeRootView();
            mOptionLayout = mApp.getActivity().getLayoutInflater().inflate(
                    R.layout.flash_option, viewGroup, false);
            mFlashChoiceView = mOptionLayout.findViewById(R.id.flash_choice);
            mFlashOnIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_on);
            mFlashOffIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_off);
            mFlashAutoIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_auto);

            mFlashOffIcon.setOnClickListener(mFlashChoiceViewListener);
            mFlashOnIcon.setOnClickListener(mFlashChoiceViewListener);
            mFlashAutoIcon.setOnClickListener(mFlashChoiceViewListener);
        }
    }

        private void updateChoiceView() {
        if (FLASH_ON_VALUE.equals(mFlash.getValue())) {
            mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on_selected);
            mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off);
            mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto);
        } else if (FLASH_OFF_VALUE.equals(mFlash.getValue())) {
            mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on);
            mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off_selected);
            mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto);
        } else {
            mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on);
            mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off);
            mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto_selected);
        }
    }

host/src/com/mediatek/camera/ui/CameraAppUI.java
    @Override
    public void showQuickSwitcherOption(View optionView) {
        mApp.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mQuickSwitcherManager.showQuickSwitcherOption(optionView);
            }
        });
    }
host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
    public void showQuickSwitcherOption(View optionView) {
        if (mOptionRoot.getChildCount() != 0) {
            LogHelper.e(TAG, "[showQuickSwitcherOption] Already has options to be shown!");
            return;
        }
        Animation inAnim = AnimationUtils.loadAnimation(mApp.getActivity(), R.anim.anim_top_in);
        mOptionRoot.addView(optionView);
        int orientation = mApp.getGSensorOrientation();
        CameraUtil.rotateRotateLayoutChildView(mApp.getActivity(), mOptionRoot, orientation, true);
        mOptionRoot.setVisibility(View.VISIBLE);
        mOptionRoot.setClickable(true);
        mOptionRoot.startAnimation(inAnim);
        mTopBar.setVisibility(View.GONE);
        mApp.registerOnOrientationChangeListener(mOrientationChangeListener);
    }


    private View.OnClickListener mFlashChoiceViewListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String value = "";
            if (mFlashAutoIcon == view) {
                value = FLASH_AUTO_VALUE;
            } else if (mFlashOnIcon == view) {
                value = FLASH_ON_VALUE;
            //*/ hct.huangfei, 20201212.add torch mode for flash.
            } else if (mFlash.isTorchModeSupport()&&mFlashTorchIcon == view) {
                value = FLASH_TORCH_VALUE;
            //*/
            } else {
                value = FLASH_OFF_VALUE;
            }
            mApp.getAppUi().hideQuickSwitcherOption();
            updateFlashEntryView(value);
            // Flash indicator no need to show now,would be enable later
            // updateFlashIndicator(value);
            mFlash.onFlashValueChanged(value);
        }

    };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值