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

来源:互联网 发布:手机淘宝历史价格查询 编辑:程序博客网 时间:2023/12/10 22:41
<?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,执行相关的属性动画,来实现。


0 0