Android實(shí)現(xiàn)長按圓環(huán)動畫View效果的思路代碼
一、需求來源
最近想到一個需求,類似悅跑圈或者Keep的結(jié)束按鈕動畫
二、思路代碼
該動畫按鈕的主要作用就是防止用戶誤操作,具體實(shí)現(xiàn)思路如下:
1、監(jiān)聽用戶的觸摸事件OnTouchListener,在ACTION_DOWN的時候,記錄下xy坐標(biāo)和觸摸時間,同時start自定義View動畫;在ACTION_MOVE的過程中,判斷坐標(biāo)差值的偏移量是否在一個可接受的范圍內(nèi),是的話就保留當(dāng)前動畫,不是的話就清除按鈕上繪制的path;在ACTION_UP的時候,再次記錄下觸摸時間,比較兩個時間是否達(dá)到了長按規(guī)定的時間,是的話就執(zhí)行下一個事件,不是的話就停止動畫重置Path。
val touchMax = 50 var lastX = 0 var lastY = 0 circleView.setOnTouchListener(object : View.OnTouchListener{ override fun onTouch(p0: View?, motionEvent: MotionEvent): Boolean { val endTime: Long val x = motionEvent.x val y = motionEvent.y when (motionEvent.action) { MotionEvent.ACTION_DOWN -> { startTime = System.currentTimeMillis() lastX = x.toInt() lastY = y.toInt() circleView.startAnim() } MotionEvent.ACTION_UP -> { endTime = System.currentTimeMillis() val during = endTime - startTime if (during < App.LONG_CLICK_TIME) { circleView.cancelAnim() circleView.clearAll() }else{ playMaxWarn() } } MotionEvent.ACTION_MOVE -> { if (abs(lastX - x) > touchMax || abs(lastY - y) > touchMax) { circleView.clearAll() } } } return false } })
2、就是在自定義View里arcTo畫一個圓,再使用屬性動畫來監(jiān)聽動畫的播放即可
fun startAnim() { isClear = false valueAnimator = ValueAnimator.ofFloat(0F, 359.9999F) valueAnimator!!.duration = App.LONG_CLICK_TIME valueAnimator!!.addUpdateListener { animation -> mProgress = animation.animatedValue as Float invalidate() } valueAnimator!!.start() }
三、效果展示
最終實(shí)現(xiàn)效果圖雖然沒有上面那么好看,但基本效果還是達(dá)到了
四、全部代碼
package cn.xmliu.melongo.view import android.animation.ValueAnimator import android.content.Context import android.graphics.* import android.util.AttributeSet import android.view.View import androidx.core.content.ContextCompat import cn.xmliu.melongo.App import cn.xmliu.melongo.R /** * Date: 2020/8/12 13:21 * Email: diyangxia@163.com * Description: 長按動畫View */ class LongCircleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) { /** * 畫筆 */ private val paint = Paint() private var arcPath: Path? = null private var rectF: RectF? = null private var lineColor = 0 /** * 中心點(diǎn)坐標(biāo)、半徑 */ private var centerX: Float? = null private var centerY: Float? = null private var radius: Float? = null private var left = -1F private var top = -1F private var right = -1F private var bottom = -1F private val offset = 10 private var mProgress = -1F private var valueAnimator: ValueAnimator ?= null private var isClear = true init { lineColor = ContextCompat.getColor(context!!, R.color.red) } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) centerX = width / 2.toFloat() centerY = height / 2.toFloat() radius = height / 2.toFloat() left = centerX!! - radius!! + offset top = centerY!! - radius!! + offset right = centerX!! + radius!! - offset bottom = centerY!! + radius!! - offset rectF = RectF(left, top, right, bottom) arcPath = Path() } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) paint.isAntiAlias = true paint.color = lineColor paint.style = Paint.Style.STROKE paint.strokeWidth = 10F arcPath!!.rewind() // 清除直線數(shù)據(jù),保留數(shù)據(jù)結(jié)構(gòu),方便快速重用 if(isClear) return arcPath!!.arcTo(rectF!!, 270F, mProgress) canvas?.drawPath(arcPath!!, paint) } fun startAnim() { isClear = false valueAnimator = ValueAnimator.ofFloat(0F, 359.9999F) valueAnimator!!.duration = App.LONG_CLICK_TIME valueAnimator!!.addUpdateListener { animation -> mProgress = animation.animatedValue as Float invalidate() } valueAnimator!!.start() } fun cancelAnim(){ valueAnimator!!.cancel() } fun clearAll() { isClear = true invalidate() } }
<RelativeLayout android:layout_width="wrap_content" android:layout_marginTop="5dp" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/flashLayout" android:layout_centerInParent="true" android:layout_width="70dp" android:layout_height="70dp" android:background="@drawable/btn_circle_white" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/flashIV" android:layout_width="40dp" android:layout_height="40dp" android:padding="7dp" android:src="@drawable/menu_flash_black" android:text="閃燈開" android:tint="@color/main_color" /> <TextView android:id="@+id/flashTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="閃燈" android:textColor="@color/main_color" /> </LinearLayout> <cn.xmliu.melongo.view.LongCircleView android:id="@+id/circleView" android:layout_width="80dp" android:layout_height="80dp" /> </RelativeLayout>
val touchMax = 50 var lastX = 0 var lastY = 0 circleView.setOnTouchListener(object : View.OnTouchListener{ override fun onTouch(p0: View?, motionEvent: MotionEvent): Boolean { val endTime: Long val x = motionEvent.x val y = motionEvent.y when (motionEvent.action) { MotionEvent.ACTION_DOWN -> { startTime = System.currentTimeMillis() lastX = x.toInt() lastY = y.toInt() circleView.startAnim() } MotionEvent.ACTION_UP -> { endTime = System.currentTimeMillis() val during = endTime - startTime if (during < App.LONG_CLICK_TIME) { circleView.cancelAnim() circleView.clearAll() }else{ flashTV.text = "OK" } } MotionEvent.ACTION_MOVE -> { if (abs(lastX - x) > touchMax || abs(lastY - y) > touchMax) { circleView.clearAll() } } } return false } })
總結(jié)
到此這篇關(guān)于Android實(shí)現(xiàn)長按圓環(huán)動畫View效果的文章就介紹到這了,更多相關(guān)android長按圓環(huán)動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android BottomNavigationBar底部導(dǎo)航控制器使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android BottomNavigationBar底部導(dǎo)航控制器使用方法,感興趣的小伙伴們可以參考一下2016-03-03RecyclerView實(shí)現(xiàn)側(cè)滑拖拽功能
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)側(cè)滑拖拽功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07android studio3.3.1代碼提示忽略大小寫的設(shè)置
這篇文章主要介紹了android studio3.3.1代碼提示忽略大小寫的設(shè)置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03android實(shí)現(xiàn)來電靜音示例(監(jiān)聽來電)
這篇文章主要介紹了手機(jī)來電鈴聲響起后,通過此代碼實(shí)現(xiàn)靜音而非掛斷的方法的相關(guān)資料2014-03-03android實(shí)現(xiàn)倒計時功能(開始、暫停、0秒結(jié)束)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計時功能,開始、暫停、0秒結(jié)束,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09ReactiveCocoa代碼實(shí)踐之-RAC網(wǎng)絡(luò)請求重構(gòu)
這篇文章主要介紹了ReactiveCocoa代碼實(shí)踐之-RAC網(wǎng)絡(luò)請求重構(gòu) 的相關(guān)資料,需要的朋友可以參考下2016-04-04Android自定義View onDraw()方法會調(diào)用兩次的問題解決
這篇文章主要介紹了Android自定義View onDraw()方法會調(diào)用兩次的問題解決,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01Android編程實(shí)現(xiàn)從字符串中查找電話號碼的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)從字符串中查找電話號碼的方法,涉及Android針對字符串的匹配與查找相關(guān)技巧,需要的朋友可以參考下2016-03-03