Merge "Update ClippingBasic sample for L" into lmp-dev
diff --git a/ui/views/Clipping/ClippingBasic/Application/README-fragmentview.txt b/ui/views/Clipping/ClippingBasic/Application/README-fragmentview.txt
new file mode 100644
index 0000000..38d903f
--- /dev/null
+++ b/ui/views/Clipping/ClippingBasic/Application/README-fragmentview.txt
@@ -0,0 +1,37 @@
+<!--
+ Copyright 2013 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.
+-->
+
+Steps to implement FragmentView template:
+-in template-params.xml.ftl:
+ -add the following line to common imports
+ <common src="activities"/>
+
+-Add a Fragment to show behavior. In your MainActivity.java class, it will reference a Fragment
+ called (yourProjectName)Fragment.java. Create that file in your project, using the "main" source
+ folder instead of "common" or "templates".
+ For instance, if your package name is com.example.foo, create the file
+ src/main/java/com/example/foo/FooFragment.java
+
+
+-Within this fragment, make sure that the onCreate method has the line
+ "setHasOptionsMenu(true);", to enable the fragment to handle menu events.
+
+-In order to override menu events, override onOptionsItemSelected.
+
+-refer to sampleSamples/fragmentViewSample for a reference implementation of a
+project built on this template.
+
+
diff --git a/ui/views/Clipping/ClippingBasic/Application/src/main/java/com/example/android/clippingbasic/ClippingBasicFragment.java b/ui/views/Clipping/ClippingBasic/Application/src/main/java/com/example/android/clippingbasic/ClippingBasicFragment.java
index ef14be1..295a474 100644
--- a/ui/views/Clipping/ClippingBasic/Application/src/main/java/com/example/android/clippingbasic/ClippingBasicFragment.java
+++ b/ui/views/Clipping/ClippingBasic/Application/src/main/java/com/example/android/clippingbasic/ClippingBasicFragment.java
@@ -23,6 +23,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.view.ViewOutlineProvider;
import android.widget.Button;
import android.widget.TextView;
@@ -37,7 +38,7 @@
private int mClickCount = 0;
/* The {@Link Outline} used to clip the image with. */
- private Outline mClip;
+ private ViewOutlineProvider mOutlineProvider;
/* An array of texts. */
private String[] mSampleTexts;
@@ -49,7 +50,7 @@
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
- mClip = new Outline();
+ mOutlineProvider = new ClipOutlineProvider();
mSampleTexts = getResources().getStringArray(R.array.sample_texts);
}
@@ -67,35 +68,28 @@
mTextView = (TextView) view.findViewById(R.id.text_view);
changeText();
+
+ final View clippedView = view.findViewById(R.id.frame);
+
+ /* Sets the OutlineProvider for the View. */
+ clippedView.setOutlineProvider(mOutlineProvider);
+
/* When the button is clicked, the text is clipped or un-clipped. */
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View bt) {
-
- View clippedView = view.findViewById(R.id.frame);
-
- /* The view is already clipped if {Link View#getClipToOutline()} returns true. */
+ // Toggle whether the View is clipped to the outline
if (clippedView.getClipToOutline()) {
/* The Outline is set for the View, but disable clipping. */
clippedView.setClipToOutline(false);
- Log.d(TAG, String.format("Clipping was removed."));
+ Log.d(TAG, String.format("Clipping to outline is disabled"));
((Button) bt).setText(R.string.clip_button);
-
} else {
- /*
- Sets the dimensions and shape of the {@Link Outline}. A rounded rectangle
- with a margin determined by the width or height.
- */
- int margin = Math.min(clippedView.getWidth(), clippedView.getHeight()) / 10;
- mClip.setRoundRect(margin, margin, clippedView.getWidth() - margin,
- clippedView.getHeight() - margin, margin / 2);
- /* Sets the Outline of the View. */
- clippedView.setOutline(mClip);
/* Enables clipping on the View. */
clippedView.setClipToOutline(true);
- Log.d(TAG, String.format("View was clipped."));
+ Log.d(TAG, String.format("Clipping to outline is enabled"));
((Button) bt).setText(R.string.unclip_button);
}
}
@@ -106,20 +100,40 @@
@Override
public void onClick(View view) {
mClickCount++;
+
+ // Update the text in the TextView
changeText();
+
+ // Invalidate the outline just in case the TextView changed size
+ clippedView.invalidateOutline();
}
});
}
private void changeText() {
- /*
- Compute the position of the string in the array using the number of strings
- and the number of clicks.
- */
+ // Compute the position of the string in the array using the number of strings
+ // and the number of clicks.
String newText = mSampleTexts[mClickCount % mSampleTexts.length];
/* Once the text is selected, change the TextView */
mTextView.setText(newText);
Log.d(TAG, String.format("Text was changed."));
+
+
+ }
+
+ /**
+ * A {@link ViewOutlineProvider} which clips the view with a rounded rectangle which is inset
+ * by 10%
+ */
+ private class ClipOutlineProvider extends ViewOutlineProvider {
+
+ @Override
+ public void getOutline(View view, Outline outline) {
+ final int margin = Math.min(view.getWidth(), view.getHeight()) / 10;
+ outline.setRoundRect(margin, margin, view.getWidth() - margin,
+ view.getHeight() - margin, margin / 2);
+ }
+
}
}
\ No newline at end of file
diff --git a/ui/views/Clipping/ClippingBasic/Application/src/main/res/values/strings.xml b/ui/views/Clipping/ClippingBasic/Application/src/main/res/values/strings.xml
index e9cc40d..b01a168 100644
--- a/ui/views/Clipping/ClippingBasic/Application/src/main/res/values/strings.xml
+++ b/ui/views/Clipping/ClippingBasic/Application/src/main/res/values/strings.xml
@@ -21,6 +21,6 @@
<item>De finibus bonorum et malorum.</item>
</string-array>
- <string name="clip_button">Clip</string>
- <string name="unclip_button">Remove Clipping</string>
+ <string name="clip_button">Enabled outline clipping</string>
+ <string name="unclip_button">Disable outline clipping</string>
</resources>
\ No newline at end of file