編輯:關於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數組畫出每個人臉所對應的識別框。
幾個月沒有碰Android Studio了,打開時卻突然出現了這樣的錯誤:我可是百事不得其解啊!我最後一次使用的時候都好好的,現在居然說我的Java環境變量有問題。我一看
顧名思義Camera就是拍照和錄像的功能,像微信裡面,我們想拍照傳一下照片,就可以通過camera來拍照,然後存儲照片,發送給好友。那麼微信的app裡面是不會直接通過ca
智能手機的迅速普及,大大的豐富了我們的娛樂生活。現在大家都喜歡晚上睡覺前玩會兒手機,但是應用的日間模式往往亮度太大,對眼睛有較為嚴重的傷害。因此,如今的應用往往開發了 日
上個周末,3個小時總體上讀完了《Android群英傳》,本周主要在研究代碼層次的設計和實現。編譯安裝在手機上,玩了幾把,結合代碼,一周時間才掌握了整體的思路。大部分時間,