亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android粒子線條效果實(shí)現(xiàn)過程與代碼

 更新時(shí)間:2023年02月09日 09:29:29   作者:撿一晌貪歡  
這篇文章主要介紹了Android粒子線條效果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

前言

很久沒寫代碼了,忙工作、忙朋友、人也懶了,最近重新調(diào)整自己,對技術(shù)還是要有熱情,要熱情的話還是用自定義view做游戲有趣,寫完這個(gè)粒子線條后面我會(huì)更新幾個(gè)小游戲博文及代碼,希望讀者喜歡。

這個(gè)粒子效果的控件是去年寫的,寫的很差勁,這幾天又重構(gòu)了一下,還是難看的要命,勉強(qiáng)記錄下吧。

需求

主要就是看到博客園的粒子線條背景很有意思,就想模仿一下。核心思想如下:

1、隨機(jī)出現(xiàn)點(diǎn)

2、范圍內(nèi)的點(diǎn)連線

3、手指按下,加入點(diǎn),范圍內(nèi)點(diǎn)向手指移動(dòng)

效果圖

效果圖就是難看,沒得說。

代碼

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import java.lang.ref.WeakReference
import kotlin.math.pow
import kotlin.math.sqrt
/**
 * 模仿博客粒子線條的view
 *
 * 核心思想簡易版
 *
 * 1、隨機(jī)出現(xiàn)點(diǎn)
 * 2、范圍內(nèi)的點(diǎn)連線
 * 3、手指按下,加入點(diǎn),范圍內(nèi)點(diǎn)向手指移動(dòng)
 *
 * @author silence
 * @date 2022-11-09
 *
 */
class ParticleLinesBgView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
): View(context, attributeSet, defStyleAttr){
    companion object{
        // 屏幕刷新時(shí)間,每秒20次
        const val SCREEN_FLUSH_TIME = 50L
        // 新增點(diǎn)的間隔時(shí)間
        const val POINT_ADD_TIME = 200L
        // 粒子存活時(shí)間
        const val POINT_ALIVE_TIME = 18000L
        // 吸引的合適距離
        const val ATTRACT_LENGTH = 250f
        // 維持的合適距離
        const val PROPER_LENGTH = 150f
        // 粒子被吸引每次接近的距離
        const val POINT_MOVE_LENGTH = 30f
        // 距離計(jì)算公式
        fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float {
            return sqrt(((x1 - x2).toDouble().pow(2.0)
                    + (y1 - y2).toDouble().pow(2.0)).toFloat())
        }
    }
    // 存放的粒子
    private val mParticles = ArrayList<Particle>(64)
    // 手指按下位置
    private var mTouchParticle: Particle? = null
    // 處理的handler
    private val mHandler = ParticleHandler(this)
    // 畫筆
    private val mPaint = Paint().apply {
        color = Color.LTGRAY
        strokeWidth = 3f
        style = Paint.Style.STROKE
        flags = Paint.ANTI_ALIAS_FLAG
    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // 通過發(fā)送消息給handler實(shí)現(xiàn)間隔添加點(diǎn)
        mHandler.removeMessages(0)
        mHandler.sendEmptyMessageDelayed(0, POINT_ADD_TIME)
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        // 繪制點(diǎn)和線
        for (i in 0 until mParticles.size) {
            val point = mParticles[i]
            canvas.drawPoint(point.x, point.y, mPaint)
            // 連線
            for (j in (i + 1) until mParticles.size) {
                val another = mParticles[j]
                val distance = getDistance(point.x, point.y, another.x, another.y)
                if (distance <= PROPER_LENGTH) {
                    canvas.drawLine(point.x, point.y, another.x, another.y, mPaint)
                }
            }
        }
        mTouchParticle?.let {
            // 手指按下點(diǎn)與附近連線
            for(point in mParticles) {
                val distance = getDistance(point.x, point.y, it.x, it.y)
                if (distance <= PROPER_LENGTH) {
                    canvas.drawLine(point.x, point.y, it.x, it.y, mPaint)
                }
            }
            // 吸引范圍顯示
            mPaint.color = Color.BLUE
            canvas.drawCircle(it.x, it.y, PROPER_LENGTH, mPaint)
            mPaint.color = Color.LTGRAY
        }
    }
    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when(event.action) {
            MotionEvent.ACTION_DOWN -> {
                mTouchParticle = Particle(event.x, event.y, 0)
            }
            MotionEvent.ACTION_MOVE -> {
                mTouchParticle!!.x = event.x
                mTouchParticle!!.y = event.y
                invalidate()
            }
            MotionEvent.ACTION_UP -> {
                mTouchParticle = null
            }
        }
        return true
    }
    // 粒子
    class Particle(var x: Float, var y: Float, var counter: Int)
    // kotlin自動(dòng)編譯為Java靜態(tài)類,控件引用使用弱引用
    class ParticleHandler(view: ParticleLinesBgView): Handler(Looper.getMainLooper()){
        // 控件引用
        private val mRef: WeakReference<ParticleLinesBgView> = WeakReference(view)
        // 粒子出現(xiàn)控制
        private var mPointCounter = 0
        override fun handleMessage(msg: Message) {
            mRef.get()?.let {view->
                // 新增點(diǎn)
                mPointCounter++
                if (mPointCounter == (POINT_ADD_TIME / SCREEN_FLUSH_TIME).toInt()) {
                    // 隨機(jī)位置
                    val x = (Math.random() * view.width).toFloat()
                    val y = (Math.random() * view.height).toFloat()
                    view.mParticles.add(Particle(x, y, 0))
                    mPointCounter = 0
                }
                val iterator = view.mParticles.iterator()
                while (iterator.hasNext()) {
                    val point = iterator.next()
                    // 移除失活粒子
                    if (point.counter == (POINT_ALIVE_TIME / SCREEN_FLUSH_TIME).toInt()) {
                        iterator.remove()
                    }
                    // 手指按下時(shí),粒子朝合適的距離移動(dòng)
                    view.mTouchParticle?.let {
                        val distance = getDistance(point.x, point.y, it.x, it.y)
                        if(distance in PROPER_LENGTH..ATTRACT_LENGTH) {
                            // 橫向接近
                            if (point.x < it.x) point.x += POINT_MOVE_LENGTH
                            else point.x -= POINT_MOVE_LENGTH
                            // 縱向接近
                            if (point.y < it.y) point.y += POINT_MOVE_LENGTH
                            else point.y -= POINT_MOVE_LENGTH
                        }else if(distance <= PROPER_LENGTH) {
                            // 橫向遠(yuǎn)離
                            if (point.x < it.x) point.x -= POINT_MOVE_LENGTH
                            else point.x += POINT_MOVE_LENGTH
                            // 縱向遠(yuǎn)離
                            if (point.y < it.y) point.y -= POINT_MOVE_LENGTH
                            else point.y += POINT_MOVE_LENGTH
                        }
                    }
                }
                // 循環(huán)發(fā)送
                view.invalidate()
                view.mHandler.sendEmptyMessageDelayed(0, POINT_ADD_TIME)
            }
        }
    }
}

這里沒寫onMeasure,注意下不能用wrap-content,布局的話改個(gè)黑色背景就行了。

主要問題

下面簡單講講吧。

粒子

這里用了個(gè)數(shù)據(jù)類構(gòu)造了粒子,用了一個(gè)ArrayList來存放,本來想用linkedHashMap來保存并實(shí)現(xiàn)下LRU的,結(jié)果連線的時(shí)候比較復(fù)雜,重構(gòu)的時(shí)候直接刪了,后面用了一個(gè)counter來控制粒子的存活時(shí)間。

邏輯控制

一開始的時(shí)候想的比較復(fù)雜,實(shí)現(xiàn)來弄得自己頭疼,后面覺得何不將邏輯和繪制分離,在ondraw里面只進(jìn)行繪制不就行了,邏輯通過handler來更新,實(shí)際這樣在我看來是對的。

我這用了一個(gè)Handler配合嵌套循環(huán)發(fā)送空消息,實(shí)現(xiàn)定時(shí)更新效果,每隔一段時(shí)間更新一下邏輯,Handler內(nèi)部通過弱引用獲得view,并對其中的內(nèi)容修改,修改完成后,通過invalidate出發(fā)線程更新。

新增點(diǎn)

Handler會(huì)定時(shí)更新,只需要在handleMessage里面添加點(diǎn)就行了,為了控制點(diǎn)出現(xiàn)的頻率,我這又引入了控制變量。

粒子生命周期

handleMessage里面會(huì)檢查粒子是否失活,失活了就通過iterator去移除,移除數(shù)組內(nèi)內(nèi)容還是盡量通過iterator去實(shí)現(xiàn)吧,特別是for-eacn循環(huán)以及for循環(huán)內(nèi)刪除多個(gè)時(shí),會(huì)出錯(cuò)的!

粒子趨向于手指

手指按下時(shí)設(shè)置mTouchParticle,移動(dòng)時(shí)更新這個(gè)mTouchParticle,手指抬起時(shí)對mTouchParticle賦空,這樣在handleMessage里面只要在mTouchParticle不為空時(shí)稍稍改變下其他粒子的位置,就可以達(dá)到趨向效果。

粒子連線

這里我沒有想到什么好辦法,直接兩兩計(jì)算,并對合適距離的粒子進(jìn)行連線。

到此這篇關(guān)于Android粒子線條效果實(shí)現(xiàn)過程與代碼的文章就介紹到這了,更多相關(guān)Android粒子線條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論