关于安卓着色器的使用(一)

简介: 关于安卓着色器的使用(一)

背景:

对于着色器的使用,相信大部分的场景都有可能出现,例如以下的场景:
同一个图片,不同的颜色。这个时候,就可以使用着色器进行配置。

环境

as 4.2.2
jdk 8
win 10

使用

(一)xml中使用。当在xml中使用的时候,只需要申明即可,例子如下:

                        <ImageView
                            android:id="@+id/iv_select_music"
                            android:layout_width="12dp"
                            android:layout_height="12dp"
                            android:src="@drawable/ic_select_course_subject_arr"
                            app:layout_constraintBottom_toBottomOf="@+id/tv_select_music_text"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintTop_toTopOf="@+id/tv_select_music_text"
                            app:tint="@color/c_line_gray" />

上述代码中, 直接设置 app:tint="@color/c_line_gray" 既可以把图片设置成为对应的颜色。

(二)代码中设置。通过以下代码,可以动态设置着色器:

            val drawableRes = ResourcesCompat.getDrawable(
                resources,
                resId, null
            ) ?: return
            val tintIcon = DrawableCompat.wrap(drawableRes)
            DrawableCompat.setTint(tintIcon, hintColor)
            setImageDrawable(tintIcon)

注意,如果这里要处理取消着色器,重新设置setImageDrawable即可。

对于上述两种写法,页面中只有一个着色器效果的时候,都是没有问题的。若是多个着色器,则会导致复用问题。需要调用.mutate()生成一个新的drawable即可。具体实现如下:

            val drawableRes = ResourcesCompat.getDrawable(
                resources,
                resId, null
            ) ?: return
            val tintIcon = DrawableCompat.wrap(drawableRes.mutate())
            DrawableCompat.setTint(tintIcon, hintColor)
            setImageDrawable(tintIcon)

好了,观察mutate的源码解析:

    /**
     * Make this drawable mutable. This operation cannot be reversed. A mutable
     * drawable is guaranteed to not share its state with any other drawable.
     * This is especially useful when you need to modify properties of drawables
     * loaded from resources. By default, all drawables instances loaded from
     * the same resource share a common state; if you modify the state of one
     * instance, all the other instances will receive the same modification.
     *
     * Calling this method on a mutable Drawable will have no effect.
     *
     * @return This drawable.
     * @see ConstantState
     * @see #getConstantState()
     */
    public @NonNull Drawable mutate() {
        return this;
    }

大概的意思,如果不使用mutate的情况下,因为安卓共用一个同resId的drawable对象,所以如果drawable对象的属性发生了变化,则会导致其他使用到该drawable的地方,也会发生变化。若使用了该方法后,drawable对象的属性修改,不会对原有的共享的drawble对象产生影响。

that's all-----------------------------------------------------------------------------------------------------------------------

目录
相关文章
|
2月前
|
Android开发
Flutter适配安卓刘海、水滴屏显示全屏
Flutter适配安卓刘海、水滴屏显示全屏
44 2
|
XML Android开发 数据格式
安卓的几种布局介绍
安卓开发工作有相当一部分时间在画布局,而布局构建的好坏也影响到用户的体验和程序功能的健全,而我们最常见的有几种布局:LinearLayout(线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)、ConstraintLayout(约束布局)。
279 0
|
前端开发 Android开发
关于安卓自定义进度条(二)
关于安卓自定义进度条(二)
213 0
|
前端开发 API Android开发
安卓的自定义布局的使用
安卓开发中经常会遇到一些需求,它要求的视图用安卓原生控件表达不出来,这时候需要我们自定义一些view,这时候就是头疼的时候,其实很多情况都是UI采用了苹果的设计图,结果安卓端的控件不一样,因此需要重新设计来展现与IOS端相同的布局样式。
114 0
|
Java C# Android开发
Unity与安卓通信(2)
Unity与安卓通信(2)
106 0
|
Java API 图形学
安卓与Unity通信(1)
通过导入Unity的classes.jar,继承unity的UnityPlayerActivity实现unity与安卓的交互
255 0
|
iOS开发
iOS 粒子效果实现:CAEmitterLayer + CAEmitterCell
CAEmitterLayer是Core Animation中的特殊图层,继承自CALayer,是一个粒子发射器,用于控制粒子效果
400 0
iOS 粒子效果实现:CAEmitterLayer + CAEmitterCell
|
前端开发 Android开发 计算机视觉
|
Android开发
|
前端开发 Android开发
安卓凹凸自定义View
这个是产品的效果图 然后实际运行的结果 那到这个需求感觉还是很简单的,让美术出了一张图,然后我把这个背景图做成了.9图,然而,并没有什么卵用,最大的原因就是background被拉伸、挤压,高度在不同的机型显示的不一样,但是图片的半圆缺角是不变的,所以想想还是写个View。
1075 0