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!!)
    }
}
相关推荐
betazhou1 小时前
mariadb5.5.56在centos7.6环境安装
android·数据库·adb·mariadb·msyql
doublelixin7 小时前
AOSP (Android11) 集成Google GMS三件套
android
xzkyd outpaper9 小时前
onSaveInstanceState() 和 ViewModel 在数据保存能力差异
android·计算机八股
梓仁沐白10 小时前
【Kotlin】协程
开发语言·python·kotlin
CYRUS STUDIO11 小时前
FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码
android·安全·逆向·app加固·fart·脱壳
WAsbry11 小时前
现代 Android 开发自定义主题实战指南
android·kotlin·material design
xzkyd outpaper11 小时前
Android动态广播注册收发原理
android·计算机八股
唐墨12312 小时前
android与Qt类比
android·开发语言·qt
林林要一直努力12 小时前
Android Studio 向模拟器手机添加照片、视频、音乐
android·智能手机·android studio
AD钙奶-lalala12 小时前
Mac版本Android Studio配置LeetCode插件
android·ide·android studio