Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> App層focus or face detection界面顯示分析

App層focus or face detection界面顯示分析

編輯:關於Android編程

Android應用的重要工作就是更新界面顯示,在camera應用中也不例外, 觀察目錄結構,發現ui相關的類和接口保存在src/com/android/camera/ui 文件夾下,在使用相機時我們發現無論是對焦還是人臉識別他們的相關界面是極其相似的,究其原因是在代碼實現的過程中利用了面向對象的重要性質-----多態。

代碼分析:

首先界面更新調用了FocusManager.java中的updateFocusUI()方法。

public void updateFocusUI() {

       if (!mInitialized) return;

 

       // Set the length of focus indicator according to preview frame size.

       int len = Math.min(mPreviewFrame.getWidth(), mPreviewFrame.getHeight()) / 4;

       ViewGroup.LayoutParams layout = mFocusIndicator.getLayoutParams();

       layout.width = len;

       layout.height = len;

 

       // Show only focus indicator or face indicator.

       boolean faceExists = (mFaceView != null && mFaceView.faceExists());

       FocusIndicator focusIndicator = (faceExists) ? mFaceView : mFocusIndicator;

 

       if (mState == STATE_IDLE) {

           if (mFocusArea == null) {

                focusIndicator.clear();

           } else {

               // Users touch on the preview and the indicator represents the

               // metering area. Either focus area is not supported or

               // autoFocus call is not required.

               focusIndicator.showStart();

           }

       } else if (mState == STATE_FOCUSING || mState == STATE_FOCUSING_SNAP_ON_FINISH) {

           focusIndicator.showStart();

       } else {

           if (mState == STATE_SUCCESS) {

                focusIndicator.showSuccess();

           } else if (mState == STATE_FAIL) {

               focusIndicator.showFail();

           }

       }

}

多態實現代碼用綠色表示,根據代碼FaceView,FocusIndicatorView 都繼承了FocusIndicator接口,而實際上此接口就定義了顯示規范。

public class FaceView extends View implements FocusIndicator, Rotatable {

 

public class FocusIndicatorView extends View implements FocusIndicator {

 

public interface FocusIndicator {

    public void showStart();

    public void showSuccess();

    public void showFail();

    public void clear();

}

另一方面,FaceView,FocusIndicatorView在顯示中還有細微不同,最重要的是FaceView可以識別多個人臉,顯示多個識別框。這個功能是通過重寫view的onDraw(Canvas canvas)方法實現的。

代碼分析:

@Override

    protected void onDraw(Canvas canvas) {

       if (mFaces != null && mFaces.length > 0) {

           // Prepare the matrix.

           Util.prepareMatrix(mMatrix, mMirror, mDisplayOrientation, getWidth(), getHeight());

 

           // Focus indicator is directional. Rotate the matrix and the canvas

           // so it looks correctly in all orientations.

           canvas.save();

           mMatrix.postRotate(mOrientation); // postRotate is clockwise

           canvas.rotate(-mOrientation); // rotate is counter-clockwise (for canvas)

            for (int i = 0; i < mFaces.length; i++) {

               // Transform the coordinates.

               mRect.set(mFaces[i].rect);

               if (LOGV) Util.dumpRect(mRect, "Original rect");

               mMatrix.mapRect(mRect);

               if (LOGV) Util.dumpRect(mRect, "Transformed rect");

 

               mFaceIndicator.setBounds(Math.round(mRect.left), Math.round(mRect.top),

                       Math.round(mRect.right), Math.round(mRect.bottom));

               mFaceIndicator.draw(canvas);

           }

           canvas.restore();

       }

       super.onDraw(canvas);

    }

}

綠色部分是通過遍歷face數組畫出每個人臉所對應的識別框。

 

 


作者:cibon
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved