Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Camera + 回転 + TextureView

Camera + 回転 + TextureView

2016.05.25 関西モバイルアプリ研究会 #14 用資料です。
Camera2のプレビューを表示するTextureViewで、端末の回転やMultiWindowに対応してみた時の記録です。

masanori_msl

May 25, 2016
Tweet

More Decks by masanori_msl

Other Decks in Programming

Transcript

  1. 回転 Camera2Activity.java private void configureTransform(int viewWidth, int viewHeight){ // 画面の回転に合わせてTextureViewの向き、サイズを変更する.

    if (previewTextureView == null || previewSize == null){ return; } runOnUiThread(() ->{ RectF rctView = new RectF(0, 0, viewWidth, viewHeight); RectF rctPreview = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); float centerX = rctView.centerX(); float centerY = rctView.centerY(); Matrix matrix = new Matrix(); int deviceRotation = getWindowManager().getDefaultDisplay().getRotation(); if(deviceRotation == Surface.ROTATION_90 || deviceRotation == Surface.ROTATION_270){ // Landscapeモードの場合のみサイズ調整. rctPreview.offset(centerX - rctPreview.centerX(), centerY – rctPreview.centerY()); matrix.setRectToRect(rctView, rctPreview, Matrix.ScaleToFit.FILL);
  2. 回転 // 縦または横の画面一杯に表示するためのScale値を取得. float scale = Math.max( (float) viewHeight /

    previewSize.getHeight() , (float) viewWidth / previewSize.getWidth() ); matrix.postScale(scale, scale, centerX, centerY); // ROTATION_90: 270度回転、ROTATION_270: 90度回転. matrix.postRotate((90 * (deviceRotation + 2)) % 360, centerX, centerY); } else{ // ROTATION_0: 0度回転、ROTATION_180: 180度回転. matrix.postRotate(90 * deviceRotation, centerX, centerY); } previewTextureView.setTransform(matrix); }); }
  3. MultiWindow対応 準備 AndroidManifest.xmlのapplicationタグに「resizeableActivity を 」 追加する。 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"

    android:resizeableActivity="true" android:supportsRtl="true" android:theme="@style/AppTheme"> ※この要素が無いとMultiWindowで開くときに 「App may not work with split-screen.」 とToast が表示される。
  4. MultiWindow対応  MultiWindowのOn / Off切替時、Windowサイズ の変更時はActivityが破棄される。  Windowのサイズによって表示上の画面の向きが 変わってしまうため、以下の値 (端末・画面の向

    き) が異なる場合がある。 // 例: 端末が縦向きならWindowサイズによらず0°となる. int displayOrientation = getWindowManager().getDefaultDisplay().getRotation(); // 例: 端末が縦向きでもWindowサイズによって // Landscape、Portraitの値が変わる. Configuration config = getResources().getConfiguration(); int configOrientation = config.orientation;
  5. 参考 android - How to detect screen rotation through 180

    degrees from landscape to landscape orientation? - Stack Overflow android.hardware.camera2 - Android Developers googlesamples - android-Camera2Basic https://developer.android.com/reference/android/hardware/ca mera2/package-summary.html http://stackoverflow.com/questions/9909037/how-to-detect- screen-rotation-through-180-degrees-from-landscape-to- landscape-or https://github.com/googlesamples/android-Camera2Basic
  6. 参考 android-MultiWindowPlayground - googlesamples - GitHub Android N で注意すること その3

    (Multi Window その1) - Qiita Multi-Window Support - Android Developers Android N で注意すること その4 (Multi Window その2) - Qiita https://developer.android.com/preview/features/multi- window.html https://github.com/googlesamples/android- MultiWindowPlayground http://qiita.com/hidenorly/items/456b4dd725875b776d9a http://qiita.com/hidenorly/items/72a659438126a8457715#_refer ence-7ccad824470f0f9c04fb
  7. Credit Google Noto Fonts  Main Page:  License: Material

    icons  Main Page:  License:  Main Page:  License: googlesamples - android-Camera2Basic https://www.google.com/get/noto/ SIL Open Font License (OFL) https://design.google.com/icons/ https://github.com/googlesamples/android- Camera2Basic CC-BY License Apache License 2.0
  8. Credit Copyright 2014 The Android Open Source Project, Inc. Licensed

    to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.