【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(一)

简介: 【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(一)

文章目录

一、Tint 着色器简介

二、布局文件中的 Tint 着色器基本用法

三、代码中使用 Tint 着色器添加颜色效果

四、参考资料





一、Tint 着色器简介


Tint 着色器的作用是是 可以使图片变色 , 使用该机制可以显示不同颜色的图片 ;


给定一个白色图标图片 , 如果要显示不同颜色的图片 , 可以直接在 ImageView 中设置 android:tint 或 app:tint 属性 , 设置一个颜色值 , 即可将该图片显示为指定颜色的图片 ;


这样一张图片 , 可以显示多种不同颜色的效果 , 从而减少了 APK 打包的图片数量 , 减少了 APK 安装包的大小 ;



该 tint 着色器效果是将非透明的像素点 , 渲染成指定的颜色 ;



用法示例 : 布局文件中 , 在 ImageView 标签中添加属性 app:tint="@color/purple_700" , 即可为其设置一个渲染颜色 ;


 

<ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"
        app:tint="@color/purple_700"
        app:srcCompat="@drawable/ic_plane"/>






二、布局文件中的 Tint 着色器基本用法


Tint 基本用法就是在 ImageView 组件中添加 app:tint 属性 , 为其设置一个颜色值属性值即可 ;



布局文件示例 :


<?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=".MainActivity">
    <ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        app:srcCompat="@drawable/ic_plane"/>
    <ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"
        app:tint="@color/purple_700"
        app:srcCompat="@drawable/ic_plane"/>
    <ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:tint="@color/teal_700"
        app:srcCompat="@drawable/ic_plane"/>
</androidx.constraintlayout.widget.ConstraintLayout>



运行效果展示 : 第一张图片是图片本身颜色 , 后面两张图片 , 分别设置了 Tint 颜色值 ;

image.png




目录
相关文章
|
6月前
|
存储 消息中间件 人工智能
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
226 3
|
10月前
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
317 3
|
10月前
|
Java Android开发
Android背景颜色滑动渐变效果(上下滑动,左右滑动)
本文分享了一种通过ScrollView实现滑动变色效果的简单方法。主要步骤包括:1) 在布局中添加ScrollView并确保内容可滑动;2) 获取屏幕高度;3) 获取控件高度;4) 使用GradientDrawable设置渐变颜色;5) 根据控件与屏幕高度比例动态调整颜色数量。示例代码展示了如何在滑动时根据比例改变背景颜色,实现流畅的视觉效果。
284 0
|
8月前
|
编解码 Java Android开发
安卓虚拟摄像头免root版,虚拟摄像头替换真实摄像头,jar代码开源分享
通过动态替换摄像头输入流的方式实现虚拟摄像头功能,代码经过简化展示核心逻辑。实际开发中还需要考虑视频编解码优化
|
10月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
278 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
10月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
841 19
|
10月前
|
存储 XML Java
Android 文件数据储存之内部储存 + 外部储存
简介:本文详细介绍了Android内部存储与外部存储的使用方法及核心原理。内部存储位于手机内存中,默认私有,适合存储SharedPreferences、SQLite数据库等重要数据,应用卸载后数据会被清除。外部存储包括公共文件和私有文件,支持SD卡或内部不可移除存储,需申请权限访问。文章通过代码示例展示了如何保存、读取、追加、删除文件以及将图片保存到系统相册的操作,帮助开发者理解存储机制并实现相关功能。
2419 2
|
10月前
|
Android开发 开发者
Android自定义view获取attr中自定义颜色的问题
本文针对Android自定义View在布局中设置颜色时遇到的问题进行分析与解决。问题表现为通过`getAttributeIntValue`方法获取颜色时,使用资源引用(如`@color/colorPrimary`)无法正确获取,而直接使用十六进制颜色值(如`#ff0000`)则正常。经过源码分析,发现是属性格式定义及获取方式不当导致。解决方案为将`attrs`文件中颜色属性的格式改为`reference|color`,并使用`TypedArray`的`getColor`方法获取颜色值,确保资源引用和直接颜色值均能正确解析。希望本文能帮助遇到类似问题的开发者。
206 0
|
Android开发
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
414 0
【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )(二)
|
5月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
812 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡

热门文章

最新文章