Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b2c660be authored by Teng-Hui Zhu's avatar Teng-Hui Zhu Committed by android-build-merger
Browse files

Merge "Add tileMode support into GradientColor" into nyc-dev

am: e4e4da5c

* commit 'e4e4da5c':
  Add tileMode support into GradientColor
parents 677a5696 e4e4da5c
Loading
Loading
Loading
Loading
+62 −4
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package android.content.res;
package android.content.res;


import android.annotation.ColorInt;
import android.annotation.ColorInt;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.res.Resources.Theme;
import android.content.res.Resources.Theme;
@@ -37,13 +38,48 @@ import android.util.Log;
import android.util.Xml;
import android.util.Xml;


import java.io.IOException;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;



/**
 * Lets you define a gradient color, which is used inside
 * {@link android.graphics.drawable.VectorDrawable}.
 *
 * {@link android.content.res.GradientColor}s are created from XML resource files defined in the
 * "color" subdirectory directory of an application's resource directory.  The XML file contains
 * a single "gradient" element with a number of attributes and elements inside.  For example:
 * <pre>
 * &lt;gradient xmlns:android="http://schemas.android.com/apk/res/android"&gt;
 *   &lt;android:startColor="?android:attr/colorPrimary"/&gt;
 *   &lt;android:endColor="?android:attr/colorControlActivated"/&gt;
 *   &lt;.../&gt;
 *   &lt;android:type="linear"/&gt;
 * &lt;/gradient&gt;
 * </pre>
 *
 * This can describe either a {@link android.graphics.LinearGradient},
 * {@link android.graphics.RadialGradient}, or {@link android.graphics.SweepGradient}.
 *
 * Note that different attributes are relevant for different types of gradient.
 * For example, android:gradientRadius is only applied to RadialGradient.
 * androd:centerX and android:centerY are only applied to SweepGradient or RadialGradient.
 * android:startX, android:startY, android:endX and android:endY are only applied to LinearGradient.
 *
 * Also note if any color "item" element is defined, then startColor, centerColor and endColor will
 * be ignored.
 */
public class GradientColor extends ComplexColor {
public class GradientColor extends ComplexColor {
    private static final String TAG = "GradientColor";
    private static final String TAG = "GradientColor";


    private static final boolean DBG_GRADIENT = false;
    private static final boolean DBG_GRADIENT = false;


    @IntDef({TILE_MODE_CLAMP, TILE_MODE_REPEAT, TILE_MODE_MIRROR})
    @Retention(RetentionPolicy.SOURCE)
    private @interface GradientTileMode {}
    private static final int TILE_MODE_CLAMP = 0;
    private static final int TILE_MODE_REPEAT = 1;
    private static final int TILE_MODE_MIRROR = 2;

