Skip to content

Commit

Permalink
Fix demo layout
Browse files Browse the repository at this point in the history
Change-Id: Ic8a206ada427889e63264659bf6f13c88033d14a
  • Loading branch information
yaraki committed Apr 13, 2017
1 parent c3f7e7d commit 0c87b4e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ ext {
compileSdkVersion = 25
minSdkVersion = 9
targetSdkVersion = 25
supportLibraryVersion = '25.3.0'
supportLibraryVersion = '25.3.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

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

private Handler mBackgroundHandler;

private View mRootView;

private FloatingActionButton mCaptureFab;

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -112,14 +106,13 @@ public void onClick(View v) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRootView = findViewById(R.id.root_view);
mCameraView = (CameraView) findViewById(R.id.camera);
if (mCameraView != null) {
mCameraView.addCallback(mCallback);
}
mCaptureFab = (FloatingActionButton) findViewById(R.id.take_picture);
if (mCaptureFab != null) {
mCaptureFab.setOnClickListener(mOnClickListener);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.take_picture);
if (fab != null) {
fab.setOnClickListener(mOnClickListener);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -221,46 +214,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
return false;
}

private void repositionCaptureFab() {
if ((mRootView == null) || (mCameraView == null) || (mCaptureFab == null)) {
return;
}
mCaptureFab.post(new Runnable() {
@Override
public void run() {
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) mCaptureFab.getLayoutParams();

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int minMargin = (int) (15 * displayMetrics.density + 0.5f);

if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
int rightMargin = ((mRootView.getWidth() - mCameraView.getWidth()) / 2) -
(mCaptureFab.getWidth() / 2);
if (rightMargin < minMargin) {
rightMargin = minMargin;
}
layoutParams.rightMargin = rightMargin;
} else {
int bottomMargin = ((mRootView.getHeight() - mCameraView.getHeight()) / 2) -
(mCaptureFab.getHeight() / 2);
if (bottomMargin < minMargin) {
bottomMargin = minMargin;
}
layoutParams.bottomMargin = bottomMargin;
}

mCaptureFab.setLayoutParams(layoutParams);
}
});
}

@Override
public void onAspectRatioSelected(@NonNull AspectRatio ratio) {
if (mCameraView != null) {
Toast.makeText(this, ratio.toString(), Toast.LENGTH_SHORT).show();
mCameraView.setAspectRatio(ratio);
repositionCaptureFab();
}
}

Expand All @@ -279,7 +237,6 @@ private Handler getBackgroundHandler() {
@Override
public void onCameraOpened(CameraView cameraView) {
Log.d(TAG, "onCameraOpened");
repositionCaptureFab();
}

@Override
Expand Down
31 changes: 23 additions & 8 deletions demo/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,41 @@
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:background="@color/primary"
tools:context=".MainActivity">

<include
layout="@layout/include_camera"
<com.google.android.cameraview.CameraView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="@android:color/black"/>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
android:layout_height="?attr/actionBarSize"
android:layout_alignEnd="@id/camera"
android:layout_alignParentTop="true"
android:layout_alignRight="@id/camera"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
app:srcCompat="@drawable/ic_camera"/>
android:layout_centerVertical="true"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_camera"
tools:ignore="RelativeOverlap"/>

</RelativeLayout>
26 changes: 20 additions & 6 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,39 @@
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
tools:context=".MainActivity">

<include
layout="@layout/include_camera"
<com.google.android.cameraview.CameraView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="@android:color/black"/>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_camera"/>

</RelativeLayout>
31 changes: 0 additions & 31 deletions demo/src/main/res/layout/include_camera.xml

This file was deleted.

0 comments on commit 0c87b4e

Please sign in to comment.