Android中使用Palette让你的页面UI优雅起来

文章目录

    • 1. 什么是Palette
    • 2. 引入Palette
    • 3. 使用 Palette
      • 3.1 同步方式
      • 3.2 异步方式
      • 3.3 获取色调值
    • 4. 举例
      • 4.1 布局文件 activity_palette_list.xml ⬇️
      • 4.2 Activity:PaletteListActivity⬇️
      • 4.3 列表Adapter:PaletteListAdapter ⬇️
      • 4.4 列表item布局:list_item_palette.xml ⬇️
      • 4.5 效果

1. 什么是Palette

Palette 翻译过来是调色板的意思,在android开发中,它的作用是自动分析一张图片中的色调,并且提取出合适的颜色,来帮我们进行动态配色
所谓的动态配色,是说根据页面中不同图片的色调,自动为其他部分的View设置背景色,以达到视觉上的协调,美观。

比如,根据页面中图片的色调,改变ToolBar的背景色,状态栏的颜色;根据卡片图片的色调,改变图片上文字的背景色等场景。

2. 引入Palette

在 moudle级别的 build.gradle 中添加 palette 的依赖

// java 工程
implementation 'androidx.palette:palette:1.0.0'// kotlin 工程
implementation 'androidx.palette:palette-ktx:1.0.0'

3. 使用 Palette

首先是获取 palette 对象。根据Bitmap,我们可以

3.1 同步方式

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.test1)
val paletteBuilder = Palette.from(bitmap)
val palette = paletteBuilder.generate()

