Android 使用 RotateDrawable 实现箭头动画

Android 使用 RotateDrawable 实现箭头动画

最终效果.gif

xml代码

TextView设置文字及背景色以及代码设置drawableStart、drawableEnd,Button用于设置点击事件。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="com.john.test.FAndroid" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/test_text_view"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:background="@color/colorAccent"
            android:layout_gravity="center"
            android:padding="12dp"
            android:text="123123123" />

        <Button
            android:id="@+id/test_click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="200dp"
            android:text="click" />
    </LinearLayout>
</layout>

Kotlin 代码

注意,mutate()方法非常重要,它是一个不可逆的操作,可以将drawable�实例状态独立出来。

class TestActivity : AppCompatActivity() {
    private lateinit var mBinding:ActivityTestBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_test)
        mBinding.lifecycleOwner = this
        
        val d1 = ResourcesCompat.getDrawable(resources, R.drawable.rotate, null) as RotateDrawable
        val d2 = ResourcesCompat.getDrawable(resources, R.drawable.rotate, null) as RotateDrawable
        Log.d("lq", "is drawable the same: ${d1 == d2}")
        d1.mutate()
        mBinding.testTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(d1, null, d2, null)

        var chose = true
        mBinding.testClick.setOnClickListener {
            if (chose) {
                ObjectAnimator.ofInt(d1, "level", 0, 5000).setDuration(300).start()
                ObjectAnimator.ofInt(d2,"level",5000,10000).setDuration(300).start()
            } else {
                ObjectAnimator.ofInt(d2,"level",0,5000).setDuration(300).start()
                ObjectAnimator.ofInt(d1, "level", 5000, 10000).setDuration(300).start()
            }
            chose = !chose
        }
    }
}

文章均来自互联网如有不妥请联系作者删除QQ:314111741 地址:http://www.mqs.net/post/13358.html

相关阅读

  • 淘宝怎么运营推广(中小卖家必学的操作思路)

    淘宝怎么运营推广(中小卖家必学的操作思路)

    淘宝在很长一段时间内,一直被认为是中国最大的电子商务平台。人们在这里购物,与在别处购物一样,会感觉到很便捷、很实惠。因此,随着电商行业的发展。如今,淘宝网站的活跃用户数已经超过了1亿人(目前该数据仅统计了部分用户)。而作为一个淘宝...

    2025.12.09 14:15:37作者:iseeyuTags:运营
  • 如何保证缓存和数据的双写一致性

    如何保证缓存和数据的双写一致性

    image 但是在更新缓存方面,对于更新完数据库,是更新缓存呢,还是删除缓存。又或者是先删除缓存,再更新数据库,其实大家存在很大的争议。目前没有一篇全面的博客,对这几种方案进行解析。于是博主战战兢兢,顶着被大家喷的风险,写了这篇...

    2025.12.09 09:28:14作者:iseeyu
  • 【百度搜索引擎优化】如何快速了解百度搜索引擎优化的知识?(搜索引擎优化基本)

    【百度搜索引擎优化】如何快速了解百度搜索引擎优化的知识?(搜索引擎优化基本)

    在百度输入SEO优化,下拉框就有很多关键词,SEO优化工具,SEO查询,SEO技巧,SEO优化方案,SEO报价,SEO优化教程,SEO优化软件,SEO优化怎么做,等等,相关搜索也有很多长尾关键词。还可以加入一些群,找些大牛问下,向这些大牛学...

    2025.12.09 07:37:38作者:iseeyu

添加新评论