Android UIPaint ComposeShader 组合渲染 ( Shader 叠加模式 | Xfermode | PorterDuff.Mode | BlendMode )

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android UIPaint ComposeShader 组合渲染 ( Shader 叠加模式 | Xfermode | PorterDuff.Mode | BlendMode )相关的知识,希望对你有一定的参考价值。

文章目录





一、ComposeShader 组合渲染



PaintComposeShader 组合渲染 , 可以将两个个 Shader 渲染组合使用 ;


ComposeShader 文档地址 : https://developer.android.google.cn/reference/kotlin/android/graphics/ComposeShader


ComposeShader 组合渲染 需要设置 dst 和 src 两个渲染 , 还有两个渲染的组合模式 , 可以设置 Xfermode / PorterDuff.Mode / BlendMode 三种之一的组合模式 ;

ComposeShader(
    shaderA: Shader, // The colors from this shader are seen as the "dst" by the mode This value cannot be null.
    shaderB: Shader, // The colors from this shader are seen as the "src" by the mode This value cannot be null.
    mode: Xfermode) // The mode that combines the colors from the two shaders. If mode is null, then SRC_OVER is assumed.
ComposeShader(
    shaderA: Shader, // The colors from this shader are seen as the "dst" by the mode This value cannot be null.
    shaderB: Shader, // The colors from this shader are seen as the "src" by the mode This value cannot be null.
    mode: PorterDuff.Mode) // The PorterDuff mode that combines the colors from the two shaders. This value cannot be null.
ComposeShader(
    shaderA: Shader, // The colors from this shader are seen as the "dst" by the mode This value cannot be null.
    shaderB: Shader, // The colors from this shader are seen as the "src" by the mode This value cannot be null.
    blendMode: BlendMode) // The blend mode that combines the colors from the two shaders. This value cannot be null.




二、ComposeShader 组合渲染代码示例



package kim.hsl.paintgradient.compose;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class ComposeShaderView extends View 

    /**
     * 画笔工具
     * 线性渐变渲染 需要设置给该 画笔工具
     */
    private Paint mPaint;

    /**
     * 使用线性渐变绘制的区域
     */
    private RectF mRectF;

    public ComposeShaderView(Context context) 
        this(context, null);
    

    public ComposeShaderView(Context context, @Nullable AttributeSet attrs) 
        this(context, attrs, 0);
    

    public ComposeShaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) 
        super(context, attrs, defStyleAttr);
        initPaint();
    

    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) 
        super.onSizeChanged(width, height, oldWidth, oldHeight);
    

    /**
     * 初始化 画笔工具, 主要是设置该画笔的渲染
     */
    private void initPaint() 
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
    

    @Override
    protected void onDraw(Canvas canvas) 
        super.onDraw(canvas);

        SweepGradient sg = new SweepGradient(
                this.getWidth() / 2,
                this.getHeight() / 2,
                Color.RED, Color.YELLOW);

        RadialGradient rg = new RadialGradient(
                this.getWidth() / 2,
                this.getHeight() / 2,
                400,
                Color.GREEN, Color.YELLOW,
                Shader.TileMode.CLAMP);

        ComposeShader cs = new ComposeShader(rg, sg, PorterDuff.Mode.MULTIPLY);


        mPaint.setShader(cs);
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 400, mPaint);
    


以上是关于Android UIPaint ComposeShader 组合渲染 ( Shader 叠加模式 | Xfermode | PorterDuff.Mode | BlendMode )的主要内容,如果未能解决你的问题,请参考以下文章

Android UIPaint Gradient 渐变渲染 ③ ( RadialGradient 环形渐变渲染 | 在给定中心和半径的情况下绘制径向渐变的着色器 | 水波纹效果 )

Android UIPaint Gradient 渐变渲染 ② ( SweepGradient 梯度渐变渲染 | 围绕中心点绘制扫描渐变的着色器 | 多渐变色构造函数 | 雷达扫描效果 )

Android逆向系列文章— Android基础逆向

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本