使用StateListAnimator实现Button按下变小,抬起变大效果

本文介绍如何使用StateListAnimator和属性动画实现按钮点击反馈效果。通过放置在anim文件夹下的XML配置,设置不同状态下的动画表现。适用于Android 5.0及以上版本,文中还探讨了在低版本中实现类似效果的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <set android:ordering="together">
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="0.99"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="0.99"
                android:valueType="floatType" />
        </set>
    </item>
    <item>
        <set android:ordering="together">

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.01"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.01"
                android:valueType="floatType" />
        </set>
    </item>
</selector>

可以看到这里使用的是属性动画实现。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">

        <com.tu315.SquaredButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#ff0000"
            android:stateListAnimator="@anim/btn_selector_animator"
            android:text="旅行"
            android:textColor="#ffffff"
            android:textSize="32sp" />

        <com.tu315.SquaredButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="#ff0000"
            android:stateListAnimator="@anim/btn_selector_animator"
            android:text="读书"
            android:textColor="#ffffff"
            android:textSize="32sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.tu315.SquaredButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ff0000"
            android:stateListAnimator="@anim/btn_selector_animator"
            android:text="饮食"
            android:textColor="#ffffff"
            android:textSize="32sp" />

        <com.tu315.SquaredButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ff0000"
            android:stateListAnimator="@anim/btn_selector_animator"
            android:text="运动"
            android:textColor="#ffffff"
            android:textSize="32sp" />
    </LinearLayout>

</LinearLayout>


public class SquaredButton extends Button {
    public SquaredButton(Context context) {
        super(context);
    }

    public SquaredButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquaredButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SquaredButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }
}


public class StateListActivity extends Activity
        implements View.OnClickListener {
    private String TAG = StateListActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state_list);
    }

    @Override
    public void onClick(View v) {

    }
}

效果图如下:




总结:

(1)android:stateListAnimator这个属性是在android5.0系统才会有效果的。

android:stateListAnimator="@anim/btn_selector_animator"
(2)btn_selector_animator.xml放在anim文件夹下就可以了。

(3)这里使用属性动画的时候,set android:ordering="together"来同时播放两个动画。

(4)

android:duration="@android:integer/config_shortAnimTime" // 动画实现
android:propertyName="scaleX" // 操作的属性
android:valueTo="0.99" //  值的变化范围
android:valueType="floatType" // 值的单位

其实这里还有一个android:valueFrom。

(5)也可以看到这stateListAnimator的实现大概也是利用selector,item进行的,

当匹配到相关的属性的时候执行相关的动画。以前的版本是每个item里面存放的是drawable之类的。

而现在是animator(属性动画)。

(6)如何在低版本实现相关的这种动画呢?

http://stackoverflow.com/questions/30545379/android-statelistanimator-in-pre-lollipop

这里给出了答案,

也就是这个库实现的原理https://github.com/ZieIony/Carbon

(7)其实还有实现方法,就是在onTouch里面的MOVE_UP,MOVE_DOWN,执行相关的属性动画,来实现。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值