README.md
1
2Android ClippingBasic Sample
3===================================
4
5A basic app showing how to clip on a View using [ViewOutlineProvider][1] interface,
6by which a View builds its outline, used for shadowing and clipping.
7
8Introduction
9------------
10
11The [ViewOutlineProvider][1] interface offers you a method to populate the outline of a View.
12You need to implement a getOutline(android.view.View, android.graphics.Outline)
13method to clip a View in a specific shape.
14
15This example clips the outline of a View as a rounded rectangle by defining a class that
16 implements ViewOutlineProvider by following code:
17
18```java
19private class ClipOutlineProvider extends ViewOutlineProvider {
20 @Override
21 public void getOutline(View view, Outline outline) {
22 final int margin = Math.min(view.getWidth(), view.getHeight()) / 10;
23 outline.setRoundRect(margin, margin, view.getWidth() - margin,
24 view.getHeight() - margin, margin / 2);
25 }
26}
27```
28
29To clip a View by the defined outline, setting a OutlineProvider to a View
30to be clipped is needed like following:
31
32```java
33final View clippedView = view.findViewById(R.id.frame);
34clippedView.setOutlineProvider(mOutlineProvider);
35```
36
37You can toggle if the View is clipped by calling [setClipToOutline(boolean)][2]
38like following code:
39
40```java
41clippedView.setClipToOutline(true); // Setting false disable clipping
42```
43
44[1]: https://developer.android.com/reference/android/view/ViewOutlineProvider.html
45[2]: https://developer.android.com/reference/android/view/View.html#setClipToOutline(boolean)
46
47Pre-requisites
48--------------
49
50- Android SDK 28
51- Android Build Tools v28.0.3
52- Android Support Repository
53
54Screenshots
55-------------
56
57<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> <img src="screenshots/screenshot-2.png" height="400" alt="Screenshot"/>
58
59Getting Started
60---------------
61
62This sample uses the Gradle build system. To build this project, use the
63"gradlew build" command or use "Import Project" in Android Studio.
64
65Support
66-------
67
68- Google+ Community: https://plus.google.com/communities/105153134372062985968
69- Stack Overflow: http://stackoverflow.com/questions/tagged/android
70
71If you've found an error in this sample, please file an issue:
72https://github.com/googlesamples/android-ClippingBasic
73
74Patches are encouraged, and may be submitted by forking this project and
75submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
76
77License
78-------
79
80Copyright 2019 The Android Open Source Project, Inc.
81
82Licensed to the Apache Software Foundation (ASF) under one or more contributor
83license agreements. See the NOTICE file distributed with this work for
84additional information regarding copyright ownership. The ASF licenses this
85file to you under the Apache License, Version 2.0 (the "License"); you may not
86use this file except in compliance with the License. You may obtain a copy of
87the License at
88
89http://www.apache.org/licenses/LICENSE-2.0
90
91Unless required by applicable law or agreed to in writing, software
92distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
93WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
94License for the specific language governing permissions and limitations under
95the License.
96