Android 5.X之Ripple,Palette的详解

2024-03-04 17:40
文章标签 android 详解 palette ripple

本文主要是介绍Android 5.X之Ripple,Palette的详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android 5.x 提供了视图的水波纹效果, RippleDrawable 实现。

RippleDrawable特性:
自定义触摸反馈动画
以波纹效果来显示状态变化的 Drawable
ripple 标签即对应一个 RippleDrawable

使用:在V21中新建XML文件:

<?xml version="1.0" encoding="utf-8"?>  
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#FFEEEEEE">  <item android:drawable="@drawable/bg" />  </ripple>
  • 1

item android:drawable=”@drawable/bg”指定了按钮的正常状态的显示效果, android:color=”#FFEEEEEE”指定了点击时候的波纹颜色

1. item android:id=”@android:id/mask给水波纹做边界,其中的drawable可以是颜色,图片,shape.xml等等,如果不加边界,将是圆形扩散的。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"android:color="@color/colorAccent"><item android:id="@android:id/mask"android:drawable="@android:color/white" />
</ripple>
  • 1

<TextView...android:background="@drawable/ripple_no_mask"android:text="Ripple With No Mask" /><Button  ...android:text="Ripple With Color Mask"android:background="@drawable/ripple_no_mask"/><ImageButtonandroid:src="@mipmap/ic_launcher"android:background="@drawable/ripple_no_mask"/><ImageViewandroid:src="@mipmap/ic_launcher"android:clickable="true"android:background="@drawable/ripple_no_mask"/><ImageViewandroid:src="@mipmap/ic_launcher"android:clickable="true"android:background="@drawable/ripper"/>
  • 1

2. 另外ripple标签只能在5.X以上版本用,所以只可以在drawable-V21文件中创建。为了兼容低版本我们可以在drawable文件中创建同名文件模仿一下水波纹。

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="200" android:exitFadeDuration="200"><item android:state_pressed="false"><shape android:shape="oval"><solid android:color="@android:color/transparent" /></shape></item><item android:state_pressed="true"><shape android:shape="oval"><solid android:color="@color/colorAccent" /></shape></item></selector>
  • 1

4. Ripple标签还可以自定义点击交互动画。以selector实现。就想下面这样定义

<ripple xmlns:android="http://schemas.android.com/apk/res/android"android:color="@color/colorAccent"><item><selector><itemandroid:drawable="@mipmap/fancy_9"android:state_pressed="true"></item><itemandroid:drawable="@mipmap/fancy_10"android:state_pressed="false"></item></selector></item>
</ripple>
  • 1

在大多数情况下,您应以下列方式指定视图背景,在您的视图 XML 中应用此功能:
?android:attr/selectableItemBackground 指定有界的波纹
?android:attr/selectableItemBackgroundBorderless 指定越界的波纹
注意: selectableItemBackgroundBorderless 是 API 级别 21 中推出的新属性。

此外,您可利用 ripple 元素将 RippleDrawable 定义为一个 XML 资源。

您可以为 RippleDrawable 对象指定一种颜色。如果要改变默认触摸反馈颜色,请使用主题的 android:colorControlHighlight 属性。

下面是一个水波纹开源库:https://github.com/traex/RippleEffect

Palette

调色板类,可以让你从图像中提取突出的颜色。Palette 是兼容包中提供的,它是和 Andoid 5.x 一起出现的。

Palette 是兼容包中提供的,所有这里首先要做的是添加 palette-v7 的依赖:
compile ‘com.android.support:palette-v7:24.+’

官方提供了四种根据 Bitmap 对象来创建 Palette 对象的方法,分别是两种同步方法和两种异步方法,但是这四种方法上都标记了 @Deprecated,现在推荐使用 Builder 来创建。

Palette generate(Bitmap bitmap)
Palette generate(Bitmap bitmap, int numColors)
generateAsync(Bitmap bitmap, PaletteAsyncListener listener)
generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener)

下面是使用 Builder 来创建的方式:

//使用 Builder 来创建 Palette: 
//同步方法:Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_palette);Palette palette = new Palette.Builder(bitmap).generate();//异步方法:Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_palette);new Palette.Builder(bitmap).generate(new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {}});
  • 1

如何设置 size ?
调色板的大小值越大,生成的调色板的时间越长,数值越小,得到的颜色信息也越少,调色板的大小最好根据图片类型来决定,比如:

联系人头像:最优值24~32
风景图:8-16
当然默认是16,大多数情况下都可以取得很好的效果。

获取色板:
Palette.Swatch s1 = Palette.getVibrantSwatch(); //充满活力的色板
Palette.Swatch s2 = Palette.getDarkVibrantSwatch(); //充满活力的暗色类型色板
Palette.Swatch s3 = Palette.getLightVibrantSwatch(); //充满活力的亮色类型色板
Palette.Swatch s4 = Palette.getMutedSwatch(); //黯淡的色板
Palette.Swatch s5 = Palette.getDarkMutedSwatch(); //黯淡的暗色类型色板
Palette.Swatch s6 = Palette.getLightMutedSwatch(); //黯淡的亮色类型色板

在 Swatch 色板中提供了五种颜色信息,分别如下:

getPopulation(): 样本中的像素数量
getRgb(): 颜色的RBG值
getHsl(): 颜色的HSL值
getBodyTextColor(): 主体文字的颜色值
getTitleTextColor(): 标题文字的颜色值

通过 getRgb() 可以得到最终的颜色值。getBodyTextColor() 和 getTitleTextColor() 可以得到此颜色下文字适合的颜色,这样很方便我们设置文字的颜色,使文字看起来更加舒服。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.fj);new Palette.Builder(bitmap).generate(new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {Palette.Swatch s1 = palette.getVibrantSwatch();Palette.Swatch s2 = palette.getDarkVibrantSwatch();Palette.Swatch s3 = palette.getLightVibrantSwatch();Palette.Swatch  s4 = palette.getMutedSwatch();Palette.Swatch s5 = palette.getDarkMutedSwatch();Palette.Swatch  s6 = palette.getLightMutedSwatch();if (s1 != null) {getVibrantSwatch.setBackgroundColor(s1.getRgb());}if (s2 != null) {getDarkVibrantSwatch.setBackgroundColor(s2.getRgb());}if (s3 != null) {getLightVibrantSwatch.setBackgroundColor(s3.getRgb());}if (s4 != null) {getMutedSwatch.setBackgroundColor(s4.getRgb());}if (s5 != null) {getDarkMutedSwatch.setBackgroundColor(s5.getRgb());}if (s6 != null) {getLightMutedSwatch.setBackgroundColor(s6.getRgb());}if (s1 != null) {getBodyTextColor.setBackgroundColor(s1.getBodyTextColor());}if (s1 != null) {getTitleTextColor.setBackgroundColor(s1.getTitleTextColor());}if (s1 != null) {getPopulation.setBackgroundColor(s1.getPopulation());}}});
  • 1

这篇关于Android 5.X之Ripple,Palette的详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/773939

相关文章

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi

Rust中的BoxT之堆上的数据与递归类型详解

《Rust中的BoxT之堆上的数据与递归类型详解》本文介绍了Rust中的BoxT类型,包括其在堆与栈之间的内存分配,性能优势,以及如何利用BoxT来实现递归类型和处理大小未知类型,通过BoxT,Rus... 目录1. Box<T> 的基础知识1.1 堆与栈的分工1.2 性能优势2.1 递归类型的问题2.2