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!!)
    }
}
相关推荐
帅得不敢出门15 分钟前
MTK Android11 APP调用OTA升级
android·java·开发语言·framework
2501_9159090624 分钟前
苹果应用加密方案的一种方法,在没有源码的前提下,如何处理 IPA 的安全问题
android·安全·ios·小程序·uni-app·iphone·webview
用户20187928316726 分钟前
Android App 换肤原理:用 "装修小房子" 故事浅谈
android
百锦再28 分钟前
与AI沟通的正确方式——AI提示词:原理、策略与精通之道
android·java·开发语言·人工智能·python·ui·uni-app
2501_9159090632 分钟前
iOS 项目中常被忽略的 Bundle ID 管理问题
android·ios·小程序·https·uni-app·iphone·webview
dora32 分钟前
如何防防防之防抓包伪造请求
android·安全
2501_9159214340 分钟前
iOS App 测试的工程化实践,多工具协同的一些尝试
android·ios·小程序·https·uni-app·iphone·webview
爱埋珊瑚海~~1 小时前
Android Studio模拟器一直加载中
android·ide·android studio
C+++Python1 小时前
PHP 反射 API
android·java·php
G31135422731 小时前
android之IM即时通信原理
android