mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Create base Chip style.
PiperOrigin-RevId: 173970927
This commit is contained in:
parent
09e51fcda5
commit
237682de5b
49
lib/res/animator/mtrl_chip_state_list_anim.xml
Normal file
49
lib/res/animator/mtrl_chip_state_list_anim.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- Pressed state -->
|
||||
<item
|
||||
android:state_enabled="true"
|
||||
android:state_pressed="true">
|
||||
<objectAnimator
|
||||
android:duration="@integer/mtrl_chip_anim_duration"
|
||||
android:propertyName="translationZ"
|
||||
android:valueTo="@dimen/mtrl_chip_pressed_translation_z"
|
||||
android:valueType="floatType"/>
|
||||
</item>
|
||||
|
||||
<!-- Enabled state -->
|
||||
<item android:state_enabled="true">
|
||||
<objectAnimator
|
||||
android:duration="@integer/mtrl_chip_anim_duration"
|
||||
android:propertyName="translationZ"
|
||||
android:valueTo="0"
|
||||
android:valueType="floatType"/>
|
||||
</item>
|
||||
|
||||
<!-- Disabled state -->
|
||||
<item>
|
||||
<objectAnimator
|
||||
android:duration="0"
|
||||
android:propertyName="translationZ"
|
||||
android:valueTo="0"
|
||||
android:valueType="floatType"/>
|
||||
</item>
|
||||
|
||||
</selector>
|
||||
24
lib/res/color/mtrl_chip_background_color.xml
Normal file
24
lib/res/color/mtrl_chip_background_color.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item android:color="#D7D7D7" android:state_checked="true" android:state_enabled="true"/>
|
||||
<item android:color="#E8E8E8" android:state_enabled="true"/>
|
||||
<item android:color="#F2F2F2"/>
|
||||
|
||||
</selector>
|
||||
27
lib/res/color/mtrl_chip_ripple_alpha.xml
Normal file
27
lib/res/color/mtrl_chip_ripple_alpha.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- 16% alpha. -->
|
||||
<item android:color="#29000000" android:state_enabled="true" android:state_pressed="true"/>
|
||||
<!-- 12% alpha. -->
|
||||
<item android:color="#1F000000" android:state_enabled="true" android:state_focused="true"/>
|
||||
<!-- 4% alpha. -->
|
||||
<item android:color="#0A000000" android:state_enabled="true" android:state_hovered="true"/>
|
||||
|
||||
</selector>
|
||||
34
lib/res/drawable/ic_mtrl_chip_checked_circle.xml
Normal file
34
lib/res/drawable/ic_mtrl_chip_checked_circle.xml
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:height="24dp"
|
||||
android:viewportHeight="24.0"
|
||||
android:viewportWidth="24.0"
|
||||
android:width="24dp">
|
||||
<path
|
||||
android:fillAlpha="0.5"
|
||||
android:fillColor="#191919"
|
||||
android:pathData="M12,12m-12,0a12,12 0,1 1,24 0a12,12 0,1 1,-24 0"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1"/>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M9.189,15.939l-3.127,-3.128l-1.061,1.061l4.189,4.189l9,-9l-1.061,-1.061z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1"/>
|
||||
</vector>
|
||||
29
lib/res/drawable/ic_mtrl_chip_close_circle.xml
Normal file
29
lib/res/drawable/ic_mtrl_chip_close_circle.xml
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:height="18dp"
|
||||
android:viewportHeight="18"
|
||||
android:viewportWidth="18"
|
||||
android:width="18dp">
|
||||
<path
|
||||
android:fillAlpha="0.539999962"
|
||||
android:fillColor="#000000"
|
||||
android:pathData="M13,11.87 L11.87,13 L9,10.13 L6.13,13 L5,11.87 L7.87,9 L5,6.13 L6.13,5 L9,7.87 L11.87,5 L13,6.13 L10.13,9 L13,11.87 Z M9,1 C4.58,1,1,4.58,1,9 C1,13.42,4.58,17,9,17 C13.42,17,17,13.42,17,9 C17,4.58,13.42,1,9,1 L9,1 Z"
|
||||
android:strokeAlpha="0.539999962"
|
||||
android:strokeWidth="1"/>
|
||||
</vector>
|
||||
@ -61,6 +61,9 @@
|
||||
<!-- 0% opacity white container fill -->
|
||||
<color name="mtrl_btn_transparent_bg_color">#00ffffff</color>
|
||||
|
||||
<color name="mtrl_chip_ripple_color">#191919</color>
|
||||
<color name="mtrl_chip_text_color">#DE000000</color>
|
||||
|
||||
<!-- 32% opacity black -->
|
||||
<color name="mtrl_scrim_color">#52000000</color>
|
||||
</resources>
|
||||
|
||||
@ -94,4 +94,7 @@
|
||||
<dimen name="mtrl_fab_translation_z_hovered_focused">2dp</dimen>
|
||||
<dimen name="mtrl_fab_translation_z_pressed">6dp</dimen>
|
||||
|
||||
<dimen name="mtrl_chip_pressed_translation_z">3dp</dimen>
|
||||
<dimen name="mtrl_chip_text_size">14sp</dimen>
|
||||
|
||||
</resources>
|
||||
|
||||
@ -17,4 +17,5 @@
|
||||
<resources>
|
||||
<integer name="bottom_sheet_slide_duration">150</integer>
|
||||
<integer name="app_bar_elevation_anim_duration">150</integer>
|
||||
<integer name="mtrl_chip_anim_duration">100</integer>
|
||||
</resources>
|
||||
|
||||
@ -220,25 +220,27 @@
|
||||
<item name="android:focusable">true</item>
|
||||
<item name="android:clickable">true</item>
|
||||
<item name="android:checkable">false</item>
|
||||
<item name="android:stateListAnimator">@animator/mtrl_chip_state_list_anim</item>
|
||||
|
||||
<item name="chipIcon">@null</item>
|
||||
<item name="checkedIcon">@drawable/ic_mtrl_chip_checked_circle</item>
|
||||
<item name="closeIcon">@drawable/ic_mtrl_chip_close_circle</item>
|
||||
|
||||
<item name="chipText">@null</item>
|
||||
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Chip</item>
|
||||
<item name="closeIconTint">@null</item>
|
||||
|
||||
<item name="chipBackgroundColor">@color/mtrl_chip_background_color</item>
|
||||
<item name="chipStrokeColor">#00000000</item>
|
||||
<item name="chipStrokeWidth">0dp</item>
|
||||
<item name="rippleColor">@color/mtrl_chip_ripple_color</item>
|
||||
<item name="rippleAlpha">@color/mtrl_chip_ripple_alpha</item>
|
||||
|
||||
<!-- TODO: All these values are fake. They only exist to test the plumbing. -->
|
||||
<item name="chipBackgroundColor">#00ff00</item>
|
||||
<item name="chipMinHeight">32dp</item>
|
||||
<item name="chipCornerRadius">16dp</item>
|
||||
<item name="chipStrokeColor">#0000ff</item>
|
||||
<item name="chipStrokeWidth">1dp</item>
|
||||
<item name="rippleColor">#ffffff</item>
|
||||
<item name="rippleAlpha">#aa000000</item>
|
||||
<item name="chipText">Hello, world!</item>
|
||||
<item name="android:textAppearance">@style/TextAppearance.Design.Error</item>
|
||||
<item name="chipIcon">@drawable/abc_ic_ab_back_material</item>
|
||||
<item name="chipIconSize">24dp</item>
|
||||
<item name="closeIcon">@drawable/abc_ic_go_search_api_material</item>
|
||||
<item name="closeIconTint">#ff000000</item>
|
||||
<item name="closeIconSize">18dp</item>
|
||||
<item name="checkedIcon">@drawable/abc_ic_arrow_drop_right_black_24dp</item>
|
||||
<item name="showMotionSpec">@animator/design_fab_show_motion_spec</item>
|
||||
<item name="hideMotionSpec">@animator/design_fab_hide_motion_spec</item>
|
||||
|
||||
<item name="chipStartPadding">4dp</item>
|
||||
<item name="iconStartPadding">0dp</item>
|
||||
<item name="iconEndPadding">0dp</item>
|
||||
@ -247,6 +249,11 @@
|
||||
<item name="closeIconStartPadding">2dp</item>
|
||||
<item name="closeIconEndPadding">2dp</item>
|
||||
<item name="chipEndPadding">6dp</item>
|
||||
|
||||
<!-- TODO: The following values are fake. They only exist to test the plumbing. -->
|
||||
<item name="showMotionSpec">@animator/design_fab_show_motion_spec</item>
|
||||
<item name="hideMotionSpec">@animator/design_fab_hide_motion_spec</item>
|
||||
|
||||
</style>
|
||||
|
||||
<style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton">
|
||||
@ -259,4 +266,9 @@
|
||||
<item name="hideMotionSpec">@animator/mtrl_fab_hide_motion_spec</item>
|
||||
</style>
|
||||
|
||||
<style name="TextAppearance.MaterialComponents.Chip" parent="android:TextAppearance">
|
||||
<item name="android:textColor">@color/mtrl_chip_text_color</item>
|
||||
<item name="android:textSize">@dimen/mtrl_chip_text_size</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
|
||||
@ -69,13 +69,26 @@ import java.util.List;
|
||||
* accessibility navigation. The main chip and close icon are considered to be separate logical
|
||||
* sub-views, and contain their own navigation behavior and state.
|
||||
*
|
||||
* <p>All attributes from {@link R.styleable#ChipDrawable} are supported. Do not use the {@code
|
||||
* android:button} and {@code android:text} attributes. They will be ignored because Chip manages
|
||||
* its own button Drawable and text. The basic attributes you can set are:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link android.R.attr#checkable android:checkable} - If true, the chip can be toggled. If
|
||||
* false, the chip acts like a button.
|
||||
* <li>{@link R.attr#chipText app:chipText} - Sets the text of the chip.
|
||||
* <li>{@link R.attr#chipIcon app:chipIcon} - Sets the icon of the chip, or use @null to display
|
||||
* no icon. Usually on the left.
|
||||
* <li>{@link R.attr#checkedIcon app:checkedIcon} - Sets a custom icon to use when checked, or
|
||||
* use @null to display no icon. Usually on the left.
|
||||
* <li>{@link R.attr#closeIcon app:closeIcon} - Sets a custom icon that the user can click to
|
||||
* close, or use @null to display no icon. Usually on the right.
|
||||
* </ul>
|
||||
*
|
||||
* <p>You can register a listener on the main chip with {@link #setOnClickListener(OnClickListener)}
|
||||
* or {@link #setOnCheckedChangeListener(OnCheckedChangeListener)}. You can register a listener on
|
||||
* the close icon with {@link #setOnCloseIconClickListener(OnClickListener)}.
|
||||
*
|
||||
* <p>All attributes from {@link R.styleable#ChipDrawable} are supported. Do not use the {@code
|
||||
* android:button} attribute. It will be ignored because Chip manages its own button Drawable.
|
||||
*
|
||||
* @see ChipDrawable
|
||||
*/
|
||||
public class Chip extends AppCompatCheckBox implements Delegate {
|
||||
|
||||
@ -22,6 +22,7 @@ import android.content.res.Resources;
|
||||
import android.content.res.Resources.NotFoundException;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Outline;
|
||||
import android.graphics.Paint;
|
||||
@ -54,6 +55,7 @@ import android.support.design.drawable.DrawableUtils;
|
||||
import android.support.design.resources.MaterialResources;
|
||||
import android.support.design.ripple.RippleUtils;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.graphics.ColorUtils;
|
||||
import android.support.v4.graphics.drawable.DrawableCompat;
|
||||
import android.support.v4.graphics.drawable.TintAwareDrawable;
|
||||
import android.support.v4.text.BidiFormatter;
|
||||
@ -75,8 +77,30 @@ import org.xmlpull.v1.XmlPullParserException;
|
||||
*
|
||||
* <p>You can use ChipDrawable directly in contexts that require a Drawable. For example, an
|
||||
* auto-complete enabled EditText can replace snippets of text with a ChipDrawable to represent it
|
||||
* as a semantic entity. When used in this stand-alone mode, the host view must explicitly manage
|
||||
* the ChipDrawable's state:
|
||||
* as a semantic entity. To create an instance of ChipDrawable, use {@link
|
||||
* ChipDrawable#createFromResource(Context, int)} and pass in an XML resource in this form:
|
||||
*
|
||||
* <pre>{@code
|
||||
* <chip xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
* app:chipText="Hello, World!"/>
|
||||
* }</pre>
|
||||
*
|
||||
* <p>The basic attributes you can set are:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link android.R.attr#checkable android:checkable} - If true, the chip can be toggled. If
|
||||
* false, the chip acts like a button.
|
||||
* <li>{@link R.attr#chipText app:chipText} - Sets the text of the chip.
|
||||
* <li>{@link R.attr#chipIcon app:chipIcon} - Sets the icon of the chip, or use @null to display
|
||||
* no icon. Usually on the left.
|
||||
* <li>{@link R.attr#checkedIcon app:checkedIcon} - Sets a custom icon to use when checked, or
|
||||
* use @null to display no icon. Usually on the left.
|
||||
* <li>{@link R.attr#closeIcon app:closeIcon} - Sets a custom icon that the user can click to
|
||||
* close, or use @null to display no icon. Usually on the right.
|
||||
* </ul>
|
||||
*
|
||||
* <p>When used in this stand-alone mode, the host view must explicitly manage the ChipDrawable's
|
||||
* state:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link ChipDrawable#setBounds(int, int, int, int)}, taking into account {@link
|
||||
@ -275,6 +299,9 @@ public class ChipDrawable extends Drawable implements TintAwareDrawable, Callbac
|
||||
|
||||
textPaint.density = context.getResources().getDisplayMetrics().density;
|
||||
debugPaint = DEBUG ? new Paint(Paint.ANTI_ALIAS_FLAG) : null;
|
||||
if (debugPaint != null) {
|
||||
debugPaint.setStyle(Style.STROKE);
|
||||
}
|
||||
|
||||
setState(DEFAULT_STATE);
|
||||
setCloseIconState(DEFAULT_STATE);
|
||||
@ -595,9 +622,39 @@ public class ChipDrawable extends Drawable implements TintAwareDrawable, Callbac
|
||||
}
|
||||
|
||||
private void drawDebug(@NonNull Canvas canvas, Rect bounds) {
|
||||
if (DEBUG) {
|
||||
canvas.drawLine(
|
||||
bounds.left, bounds.exactCenterY(), bounds.right, bounds.exactCenterY(), debugPaint);
|
||||
if (debugPaint != null) {
|
||||
debugPaint.setColor(ColorUtils.setAlphaComponent(Color.BLACK, 255 / 2));
|
||||
|
||||
// Background.
|
||||
canvas.drawRect(bounds, debugPaint);
|
||||
|
||||
// Chip and checked icon.
|
||||
if (chipIcon != null || (checkedIcon != null && currentChecked)) {
|
||||
calculateChipIconBounds(bounds, rectF);
|
||||
canvas.drawRect(rectF, debugPaint);
|
||||
}
|
||||
|
||||
// Chip text.
|
||||
if (chipText != null) {
|
||||
canvas.drawLine(
|
||||
bounds.left, bounds.exactCenterY(), bounds.right, bounds.exactCenterY(), debugPaint);
|
||||
}
|
||||
|
||||
// Close icon.
|
||||
if (closeIcon != null) {
|
||||
calculateCloseIconBounds(bounds, rectF);
|
||||
canvas.drawRect(rectF, debugPaint);
|
||||
}
|
||||
|
||||
// Chip touch bounds.
|
||||
debugPaint.setColor(ColorUtils.setAlphaComponent(Color.RED, 255 / 2));
|
||||
calculateChipTouchBounds(bounds, rectF);
|
||||
canvas.drawRect(rectF, debugPaint);
|
||||
|
||||
// Close icon touch bounds.
|
||||
debugPaint.setColor(ColorUtils.setAlphaComponent(Color.GREEN, 255 / 2));
|
||||
calculateCloseIconTouchBounds(bounds, rectF);
|
||||
canvas.drawRect(rectF, debugPaint);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user