1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Copyright 2013 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16--> 17 18 19 20<sample> 21 <name>ClippingBasic</name> 22 <group>UI</group> 23 <package>com.example.android.clippingbasic</package> 24 25 26 <minSdk>21</minSdk> 27 <compileSdkVersion>21</compileSdkVersion> 28 29 30 <strings> 31 <intro> 32 <![CDATA[ 33 Basic sample to demonstrate clipping on a View. 34 ]]> 35 </intro> 36 </strings> 37 38 <template src="base"/> 39 <template src="FragmentView"/> 40 <common src="logger"/> 41 <common src="activities"/> 42 43 <metadata> 44 <status>PUBLISHED</status> 45 <categories>UI Views</categories> 46 <technologies>Android</technologies> 47 <languages>Java</languages> 48 <solutions>Mobile</solutions> 49 <level>BEGINNER</level> 50 <icon>screenshots/web-icon.png</icon> 51 <screenshots> 52 <img>screenshots/screenshot-1.png</img> 53 <img>screenshots/screenshot-2.png</img> 54 </screenshots> 55 <api_refs> 56 <android>android.view.ViewOutlineProvider</android> 57 </api_refs> 58 59 <description> 60<![CDATA[ 61A basic app showing how to clip on a View using [ViewOutlineProvider][1] interface, 62by which a View builds its outline, used for shadowing and clipping. 63]]> 64 </description> 65 66 <intro> 67<![CDATA[ 68The [ViewOutlineProvider][1] interface offers you a method to populate the outline of a View. 69You need to implement a getOutline(android.view.View, android.graphics.Outline) 70method to clip a View in a specific shape. 71 72This example clips the outline of a View as a rounded rectangle by defining a class that 73 implements ViewOutlineProvider by following code: 74 75```java 76private class ClipOutlineProvider extends ViewOutlineProvider { 77 @Override 78 public void getOutline(View view, Outline outline) { 79 final int margin = Math.min(view.getWidth(), view.getHeight()) / 10; 80 outline.setRoundRect(margin, margin, view.getWidth() - margin, 81 view.getHeight() - margin, margin / 2); 82 } 83} 84``` 85 86To clip a View by the defined outline, setting a OutlineProvider to a View 87to be clipped is needed like following: 88 89```java 90final View clippedView = view.findViewById(R.id.frame); 91clippedView.setOutlineProvider(mOutlineProvider); 92``` 93 94You can toggle if the View is clipped by calling [setClipToOutline(boolean)][2] 95like following code: 96 97```java 98clippedView.setClipToOutline(true); // Setting false disable clipping 99``` 100 101[1]: https://developer.android.com/reference/android/view/ViewOutlineProvider.html 102[2]: https://developer.android.com/reference/android/view/View.html#setClipToOutline(boolean) 103]]> 104 </intro> 105 </metadata> 106</sample> 107