afohrman 98d4b148d1 Create deep copies of CornerTreatments and EdgeTreatments in ShapeAppearanceModel's copy constructor.
Implement Cloneable and override Object#clone() to create deep copies of the corner and edge treatments. The Cloneable method is generally discouraged, but this solution appears to be the most effective solution for our use case because:

- CornerTreatment and EdgeTreatment don't contain mutable fields, so using the native Object#clone() should be fine. This allows us to avoid intervening in the clone() method, which is one of the main reasons to avoid using Obect#clone().
- Since we have to maintain binary compatibility with our 1.0.0 stable release, we have very little wiggle room with which to ensure that we have effective copy() methods in each subclass of CornerTreatment and EdgeTreatment.

PiperOrigin-RevId: 222311453
2018-12-06 11:17:48 -05:00

53 lines
2.0 KiB
Java

/*
* Copyright 2017 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.material.shape;
import com.google.android.material.internal.Experimental;
/**
* An edge treatment which draws triangles at the midpoint of an edge, facing into or out of the
* shape.
*/
@Experimental("The shapes API is currently experimental and subject to change")
public class TriangleEdgeTreatment extends EdgeTreatment implements Cloneable {
private final float size;
private final boolean inside;
/**
* Instantiates a triangle treatment of the given size, which faces inward or outward relative to
* the shape.
*
* @param size the length in pixels that the triangle extends into or out of the shape. The length
* of the side of the triangle coincident with the rest of the edge is 2 * size.
* @param inside true if the triangle should be "cut out" of the shape (i.e. inward-facing); false
* if the triangle should extend out of the shape.
*/
public TriangleEdgeTreatment(float size, boolean inside) {
this.size = size;
this.inside = inside;
}
@Override
public void getEdgePath(float length, float center, float interpolation, ShapePath shapePath) {
shapePath.lineTo(center - (size * interpolation), 0);
shapePath.lineTo(center, inside ? size * interpolation : -size * interpolation);
shapePath.lineTo(center + (size * interpolation), 0);
shapePath.lineTo(length, 0);
}
}