Skip to content

Commit f611771

Browse files
author
Albert Lo
committed
Merge remote-tracking branch 'gOrigin/master'
2 parents dc29818 + cd7405c commit f611771

File tree

11 files changed

+105
-115
lines changed

11 files changed

+105
-115
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ ext {
2727
compileSdkVersion = 25
2828
minSdkVersion = 9
2929
targetSdkVersion = 25
30-
supportLibraryVersion = '25.2.0'
30+
supportLibraryVersion = '25.3.0'
3131
}

demo/src/main/java/com/google/android/cameraview/demo/MainActivity.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import android.support.v7.app.AlertDialog;
3636
import android.support.v7.app.AppCompatActivity;
3737
import android.support.v7.widget.Toolbar;
38+
import android.util.DisplayMetrics;
3839
import android.util.Log;
3940
import android.view.Menu;
4041
import android.view.MenuItem;
4142
import android.view.View;
43+
import android.widget.RelativeLayout;
4244
import android.widget.Toast;
4345

4446
import com.google.android.cameraview.AspectRatio;
@@ -89,6 +91,10 @@ public class MainActivity extends AppCompatActivity implements
8991

9092
private Handler mBackgroundHandler;
9193

94+
private View mRootView;
95+
96+
private FloatingActionButton mCaptureFab;
97+
9298
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
9399
@Override
94100
public void onClick(View v) {
@@ -106,13 +112,14 @@ public void onClick(View v) {
106112
protected void onCreate(Bundle savedInstanceState) {
107113
super.onCreate(savedInstanceState);
108114
setContentView(R.layout.activity_main);
115+
mRootView = findViewById(R.id.root_view);
109116
mCameraView = (CameraView) findViewById(R.id.camera);
110117
if (mCameraView != null) {
111118
mCameraView.addCallback(mCallback);
112119
}
113-
FloatingActionButton takePicture = (FloatingActionButton) findViewById(R.id.take_picture);
114-
if (takePicture != null) {
115-
takePicture.setOnClickListener(mOnClickListener);
120+
mCaptureFab = (FloatingActionButton) findViewById(R.id.take_picture);
121+
if (mCaptureFab != null) {
122+
mCaptureFab.setOnClickListener(mOnClickListener);
116123
}
117124
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
118125
setSupportActionBar(toolbar);
@@ -214,11 +221,46 @@ public boolean onOptionsItemSelected(MenuItem item) {
214221
return false;
215222
}
216223

224+
private void repositionCaptureFab() {
225+
if ((mRootView == null) || (mCameraView == null) || (mCaptureFab == null)) {
226+
return;
227+
}
228+
mCaptureFab.post(new Runnable() {
229+
@Override
230+
public void run() {
231+
RelativeLayout.LayoutParams layoutParams =
232+
(RelativeLayout.LayoutParams) mCaptureFab.getLayoutParams();
233+
234+
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
235+
int minMargin = (int) (15 * displayMetrics.density + 0.5f);
236+
237+
if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
238+
int rightMargin = ((mRootView.getWidth() - mCameraView.getWidth()) / 2) -
239+
(mCaptureFab.getWidth() / 2);
240+
if (rightMargin < minMargin) {
241+
rightMargin = minMargin;
242+
}
243+
layoutParams.rightMargin = rightMargin;
244+
} else {
245+
int bottomMargin = ((mRootView.getHeight() - mCameraView.getHeight()) / 2) -
246+
(mCaptureFab.getHeight() / 2);
247+
if (bottomMargin < minMargin) {
248+
bottomMargin = minMargin;
249+
}
250+
layoutParams.bottomMargin = bottomMargin;
251+
}
252+
253+
mCaptureFab.setLayoutParams(layoutParams);
254+
}
255+
});
256+
}
257+
217258
@Override
218259
public void onAspectRatioSelected(@NonNull AspectRatio ratio) {
219260
if (mCameraView != null) {
220261
Toast.makeText(this, ratio.toString(), Toast.LENGTH_SHORT).show();
221262
mCameraView.setAspectRatio(ratio);
263+
repositionCaptureFab();
222264
}
223265
}
224266

@@ -237,6 +279,7 @@ private Handler getBackgroundHandler() {
237279
@Override
238280
public void onCameraOpened(CameraView cameraView) {
239281
Log.d(TAG, "onCameraOpened");
282+
repositionCaptureFab();
240283
}
241284

242285
@Override

demo/src/main/res/layout-land/activity_main.xml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@
1111
See the License for the specific language governing permissions and
1212
limitations under the License.
1313
-->
14-
<LinearLayout
14+
<RelativeLayout
1515
xmlns:android="http://schemas.android.com/apk/res/android"
1616
xmlns:tools="http://schemas.android.com/tools"
17+
xmlns:app="http://schemas.android.com/apk/res-auto"
18+
android:id="@+id/root_view"
1719
android:layout_width="match_parent"
1820
android:layout_height="match_parent"
1921
android:baselineAligned="false"
20-
android:orientation="horizontal"
22+
android:background="@color/primary"
2123
tools:context=".MainActivity">
2224

2325
<include
2426
layout="@layout/include_camera"
2527
android:layout_width="wrap_content"
2628
android:layout_height="match_parent"/>
2729

28-
<include
29-
layout="@layout/include_control"
30-
android:layout_width="0dp"
31-
android:layout_height="match_parent"
32-
android:layout_weight="1"/>
30+
<android.support.design.widget.FloatingActionButton
31+
android:id="@+id/take_picture"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:layout_centerVertical="true"
35+
android:layout_alignParentRight="true"
36+
app:srcCompat="@drawable/ic_camera"/>
3337

34-
</LinearLayout>
38+
</RelativeLayout>

demo/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@
1111
See the License for the specific language governing permissions and
1212
limitations under the License.
1313
-->
14-
<LinearLayout
14+
<RelativeLayout
1515
xmlns:android="http://schemas.android.com/apk/res/android"
1616
xmlns:tools="http://schemas.android.com/tools"
17+
xmlns:app="http://schemas.android.com/apk/res-auto"
18+
android:id="@+id/root_view"
1719
android:layout_width="match_parent"
1820
android:layout_height="match_parent"
19-
android:orientation="vertical"
21+
android:background="@color/primary"
2022
tools:context=".MainActivity">
2123

2224
<include
2325
layout="@layout/include_camera"
2426
android:layout_width="match_parent"
2527
android:layout_height="wrap_content"/>
2628

27-
<include
28-
layout="@layout/include_control"
29-
android:layout_width="match_parent"
30-
android:layout_height="0dp"
31-
android:layout_weight="1"/>
29+
<android.support.design.widget.FloatingActionButton
30+
android:id="@+id/take_picture"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_centerHorizontal="true"
34+
android:layout_alignParentBottom="true"
35+
app:srcCompat="@drawable/ic_camera"/>
3236

33-
</LinearLayout>
37+
</RelativeLayout>

demo/src/main/res/layout/include_camera.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<FrameLayout
1515
xmlns:android="http://schemas.android.com/apk/res/android"
1616
android:layout_width="match_parent"
17-
android:layout_height="match_parent">
17+
android:layout_height="match_parent"
18+
android:background="@android:color/black">
1819

1920
<com.google.android.cameraview.CameraView
2021
android:id="@+id/camera"

demo/src/main/res/layout/include_control.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

library/src/main/api14/com/google/android/cameraview/Camera1.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ int getFacing() {
150150

151151
@Override
152152
Set<AspectRatio> getSupportedAspectRatios() {
153-
return mPreviewSizes.ratios();
153+
SizeMap idealAspectRatios = mPreviewSizes;
154+
for (AspectRatio aspectRatio : idealAspectRatios.ratios()) {
155+
if (mPictureSizes.sizes(aspectRatio) == null) {
156+
idealAspectRatios.remove(aspectRatio);
157+
}
158+
}
159+
return idealAspectRatios.ratios();
154160
}
155161

156162
@Override

library/src/main/api21/com/google/android/cameraview/Camera2.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import java.util.Arrays;
4040
import java.util.Set;
4141
import java.util.SortedSet;
42-
import java.util.TreeSet;
4342

43+
@SuppressWarnings("MissingPermission")
4444
@TargetApi(21)
4545
class Camera2 extends CameraViewImpl {
4646

@@ -417,10 +417,19 @@ private void collectCameraInfo() {
417417
}
418418
mPreviewSizes.clear();
419419
for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) {
420-
mPreviewSizes.add(new Size(size.getWidth(), size.getHeight()));
420+
int width = size.getWidth();
421+
int height = size.getHeight();
422+
if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) {
423+
mPreviewSizes.add(new Size(width, height));
424+
}
421425
}
422426
mPictureSizes.clear();
423427
collectPictureSizes(mPictureSizes, map);
428+
for (AspectRatio ratio : mPreviewSizes.ratios()) {
429+
if (!mPictureSizes.ratios().contains(ratio)) {
430+
mPreviewSizes.remove(ratio);
431+
}
432+
}
424433

425434
if (!mPreviewSizes.ratios().contains(mAspectRatio)) {
426435
mAspectRatio = mPreviewSizes.ratios().iterator().next();
@@ -490,24 +499,16 @@ private Size chooseOptimalSize() {
490499
surfaceLonger = surfaceWidth;
491500
surfaceShorter = surfaceHeight;
492501
}
493-
SortedSet<Size> allCandidates = mPreviewSizes.sizes(mAspectRatio);
494-
495-
// Eliminate candidates that are bigger than Camera2 PREVIEW guarantees
496-
SortedSet<Size> guaranteedCandidates = new TreeSet<>();
497-
for (Size size: allCandidates) {
498-
if (size.getWidth() <= MAX_PREVIEW_WIDTH && size.getHeight() <= MAX_PREVIEW_HEIGHT) {
499-
guaranteedCandidates.add(size);
500-
}
501-
}
502+
SortedSet<Size> candidates = mPreviewSizes.sizes(mAspectRatio);
502503

503504
// Pick the smallest of those big enough
504-
for (Size size : guaranteedCandidates) {
505+
for (Size size : candidates) {
505506
if (size.getWidth() >= surfaceLonger && size.getHeight() >= surfaceShorter) {
506507
return size;
507508
}
508509
}
509510
// If no size is big enough, pick the largest one.
510-
return guaranteedCandidates.last();
511+
return candidates.last();
511512
}
512513

513514
/**

library/src/main/base/com/google/android/cameraview/SizeMap.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public boolean add(Size size) {
5454
return true;
5555
}
5656

57+
/**
58+
* Removes the specified aspect ratio and all sizes associated with it.
59+
*
60+
* @param ratio The aspect ratio to be removed.
61+
*/
62+
public void remove(AspectRatio ratio) {
63+
mRatios.remove(ratio);
64+
}
65+
5766
Set<AspectRatio> ratios() {
5867
return mRatios.keySet();
5968
}

library/src/main/java/com/google/android/cameraview/CameraView.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.support.annotation.Nullable;
2828
import android.support.v4.os.ParcelableCompat;
2929
import android.support.v4.os.ParcelableCompatCreatorCallbacks;
30+
import android.support.v4.view.ViewCompat;
3031
import android.util.AttributeSet;
3132
import android.widget.FrameLayout;
3233

@@ -141,7 +142,7 @@ private PreviewImpl createPreviewImpl(Context context) {
141142
protected void onAttachedToWindow() {
142143
super.onAttachedToWindow();
143144
if (!isInEditMode()) {
144-
mDisplayOrientationDetector.enable(ViewCompat2.getDisplay(this));
145+
mDisplayOrientationDetector.enable(ViewCompat.getDisplay(this));
145146
}
146147
}
147148

0 commit comments

Comments
 (0)