Android属性动画实现跑马灯

一、前言

有时候要实现跑马灯效果,但是网上的跑马灯实现方式都比较复杂,如果要求不高的话使用属性动画会简单一些。主要有以下优缺点:

优点:

  1. 代码简单,方便修改
  2. 动画过程可以进行控制
  3. 动画结束后可以回归原始位置(或者其它位置)

缺点:

  1. 没有办法一行文字,两端各显示一半,比如已经移动了一半时候,末尾不会出现另外一半

二、代码

使用HorizontalScrollView包裹,防止文字显示不完

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <HorizontalScrollView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1234567890"
            android:lines="1"/>
    </HorizontalScrollView>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画"
        android:onClick="onClick"
        app:layout_constraintTop_toBottomOf="@+id/tv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="撤销动画"
        android:onClick="onClick"
        app:layout_constraintTop_toBottomOf="@+id/btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
    private var textView: TextView ?= null
    private var animator: ObjectAnimator ?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById(R.id.tv)
    }

    fun onClick(view: View){
        when(view.id){
            R.id.btn -> {
                startAnimation()
            }
            R.id.cancel_btn -> {
                cancelAnimation()
            }
        }
    }

    private fun startAnimation(){
        val right = textView!!.right //当前控件相对于父控件的位置
        val selfWith = textView!!.width // 当前控件的宽度
        Log.d("YM","----->right:$right")
        Log.d("YM","----->selfWith:$selfWith")
        var times = 1
        animator = ObjectAnimator.ofFloat(textView!!, "translationX", 200f,-500f).apply {
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            addListener{
                doOnRepeat {
                    times++
                    if (times > 1){
                        setFloatValues(0f,-500f)
                    }
                    Log.d("YM","---->重复次数:$times")
                }
            }
            start()
        }
    }

    private fun cancelAnimation(){
        animator?.cancel()//因为属性动画是移动位置的,所以直接撤销的话会停在移动的位置。所以要再次开始并撤销才行
        animator?.start()
        animator?.cancel()
        textView!!.translationX = 0f
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值