Button自定义样式及动画
程序员文章站
2022-03-01 21:05:15
...
1.自定义样式
drawable目录下面新建shape_buttuon
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#ff91d119" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="24dip" />
s
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
然后设为button背景
2.自定义动画
(1).添加监听器
bt_login.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 点击按钮松开后按钮缩小
narrow(v);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 点击按钮未松开时按钮放大
blow_up(v);
}
return false;
}
});
(2).动画
// 放大按钮动画
private void blow_up(View v) {
float[] vaules = new float[] { 0.9f, 0.95f, 1.0f };
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(v, "scaleX", vaules),
ObjectAnimator.ofFloat(v, "scaleY", vaules));
set.setDuration(30);
set.start();
}
// 缩小按钮动画
private void narrow(View v) {
float[] vaules = new float[] { 1.0f, 0.95f, 0.9f };
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(v, "scaleX", vaules),
ObjectAnimator.ofFloat(v, "scaleY", vaules));
set.setDuration(30);
set.start();
}
3.也可以使用selector编辑
在drawable下面新建selector,设置backgroud为selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_press" android:state_pressed="true"/>
<item android:drawable="@drawable/button_nomal" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/button_focus" android:state_focused="true"/>
<item android:drawable="@drawable/button_nomal" android:state_focused="false"/>
</selector>
上一篇: 方法定义及调用 练习
下一篇: animation动画