欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现长按圆环动画View效果的思路代码

程序员文章站 2022-04-12 19:12:53
一、需求来源最近想到一个需求,类似悦跑圈或者keep的结束按钮动画二、思路代码该动画按钮的主要作用就是防止用户误操作,具体实现思路如下:1、监听用户的触摸事件ontouchlistener,在acti...

一、需求来源

最近想到一个需求,类似悦跑圈或者keep的结束按钮动画

Android实现长按圆环动画View效果的思路代码
Android实现长按圆环动画View效果的思路代码

二、思路代码

该动画按钮的主要作用就是防止用户误操作,具体实现思路如下:
1、监听用户的触摸事件ontouchlistener,在action_down的时候,记录下xy坐标和触摸时间,同时start自定义view动画;在action_move的过程中,判断坐标差值的偏移量是否在一个可接受的范围内,是的话就保留当前动画,不是的话就清除按钮上绘制的path;在action_up的时候,再次记录下触摸时间,比较两个时间是否达到了长按规定的时间,是的话就执行下一个事件,不是的话就停止动画重置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画一个圆,再使用属性动画来监听动画的播放即可

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()
  }

三、效果展示

最终实现效果图虽然没有上面那么好看,但基本效果还是达到了

Android实现长按圆环动画View效果的思路代码

四、全部代码

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

  /**
   * 中心点坐标、半径
   */
  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() // 清除直线数据,保留数据结构,方便快速重用
    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
      }
    })

总结

到此这篇关于android实现长按圆环动画view效果的文章就介绍到这了,更多相关android长按圆环动画内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!