forked from google/cameraview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the aspect ratio in the demo app
This also enhances the corresponding test. Change-Id: I5265b499823763845c1c0744d5fd04a44ae583b3
- Loading branch information
Showing
8 changed files
with
299 additions
and
26 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
demo/src/main/java/com/google/android/cameraview/demo/AspectRatioFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.android.cameraview.demo; | ||
|
||
import android.app.Dialog; | ||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.DialogFragment; | ||
import android.support.v7.app.AlertDialog; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.TextView; | ||
|
||
import com.google.android.cameraview.AspectRatio; | ||
|
||
import java.util.Set; | ||
|
||
|
||
/** | ||
* A simple dialog that allows user to pick an aspect ratio. | ||
*/ | ||
public class AspectRatioFragment extends DialogFragment { | ||
|
||
private static final String ARG_ASPECT_RATIOS = "aspect_ratios"; | ||
private static final String ARG_CURRENT_ASPECT_RATIO = "current_aspect_ratio"; | ||
|
||
private Listener mListener; | ||
|
||
public static AspectRatioFragment newInstance(Set<AspectRatio> ratios, | ||
AspectRatio currentRatio) { | ||
final AspectRatioFragment fragment = new AspectRatioFragment(); | ||
final Bundle args = new Bundle(); | ||
args.putParcelableArray(ARG_ASPECT_RATIOS, | ||
ratios.toArray(new AspectRatio[ratios.size()])); | ||
args.putParcelable(ARG_CURRENT_ASPECT_RATIO, currentRatio); | ||
fragment.setArguments(args); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public void onAttach(Context context) { | ||
super.onAttach(context); | ||
mListener = (Listener) context; | ||
} | ||
|
||
@Override | ||
public void onDetach() { | ||
mListener = null; | ||
super.onDetach(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
final Bundle args = getArguments(); | ||
final AspectRatio[] ratios = (AspectRatio[]) args.getParcelableArray(ARG_ASPECT_RATIOS); | ||
if (ratios == null) { | ||
throw new RuntimeException("No ratios"); | ||
} | ||
final AspectRatio current = args.getParcelable(ARG_CURRENT_ASPECT_RATIO); | ||
final AspectRatioAdapter adapter = new AspectRatioAdapter(ratios, current); | ||
return new AlertDialog.Builder(getActivity()) | ||
.setAdapter(adapter, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialog, int position) { | ||
mListener.onAspectRatioSelected(ratios[position]); | ||
} | ||
}) | ||
.create(); | ||
} | ||
|
||
private static class AspectRatioAdapter extends BaseAdapter { | ||
|
||
private final AspectRatio[] mRatios; | ||
private final AspectRatio mCurrentRatio; | ||
|
||
AspectRatioAdapter(AspectRatio[] ratios, AspectRatio current) { | ||
mRatios = ratios; | ||
mCurrentRatio = current; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return mRatios.length; | ||
} | ||
|
||
@Override | ||
public AspectRatio getItem(int position) { | ||
return mRatios[position]; | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return getItem(position).hashCode(); | ||
} | ||
|
||
@Override | ||
public View getView(int position, View view, ViewGroup parent) { | ||
AspectRatioAdapter.ViewHolder holder; | ||
if (view == null) { | ||
view = LayoutInflater.from(parent.getContext()) | ||
.inflate(android.R.layout.simple_list_item_1, parent, false); | ||
holder = new AspectRatioAdapter.ViewHolder(); | ||
holder.text = (TextView) view.findViewById(android.R.id.text1); | ||
view.setTag(holder); | ||
} else { | ||
holder = (AspectRatioAdapter.ViewHolder) view.getTag(); | ||
} | ||
AspectRatio ratio = getItem(position); | ||
StringBuilder sb = new StringBuilder(ratio.toString()); | ||
if (ratio.equals(mCurrentRatio)) { | ||
sb.append(" *"); | ||
} | ||
holder.text.setText(sb); | ||
return view; | ||
} | ||
|
||
private static class ViewHolder { | ||
TextView text; | ||
} | ||
|
||
} | ||
|
||
public interface Listener { | ||
void onAspectRatioSelected(@NonNull AspectRatio ratio); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!-- | ||
~ 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. | ||
--> | ||
|
||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FFFFFFFF" | ||
android:pathData="M18,4L6,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,6c0,-1.1 -0.9,-2 -2,-2zM18,18L6,18L6,6h12v12z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
library/src/androidTest/java/com/google/android/cameraview/CameraViewActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.android.cameraview; | ||
|
||
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.test.espresso.UiController; | ||
import android.support.test.espresso.ViewAction; | ||
import android.view.View; | ||
|
||
import org.hamcrest.Matcher; | ||
|
||
class CameraViewActions { | ||
|
||
static ViewAction setAspectRatio(@NonNull final AspectRatio ratio) { | ||
return new ViewAction() { | ||
|
||
@Override | ||
public Matcher<View> getConstraints() { | ||
return isAssignableFrom(CameraView.class); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Set aspect ratio to " + ratio; | ||
} | ||
|
||
@Override | ||
public void perform(UiController controller, View view) { | ||
((CameraView) view).setAspectRatio(ratio); | ||
} | ||
}; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
library/src/androidTest/java/com/google/android/cameraview/CameraViewMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.android.cameraview; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.view.View; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
class CameraViewMatchers { | ||
|
||
static Matcher<View> hasAspectRatio(@NonNull final AspectRatio ratio) { | ||
return new TypeSafeMatcher<View>() { | ||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("has aspect ratio of " + ratio); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(View view) { | ||
return ratio.equals(((CameraView) view).getAspectRatio()); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters