Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)

Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)

这篇

Android绘图Path基于LinearGradient线性渐变,Kotlin(1)-CSDN博客文章浏览阅读633次,点赞11次,收藏17次。Android SurfaceView简例Android中各的SurfaceView和View有很大的不同,两者应用场景不同。其中,LinearGradient 1,2,3只是修改渲染器的渲染模式,LinearGradient 1为重复(repeat),LinearGradient 2为镜像模式(mirror),Lin_android lineargradient。作者提供了`getPoints`方法和`FloatPoint`类的示例,以及`PaintView`组件在实际应用中的使用。https://blog.csdn.net/zhangphil/article/details/144508262画出的渐变线是静态的,下面实现动态动画的线性渐变:

Kotlin 复制代码
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Shader
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.widget.AppCompatImageView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MyView : AppCompatImageView {
    private var mLinearGradient: LinearGradient? = null
    private var mPaint: Paint? = null
    private var mPath: Path? = null

    private var mStartX = 0f
    private var mStartY = 0f

    private var mEndX = 0f
    private var mEndY = 0f

    private val DELTA = 3f

    constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
        mPaint?.style = Paint.Style.STROKE
        mPaint?.strokeWidth = 50f

        mPath = Path()
        mPath?.moveTo(mStartX, mStartY)

        val W = resources.displayMetrics.widthPixels
        val H = resources.displayMetrics.heightPixels

        val factor = H / W

        CoroutineScope(Dispatchers.IO).launch {
            while (true) {
                delay(5)

                mEndX = mEndX + DELTA
                mEndY = mEndX * factor//mEndY + DELTA

                Log.d("fly", "$mEndX $mEndY")

                mPath?.lineTo(mEndX, mEndY)

                mLinearGradient =
                    LinearGradient(
                        mStartX,
                        mStartY,
                        mEndX,
                        mEndY,
                        intArrayOf(Color.RED, Color.BLUE, Color.YELLOW),
                        null,
                        Shader.TileMode.CLAMP
                    )

                mPaint?.setShader(mLinearGradient)

                if (mEndX >= W || mEndY >= H) {
                    Log.d("fly", "break $mEndX $mEndY $W $H")
                    break
                }

                postInvalidate()
            }
        }
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawPath(mPath!!, mPaint!!)
    }
}
相关推荐
触想工业平板电脑一体机2 小时前
【触想智能】工业安卓一体机在人工智能领域上的市场应用分析
android·人工智能·智能电视
2501_915921433 小时前
iOS 是开源的吗?苹果系统的封闭与开放边界全解析(含开发与开心上架(Appuploader)实战)
android·ios·小程序·uni-app·开源·iphone·webview
allk554 小时前
OkHttp源码解析(一)
android·okhttp
allk554 小时前
OkHttp源码解析(二)
android·okhttp
2501_915909067 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
2501_915909067 小时前
苹果软件混淆与 iOS 代码加固趋势,IPA 加密、应用防反编译与无源码保护的工程化演进
android·ios·小程序·https·uni-app·iphone·webview
2501_916007477 小时前
苹果软件混淆与 iOS 应用加固实录,从被逆向到 IPA 文件防反编译与无源码混淆解决方案
android·ios·小程序·https·uni-app·iphone·webview
介一安全7 小时前
【Frida Android】基础篇6:Java层Hook基础——创建类实例、方法重载、搜索运行时实例
android·java·网络安全·逆向·安全性测试·frida
沐怡旸10 小时前
【底层机制】【Android】深入理解UI体系与绘制机制
android·面试
啊森要自信10 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash