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!!)
    }
}
相关推荐
龙之叶14 小时前
Android13源码下载和编译过程详解
android·linux·ubuntu
闲暇部落15 小时前
kotlin内联函数——runCatching
android·开发语言·kotlin
大渔歌_16 小时前
软键盘显示/交互问题
android
LuiChun1 天前
webview_flutter_android 4.3.0使用
android·flutter
Tanecious.1 天前
C语言--分支循环实践:猜数字游戏
android·c语言·游戏
闲暇部落1 天前
kotlin内联函数——takeIf和takeUnless
android·kotlin
张云瀚1 天前
《Kotlin核心编程》下篇
kotlin·kotlin核心编程
Android西红柿1 天前
flutter-android混合编译,原生接入
android·flutter
大叔编程奋斗记1 天前
【Salesforce】审批流程,代理登录 tips
android
命运之手2 天前
[ Spring ] Spring Cloud Gateway 2025 Comprehensive Overview
java·kotlin·gateway·spring-cloud