自定义属性动画框架

本文详细介绍了如何实现一个自定义属性动画框架,允许开发者为控件设置平移、渐变、缩放和透明度等动画。通过设定特定的属性参数,可以组合多个方向的平移动画。文章分析了如何解决实现框架时遇到的问题,包括为child对象设置属性动画、获取用户设定的参数、监听滑动事件,并提出了解决方案,包括自定义LinearLayout、FramLayout和ScrollView。通过自定义接口和方法,实现动画的执行和恢复,以及根据用户滑动计算动画执行比例。
摘要由CSDN通过智能技术生成

先来观察完整的动画效果

分析:每个child都可以设置自己的属性动画 根据滑动出自身的高度比例来进行开发者设置的属性动画(平移 渐变 缩放 透明度...)既然是动画框架封装 肯定是希望开发者用的更爽,代码简洁 意图清晰。 

首先设定好自定义的属性参数

<declare-styleable name="DiscrollView_LayoutParams">
        <attr name="discrollve_alpha" format="boolean"/>
        <attr name="discrollve_scaleX" format="boolean"/>
        <attr name="discrollve_scaleY" format="boolean"/>
        <attr name="discrollve_fromBgColor" format="color"/>
        <attr name="discrollve_toBgColor" format="color"/>
        <attr name="discrollve_translation">
            <flag name="fromTop" value="0x01" />
            <flag name="fromBottom" value="0x02" />
            <flag name="fromLeft" value="0x04" />
            <flag name="fromRight" value="0x08" />
        </attr>
    </declare-styleable>

解析为什么discrollve_translation 各个参数0x01 0x02 0x04 0x08
0x01 0000 0000 0000 0001 fromTop
0x02 0000 0000 0000 0010 fromBottom
0x04 0000 0000 0000 0100 fromLeft
0x08 0000 0000 0000 1000 fromRight
这样设置的目的 因为这个平移可以同时多个方向移动 比如我们希望他 左上方向移动 就可以这样设置
app:discrollve_translation = "fromTop|fromLeft"
既 它们的值 0000 0000 0000 0101 将来我们判断的时候 可以这样判断
比如判断是否设置了fromTop  就拿这个值 0101 & 0001fromTop = 0001 如果得出来的值等于fromTop就说明有设置这个值

我们希望开发者用这个属性动画框架的时候 只需要对对应的child设置相应属性参数 将来运行的时候就会执行相应的属性动画
理想开发者在布局文件设定即可 
 

<com.twy.mywearview.MyScrollView xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:discrollve="http://schemas.android.com/apk/res-auto">
    <com.twy.mywearview.MyLinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <ImageView
           android:layout_width="match_parent"
           android:layout_height="600dp"
           android:src="@mipmap/tb_bg"
           android:scaleType="centerCrop"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#007788"
            android:textColor="@android:color/black"
            android:textSize="25sp"
            android:padding="25dp"
            android:gravity="center"
            android:fontFamily="serif"
            android:text="带上您的行李箱,准备shopping!"
            discrollve:discrollve_alpha="true"
            tools:ignore="MissingPrefix" />
        <ImageView
            android:layout_width="200dp"
            android:layout_height="120dp"
            android:layout_gravity="top|right"
            discrollve:discrollve_translation="fromLeft|fromBottom"
            discrollve:discrollve_alpha="true"
            android:src="@mipmap/baggage"
            tools:ignore="MissingPrefix" />

        <TextView
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值