前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android UI】Paint ComposeShader 组合渲染 ( Shader 叠加模式 | Xfermode | PorterDuff.Mode | BlendMode )

【Android UI】Paint ComposeShader 组合渲染 ( Shader 叠加模式 | Xfermode | PorterDuff.Mode | BlendMode )

作者头像
韩曙亮
发布2023-03-30 15:58:28
4590
发布2023-03-30 15:58:28
举报

文章目录

一、ComposeShader 组合渲染


Paint 的 ComposeShader 是 组合渲染 , 可以将两个个 Shader 渲染组合使用 ;

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

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

代码语言:javascript
复制
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.
代码语言:javascript
复制
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.
代码语言:javascript
复制
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 组合渲染代码示例


代码语言:javascript
复制
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);
    }

}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、ComposeShader 组合渲染
  • 二、ComposeShader 组合渲染代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档