Skip to content

Commit

Permalink
Implement adjustViewBounds
Browse files Browse the repository at this point in the history
Change-Id: I0917adaa3deac4346039c3a4642e4923a6160990
  • Loading branch information
yaraki committed Apr 1, 2016
1 parent f97ea8b commit a4f9936
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 52 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-alpha4'
classpath 'com.android.tools.build:gradle:2.1.0-alpha5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.google.android.cameraview.CameraView;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

Expand All @@ -32,6 +34,10 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 16) {
// Hide the status bar
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
mCameraView = (CameraView) findViewById(R.id.camera);
if (mCameraView != null) {
mCameraView.addCallback(mCallback);
Expand Down
27 changes: 27 additions & 0 deletions demo/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed 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.
-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.cameraview.CameraView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"/>

</FrameLayout>
7 changes: 2 additions & 5 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<com.google.android.cameraview.CameraView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>

</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.support.test.runner.AndroidJUnit4;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -49,7 +50,11 @@
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.Assert.assertFalse;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -122,6 +127,64 @@ public void check(View view, NoMatchingViewException noViewFoundException) {
});
}

@Test
public void testAspectRatio() {
onView(withId(R.id.camera))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
AspectRatio ratio = cameraView.getAspectRatio();
assertThat(ratio, is(notNullValue()));
SizeMap map = cameraView.getSupportedPreviewSizes();
assertThat(map.ratios(), hasItem(ratio));
AspectRatio otherRatio = null;
for (AspectRatio r : map.ratios()) {
if (!r.equals(ratio)) {
otherRatio = r;
break;
}
}
if (otherRatio != null) {
cameraView.setAspectRatio(otherRatio);
assertThat(cameraView.getAspectRatio(), is(equalTo(otherRatio)));
}
}
});
}

@Test
public void testAdjustViewBounds() {
onView(withId(R.id.camera))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
assertThat(cameraView.getAdjustViewBounds(), is(false));
cameraView.setAdjustViewBounds(true);
assertThat(cameraView.getAdjustViewBounds(), is(true));
}
})
.perform(new AnythingAction("layout") {
@Override
public void perform(UiController uiController, View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
}
})
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
AspectRatio cameraRatio = cameraView.getAspectRatio();
AspectRatio viewRatio = new AspectRatio(view.getWidth(), view.getHeight());
assertThat(cameraRatio, is(either(equalTo(viewRatio))
.or(equalTo(viewRatio.inverse()))));
}
});
}

/**
* Wait for a camera to open.
*/
Expand Down Expand Up @@ -180,29 +243,39 @@ public void registerIdleTransitionCallback(ResourceCallback callback) {

}

private static class WaitAction implements ViewAction {
private static class WaitAction extends AnythingAction {

private final long mMs;

public WaitAction(long ms) {
super("wait");
mMs = ms;
}

@Override
public Matcher<View> getConstraints() {
return new IsAnything<>();
public void perform(UiController uiController, View view) {
SystemClock.sleep(mMs);
}

@Override
public String getDescription() {
return "wait";
}

private static abstract class AnythingAction implements ViewAction {

private final String mDescription;

public AnythingAction(String description) {
mDescription = description;
}

@Override
public void perform(UiController uiController, View view) {
SystemClock.sleep(mMs);
public Matcher<View> getConstraints() {
return new IsAnything<>();
}

@Override
public String getDescription() {
return mDescription;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public int compareTo(@NonNull AspectRatio another) {
return -1;
}

/**
* @return The inverse of this {@link AspectRatio}.
*/
public AspectRatio inverse() {
//noinspection SuspiciousNameCombination
return new AspectRatio(mY, mX);
}

private static int gcd(int a, int b) {
while (b != 0) {
int c = b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

abstract class CameraViewImpl {

protected final InternalCameraViewCallback mCallback;
protected final Callback mCallback;

public CameraViewImpl(InternalCameraViewCallback callback) {
public CameraViewImpl(Callback callback) {
mCallback = callback;
}

Expand All @@ -39,4 +39,16 @@ public CameraViewImpl(InternalCameraViewCallback callback) {
abstract SizeMap getSupportedPreviewSizes();

abstract boolean isCameraOpened();

abstract void setAspectRatio(AspectRatio ratio);

abstract AspectRatio getAspectRatio();

interface Callback {

void onCameraOpened();

void onCameraClosed();

}
}

This file was deleted.

Loading

0 comments on commit a4f9936

Please sign in to comment.