解说社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 910|回复: 1

谁能帮我看一下这代码是啥意思?

[复制链接]
发表于 2020-9-20 10:05:08 来自手机 | 显示全部楼层 |阅读模式
require"import"
import "android.animation.ValueAnimator"
local w=activity.width
SnackerBar={shouldDismiss=true}
import "android.view.animation.AccelerateDecelerateInterpolator"

local layout={
  LinearLayout,
  Gravity="bottom",
  {
    LinearLayout,
    layout_height=-2,
    layout_width=-1,
    Gravity="center",
    BackgroundColor=0xff333333,
    {
      TextView,
      textColor=0xffffffff,
      layout_weight=.8,
      paddingLeft="10dp",
      paddingTop="5dp",
      paddingBottom="5dp",
      layout_width=0,
    },
    {Button,
      layout_height=-2,
      style="?android:attr/buttonBarButtonStyle",
      text="DONE",
    }
  }
}
local function addView(view)
  local mLayoutParams=ViewGroup.LayoutParams
  (-1,-1)
  activity.Window.DecorView.addView(view,mLayoutParams)
end

local function removeView(view)
  activity.Window.DecorView.removeView(view)
end
--设置提示文字
function SnackerBar:msg(textMsg)
  self.textView.text=textMsg
  return self
end
--设置按钮文字
function SnackerBar:actionText(textAction)
  self.actionView.text=textAction
  return self
end
--设置按钮动作
function SnackerBar:action(func)
  self.actionView.onClick=
  function()
    func()
    self:dismiss()
  end
  return self
end

--显示
function SnackerBar:show()
  local view=self.view
  addView(view)
  view.translationY=300
  view.animate().translationY(0)
  .setInterpolator(AccelerateDecelerateInterpolator())
  .setDuration(800).start()
  indefiniteDismiss(self)
end

function indefiniteDismiss(snackerBar)
  task(2000,function()
    if snackerBar.shouldDismiss==true then
      snackerBar:dismiss()
    else
      indefiniteDismiss(snackerBar)
    end
  end)
end

--关闭
function SnackerBar:dismiss()
  local view=self.view
  view.animate().translationY(300)
  .setDuration(800)
  .setListener(Animator.AnimatorListener{
    onAnimationEnd=function()
      removeView(view)
    end
  }).start()
end

SnackerBar.__index=SnackerBar
--构建者模式
function SnackerBar.build()
  local mSnackerBar={}
  setmetatable(mSnackerBar,SnackerBar)
  mSnackerBar.view=loadlayout(layout)
  mSnackerBar.bckView=mSnackerBar.view
  .getChildAt(0)
  mSnackerBar.textView=mSnackerBar.bckView
  .getChildAt(0)
  mSnackerBar.actionView=mSnackerBar.bckView
  .getChildAt(1)
  local function animate(v,tx,dura)
    ValueAnimator().ofFloat({v.translationX,tx}).setDuration(dura)
    .addUpdateListener( ValueAnimator.AnimatorUpdateListener
    {
      onAnimationUpdate=function( p1)
        local f=p1.animatedValue
        v.translationX=f
        v.alpha=1-math.abs(v.translationX)/w
      end
    }).addListener(ValueAnimator.AnimatorListener{
      onAnimationEnd=function()
        if math.abs(tx)>=w then
          removeView(mSnackerBar.view)
        end
      end
    }).start()
  end
  local frx,p,v,fx=0,0,0,0
  mSnackerBar.bckView.setOnTouchListener(View.OnTouchListener{
    onTouch=function(view,event)
      if event.Action==event.ACTION_DOWN then
        mSnackerBar.shouldDismiss=false
        frx=event.x
        fx=event.x
      elseif event.Action==event.ACTION_MOVE then
        --高精度手指速度测量
        if math.abs(event.rawX-frx)>=2 then
          v=math.abs((frx-event.rawX)/(os.clock()-p)/1000)
        end
        p=os.clock()
        frx=event.rawX
        view.translationX=frx-fx
        view.alpha=1-math.abs(view.translationX)/w
      elseif event.Action==event.ACTION_UP then
        mSnackerBar.shouldDismiss=true
        local tx=view.translationX
        if tx>=w/5 then
          animate(view,w,(w-tx)/v)
        elseif tx>0 and tx<w/5 then
          animate(view,0,tx/v)
        elseif tx<=-w/5 then
          animate(view,-w,(w+tx)/v)
        else
          animate(view,0,-tx/v)
        end
        fx=0
      end
      return true
    end
  })
  return mSnackerBar
end



  来自解说社区客户端
回复

使用道具 举报

 楼主| 发表于 2020-9-20 10:06:06 来自手机 | 显示全部楼层
这竟然还有问题。



  来自解说社区客户端
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|解说社区

GMT+8, 2024-5-19 14:54 , Processed in 0.045340 second(s), 13 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表