    /** Lazily-created factory for this GradientColor. */
    /** Lazily-created factory for this GradientColor. */
    private GradientColorFactory mFactory;
    private GradientColorFactory mFactory;


@@ -54,7 +90,8 @@ public class GradientColor extends ComplexColor {
    // all the XML information.
    // all the XML information.
    private Shader mShader = null;
    private Shader mShader = null;


    // Below are the attributes at the root element <gradient>
    // Below are the attributes at the root element <gradient>.
    // NOTE: they need to be copied in the copy constructor!
    private int mGradientType = GradientDrawable.LINEAR_GRADIENT;
    private int mGradientType = GradientDrawable.LINEAR_GRADIENT;


    private float mCenterX = 0f;
    private float mCenterX = 0f;
@@ -70,6 +107,8 @@ public class GradientColor extends ComplexColor {
    private int mEndColor = 0;
    private int mEndColor = 0;
    private boolean mHasCenterColor = false;
    private boolean mHasCenterColor = false;


    private int mTileMode = 0; // Clamp mode.

    private float mGradientRadius = 0f;
    private float mGradientRadius = 0f;


    // Below are the attributes for the <item> element.
    // Below are the attributes for the <item> element.
@@ -100,6 +139,7 @@ public class GradientColor extends ComplexColor {
            mEndColor = copy.mEndColor;
            mEndColor = copy.mEndColor;
            mHasCenterColor = copy.mHasCenterColor;
            mHasCenterColor = copy.mHasCenterColor;
            mGradientRadius = copy.mGradientRadius;
            mGradientRadius = copy.mGradientRadius;
            mTileMode = copy.mTileMode;


            if (copy.mItemColors != null) {
            if (copy.mItemColors != null) {
                mItemColors = copy.mItemColors.clone();
                mItemColors = copy.mItemColors.clone();
@@ -117,6 +157,20 @@ public class GradientColor extends ComplexColor {
        }
        }
    }
    }


    // Set the default to clamp mode.
    private static Shader.TileMode parseTileMode(@GradientTileMode int tileMode) {
        switch (tileMode) {
            case TILE_MODE_CLAMP:
                return Shader.TileMode.CLAMP;
            case TILE_MODE_REPEAT:
                return Shader.TileMode.REPEAT;
            case TILE_MODE_MIRROR:
                return Shader.TileMode.MIRROR;
            default:
                return Shader.TileMode.CLAMP;
        }
    }

    /**
    /**
     * Update the root level's attributes, either for inflate or applyTheme.
     * Update the root level's attributes, either for inflate or applyTheme.
     */
     */
@@ -150,6 +204,9 @@ public class GradientColor extends ComplexColor {
        mEndColor = a.getColor(
        mEndColor = a.getColor(
                R.styleable.GradientColor_endColor, mEndColor);
                R.styleable.GradientColor_endColor, mEndColor);


        mTileMode = a.getInt(
                R.styleable.GradientColor_tileMode, mTileMode);

        if (DBG_GRADIENT) {
        if (DBG_GRADIENT) {
            Log.v(TAG, "hasCenterColor is " + mHasCenterColor);
            Log.v(TAG, "hasCenterColor is " + mHasCenterColor);
            if (mHasCenterColor) {
            if (mHasCenterColor) {
@@ -157,6 +214,7 @@ public class GradientColor extends ComplexColor {
            }
            }
            Log.v(TAG, "startColor: " + mStartColor);
            Log.v(TAG, "startColor: " + mStartColor);
            Log.v(TAG, "endColor: " + mEndColor);
            Log.v(TAG, "endColor: " + mEndColor);
            Log.v(TAG, "tileMode: " + mTileMode);
        }
        }


        mGradientRadius = a.getFloat(R.styleable.GradientColor_gradientRadius,
        mGradientRadius = a.getFloat(R.styleable.GradientColor_gradientRadius,
@@ -406,11 +464,11 @@ public class GradientColor extends ComplexColor {


        if (mGradientType == GradientDrawable.LINEAR_GRADIENT) {
        if (mGradientType == GradientDrawable.LINEAR_GRADIENT) {
            mShader = new LinearGradient(mStartX, mStartY, mEndX, mEndY, tempColors, tempOffsets,
            mShader = new LinearGradient(mStartX, mStartY, mEndX, mEndY, tempColors, tempOffsets,
                    Shader.TileMode.CLAMP);
                    parseTileMode(mTileMode));
        } else {
        } else {
            if (mGradientType == GradientDrawable.RADIAL_GRADIENT) {
            if (mGradientType == GradientDrawable.RADIAL_GRADIENT) {
                mShader = new RadialGradient(mCenterX, mCenterY, mGradientRadius, tempColors,
                mShader = new RadialGradient(mCenterX, mCenterY, mGradientRadius, tempColors,
                        tempOffsets, Shader.TileMode.CLAMP);
                        tempOffsets, parseTileMode(mTileMode));
            } else {
            } else {
                mShader = new SweepGradient(mCenterX, mCenterY, tempColors, tempOffsets);
                mShader = new SweepGradient(mCenterX, mCenterY, tempColors, tempOffsets);
            }
            }
+2 −0
Original line number Original line Diff line number Diff line
@@ -8170,6 +8170,8 @@ i
             Defined in same coordinates as the path itself -->
             Defined in same coordinates as the path itself -->
        <attr name="endY" format="float" />
        <attr name="endY" format="float" />


        <!-- Defines the tile mode of the gradient. SweepGradient don't support tiling. -->
        <attr name="tileMode"/>
    </declare-styleable>
    </declare-styleable>


    <!-- Describes an item of a GradientColor. Minimally need 2 items to define the gradient
    <!-- Describes an item of a GradientColor. Minimally need 2 items to define the gradient
+30 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
 * 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.
 */
-->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
          android:angle="90"
          android:startColor="?android:attr/colorPrimary"
          android:endColor="?android:attr/colorControlActivated"
          android:centerColor="#00ff0000"
          android:startX="0"
          android:startY="0"
          android:endX="50"
          android:endY="50"
          android:type="linear"
          android:tileMode="clamp">
</gradient>
 No newline at end of file
+34 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
 * 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.
 */
-->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
          android:angle="90"
          android:startColor="?android:attr/colorPrimary"
          android:endColor="?android:attr/colorControlActivated"
          android:centerColor="#f00"
          android:startX="0"
          android:startY="0"
          android:endX="50"
          android:endY="50"
          android:type="linear"
          android:tileMode="mirror">
    <item android:offset="0.1" android:color="?android:attr/colorPrimary"/>
    <item android:offset="0.4" android:color="#f00"/>
    <item android:offset="0.4" android:color="#fff"/>
    <item android:offset="0.9" android:color="?android:attr/colorControlActivated"/>
</gradient>
 No newline at end of file
+33 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
 * 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.
 */
-->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
          android:angle="90"
          android:startColor="?android:attr/colorPrimary"
          android:endColor="?android:attr/colorControlActivated"
          android:centerColor="#f00"
          android:startX="0"
          android:startY="0"
          android:endX="50"
          android:endY="50"
          android:type="linear"
          android:tileMode="repeat">
    <item android:offset="0.1" android:color="?android:attr/colorPrimary"/>
    <item android:offset="0.4" android:color="#fff"/>
    <item android:offset="0.9" android:color="?android:attr/colorControlActivated"/>
</gradient>
 No newline at end of file
Loading