3.2 异步方式

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.test1)
val paletteBuilder = Palette.from(bitmap)
paletteBuilder.generate(object : PaletteAsyncListener {override fun onGenerated(palette: Palette?) {// 得到了 palette}
})
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
// 同步
Palette.Builder builder = Palette.from(bitmap);
Palette palette=builder.generate();
// 异步
builder.generate(bitmap, new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {}
}

3.3 获取色调值

获取了 Palette 对象后,就可以调用它的各种方法,获取各种我们需要的色调值了。

Palette.from(it).generate(object : PaletteAsyncListener{override fun onGenerated(palette: Palette?) {palette ?: return// 获取到柔和的深色的颜色, Color.GREEN是获取不到时的默认颜色val darkMutedColor = palette.getDarkMutedColor(Color.GREEN)// 获取到柔和的明亮的颜色val lightMutedColor = palette.getLightMutedColor(Color.GREEN)// 获取到活跃的深色的颜色val darkVibrantColor = palette.getDarkVibrantColor(Color.GREEN)// 获取到活跃的明亮的颜色val lightVibrantColor = palette.getLightVibrantColor(Color.GREEN)// 获取图片中一个最柔和的颜色val mutedColor = palette.getMutedColor(Color.GREEN)// 获取图片中最活跃的颜色,也可以说整个图片出现最多的颜色val vibrantColor = palette.getVibrantColor(Color.GREEN)// 获取某种特性颜色的样品val lightVibrantSwatch = palette.vibrantSwatch// 谷歌推荐的:图片的整体的颜色rgb的混合值---主色调val rgb = lightVibrantSwatch?.rgb// 谷歌推荐:图片中间的文字颜色val bodyTextColor = lightVibrantSwatch?.bodyTextColor// 谷歌推荐:作为标题的颜色(有一定的和图片的对比度的颜色值)val titleTextColor = lightVibrantSwatch?.titleTextColor// 颜色向量val hsl = lightVibrantSwatch?.hsl// 分析该颜色在图片中所占的像素多少值val population = lightVibrantSwatch?.population}})

4. 举例

用 palette 做一个优雅的卡片列表。

4.1 布局文件 activity_palette_list.xml ⬇️

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".palette.PaletteListActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rcv_palette"android:layout_width="match_parent"android:layout_height="match_parent" /></androidx.constraintlayout.widget.ConstraintLayout>

4.2 Activity:PaletteListActivity⬇️

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.example.mytest.R
import com.example.mytest.databinding.ActivityPaletteListBindingclass PaletteListActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityPaletteListBinding.inflate(layoutInflater)setContentView(binding.root)val dataList = mutableListOf<PaletteBean>(PaletteBean(R.drawable.test1, "标题1"),PaletteBean(R.drawable.test2, "hello2"),PaletteBean(R.drawable.test3, "hello3"),PaletteBean(R.drawable.test4, "hello4"),PaletteBean(R.drawable.test1, "标题1"),PaletteBean(R.drawable.test2, "hello2"),PaletteBean(R.drawable.test3, "hello3"),PaletteBean(R.drawable.test4, "hello4"),PaletteBean(R.drawable.test1, "标题1"),PaletteBean(R.drawable.test2, "hello2"),PaletteBean(R.drawable.test3, "hello3"),PaletteBean(R.drawable.test4, "hello4"),)binding.rcvPalette.apply {adapter = PaletteListAdapter(dataList)layoutManager = GridLayoutManager(this@PaletteListActivity, 2)}}
}

4.3 列表Adapter:PaletteListAdapter ⬇️

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.graphics.drawable.toBitmapOrNull
import androidx.palette.graphics.Palette
import androidx.recyclerview.widget.RecyclerView
import com.example.mytest.databinding.ListItemPaletteBinding
import kotlin.math.roundToIntclass PaletteListAdapter(private val dataList: MutableList<PaletteBean> = mutableListOf()) :RecyclerView.Adapter<PaletteListAdapter.MyViewHolder>() {fun update(lDataList: List<PaletteBean>) {dataList.clear()dataList.addAll(lDataList)notifyDataSetChanged()}class MyViewHolder(val binding: ListItemPaletteBinding) :RecyclerView.ViewHolder(binding.root) {fun bind(data: PaletteBean?) {data ?: returnbinding.ivCover.setImageResource(data.imgId)binding.tvContent.text = data.titleval bitmap = binding.ivCover.drawable.toBitmapOrNull()bitmap?.let {Palette.from(it).generate { palette ->val titleColor = palette?.vibrantSwatch?.titleTextColorval bg = getTranslucentColor(0.5f, palette?.vibrantSwatch?.rgb!!)binding.tvContent.setTextColor(titleColor!!)binding.tvContent.setBackgroundColor(bg)}}}/*** 获取指定透明度的颜色值*/private fun getTranslucentColor(percent: Float, rgb: Int): Int {val blue = Color.blue(rgb)val green = Color.green(rgb)val red = Color.red(rgb)var alpha = Color.alpha(rgb)alpha = (alpha * percent).roundToInt()return Color.argb(alpha, red, green, blue)}}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {val binding =ListItemPaletteBinding.inflate(LayoutInflater.from(parent.context), parent, false)return MyViewHolder(binding)}override fun getItemCount(): Int {return dataList?.size ?: 0}override fun onBindViewHolder(holder: MyViewHolder, position: Int) {holder.bind(dataList?.get(position))}
}

4.4 列表item布局:list_item_palette.xml ⬇️

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="150dp"app:cardCornerRadius="4dp"app:cardElevation="1dp"app:cardPreventCornerOverlap="false"app:cardUseCompatPadding="true"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="150dp"><ImageViewandroid:id="@+id/iv_cover"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop"android:src="@drawable/test1"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_content"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"android:text="你好啊"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

4.5 效果

请添加图片描述

怎么样?这样卡片底部的蒙层就合图片的主色调一致,文字颜色也是相对和谐的,整体看起来就优雅多了吧。

如果这篇文章对你有用的话,欢迎支持哦~感谢!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/12891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

「Python绘图」绘制同心圆

python 绘制同心圆 一、预期结果 二、核心代码 import turtle print("开始绘制同心圆") # 创建Turtle对象 pen turtle.Turtle() pen.shape("turtle") # 移动画笔到居中位置 pen.pensize(2) #设置外花边的大小 # 设置填充颜色 pen.fillcolor("green&…

java 并发线程应用

java 并发线程相关 线程状态 新建(NEW): 创建后尚未启动。可运行(RUNABLE): 正在 Java 虚拟机中运行。但是在操作系统层面,它可能处于运行状态,也可能等待资源调度(例如处理器资源),资源调度完成就进入运行状态。所以该状态的可运行是指可以被运行,具体有没有运行要看底层…

【C++算法】堆相关经典算法题

1.最后一块石头的重量 其实就是一个模拟的过程&#xff1a;每次从石堆中拿出最大的元素以及次大的元素&#xff0c;然后将它们粉碎&#xff1b;如果还有剩余&#xff0c;就将剩余的石头继续放在原始的石堆里面重复上面的操作&#xff0c;直到石堆里面只剩下一个元素&#xff0c…

[C/C++] -- 装饰器模式

装饰器模式是一种结构型设计模式&#xff0c;它允许在不改变原始对象的基础上动态地扩展其功能。这种模式通过将对象包装在装饰器类的对象中来实现&#xff0c;每个装饰器对象都包含一个原始对象&#xff0c;并可以在调用原始对象的方法之前或之后执行一些额外的操作。 装饰器…

自学C语言能达到什么境界呢?

C 语言是一门广泛应用于系统软件、嵌入式系统、游戏开发等领域的编程语言。那么&#xff0c;通过自学 C 语言&#xff0c;能够达到什么样的境界呢&#xff1f; 就像学习小提琴一样&#xff0c;仅凭自学也可以达到一定的水平&#xff0c;能够自娱自乐&#xff0c;在亲友聚会时表…

Xed编辑器开发第一期:使用Rust从0到1写一个文本编辑器

这是一个使用Rust实现的轻量化文本编辑器。学过Rust的都知道&#xff0c;Rust 从入门到实践中间还隔着好几个Go语言的难度&#xff0c;因此&#xff0c;如果你也正在学习Rust,那么恭喜你&#xff0c;这个项目被你捡到了。本项目内容较多&#xff0c;大概会分三期左右陆续发布&a…

NAS导航面板Sun-Panel

什么是 Sun-Panel &#xff1f; Sun-Panel 是一个服务器、NAS 导航面板、Homepage、浏览器首页。 软件主要特点&#xff1a; &#x1f349; 界面简洁&#xff0c;功能强大&#xff0c;资源消耗低&#x1f34a; 简单易用&#xff0c;可视化操作&#xff0c;零代码使用&#x1f…

python怎么安装matplotlib

1、登陆官方网址“https://pypi.org/project/matplotlib/#description”&#xff0c;下载安装包。 2、选择合适的安装包&#xff0c;下载下来。 3、将安装包放置到python交互命令窗口的当前目录下。 4、打开windows的命令行窗口&#xff0c;通过"pip install"这个命令…

新质生产力之工业互联网产业链

随着全球经济的数字化转型&#xff0c;新基建的概念逐渐成为推动工业发展的关键动力。在这一转型过程中&#xff0c;工业互联网作为新基建的核心组成部分&#xff0c;正逐渐塑造着未来工业的面貌。那么工业互联网产业链是如何构成的&#xff0c;以及它如何成为推动工业4.0和智能…

CRMEB开源打通版/标准版v4电商商城系统小程序发布之后无法生成海报问题

小程序产品分销二维码生成不了 开发者工具可以生成海报&#xff0c;但是发布之后无法生成 1.在开发者工具中&#xff0c;将不校验合法域名关闭 2.点击生成海报&#xff0c;查看console 3.将域名填写到微信公众平台小程序的download合法域名中 网址微信公众平台

react18【系列实用教程】memo —— 缓存组件 (2024最新版)

memo 的语法 如上图所示&#xff0c;在react中&#xff0c;当父组件重新渲染时&#xff0c;子组件也会重新渲染&#xff0c;即便子组件无任何变化&#xff0c;通过 memo 可以实现对组件的缓存&#xff0c;即当子组件无变化时&#xff0c;不再重新渲染子组件&#xff0c;核心代码…

【深度学习】Diffusion扩散模型的逆扩散问题

1、前言 上一篇&#xff0c;我们讲了Diffusion这个模型的原理推导。但在推导中&#xff0c;仍然遗留了一些问题。本文将解决那些问题 参考论文&#xff1a; ①Variational Diffusion Models (arxiv.org) ②Tutorial on Diffusion Models for Imaging and Vision (arxiv.org…

迭代的难题:敏捷团队每次都有未完成的工作,如何破解?

各位是否遇到过类似的情况&#xff1a;每次迭代结束后&#xff0c;团队都有未完成的任务&#xff0c;很少有完成迭代全部的工作&#xff0c;相反&#xff0c;总是将上期未完成的任务重新挪到本期计划会中&#xff0c;重新规划。敏捷的核心之一是“快速迭代&#xff0c;及时反馈…

ubuntu20.04 ROS 环境下使用速腾80线激光雷达

1.相关系统环境 系统版本:ubuntu 20.04 ROS版本&#xff1a;ROS1 - noetic 激光雷达型号&#xff1a;RoboSense Ruby &#xff08;更新于2024.5.14&#xff09; 2.网口配置&#xff1a; 将PC/工控机的网口配置为&#xff1a; ipv4&#xff0c;方式设置为手动 ip地址、掩码以…

基于springboot实现社区智慧养老监护管理平台系统项目【项目源码+论文说明】计算机毕业设计

基于SpringBoot实现社区智慧养老监护管理平台系统演示 摘要 如今社会上各行各业&#xff0c;都在用属于自己专用的软件来进行工作&#xff0c;互联网发展到这个时候&#xff0c;人们已经发现离不开了互联网。互联网的发展&#xff0c;离不开一些新的技术&#xff0c;而新技术的…

EE-SX670 槽型光电开关 5MM 限位检测感应器 使用案例

EE-SX670是一款槽型光电开关&#xff0c;也被称为U形传感器或限位检测感应器。它是光电传感器中的一种&#xff0c;通过检测物体是否插入其感应槽来触发开关。这种传感器通常用于自动化生产线上的位置检测、对象计数以及安全设备中的运动检测。 EE-SX670作为一款高性能的光电传…

谷歌外贸seo优化怎么做?

一般有两种选择&#xff0c;在大型电商平台开展业务&#xff0c;如亚马逊&#xff0c;阿里巴巴等平台&#xff0c;也可以选择搭建自己的独立站 选择在大型电商平台可以方便迅速建立起自己的商铺&#xff0c;不需要考虑太多交易&#xff0c;支付&#xff0c;物流等方面的问题&am…

MybatisPlus拓展功能(内附全功能代码)

目录 代码生成 静态工具 案例 逻辑删除 枚举处理器 ​编辑 Json处理器 分页插件功能 ​编辑 案例 封装转换方法 代码生成 静态工具 案例 Overridepublic UserVO queryUserAndAddressById(long id) { // 1.查询用户User user getById(id);if (user null || …

mobarxtem应用与华为设备端口绑定技术

交换机端口绑定 华为交换机的基础配置与MOBAXTERM终端连接 实验步骤&#xff1a; 一、给每个交换机划分vlan并添加端口 1.单个vlan的划分 2.批量划分vlan 在高端交换机CE6800上批量划分连续编号的VLAN&#xff0c;本例中连续的vlan20到vlan25 [~CE6800]vlan b 20 to 25 3…