使用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
- 使用StateListAnimator实现Button按下变小,抬起变大效果
- 安卓5.0下使用StateListAnimator实现Button按下变小,抬起变大效果
- Button按下抬起状态
- btn变大变小的效果
- android实现选中变大变小效果及边框效果的方式
- android Button 的按下和抬起事件监听
- android button 按下及抬起事件监听器设置
- android button 按下及抬起事件监听器设置
- android Button 的按下和抬起事件监听
- android button 按下及抬起事件监听器设置
- android Button按下及抬起监听事件
- Button按钮的按下和抬起点击事件
- 按钮实现按下抬起不同响应
- 实现ImageView按下变暗,抬起变亮。
- Android 贝赛尔曲线实现自定义button 果冻-压下形变,抬起后弹弹的效果
- 列表的鼠标按下滚动抬起停止效果
- Unity UGUI Button 按下,抬起,连续按下,鼠标退出事件响应函数
- Unity UGUI Button 按下,抬起,连续按下,鼠标退出事件响应函数
- 关于Block的copy和循环引用的问题
- Android 基于google Zxing实现对手机中的二维码进行扫描
- mongodb启动命令
- Linux笔记(41)——ACL权限
- git pull push没有指定branch报错的解决方法
- 使用StateListAnimator实现Button按下变小,抬起变大效果
- SVN 无法连接的解决方法:网络代理
- android 导出数据库步骤 需要root
- mysql数据库主从配置
- 已知先序中序求后序
- Android录音控件
- myecliopse中代码修改不起作用
- iOS开发_统计xcode代码行数
- MeasureSpec学习 - 转