按钮的按下和释放.gif功能虽说简单,但也值得记录!先上刺刀!看招:
话不多说,上菜:布局XML
<TextView
android:id="@+id/btn"
android:layout_width="120dp"
android:layout_height="120dp"
android:text="Hello World!"
android:textColor="@color/btn_color"
android:background="@drawable/bg"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:background的bg.xml,实现圆形和色彩
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="@color/btn_color" android:width="2dp"/>
</shape>
MainActivity.java中的实现,监听文本按钮的触摸事件,实现缩和放的特效
final TextView textView=(TextView) findViewById(R.id.btn);
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN://收缩到0.8(正常值是1),速度500
textView.animate().scaleX(0.8f).scaleY(0.8f).setDuration(500).start();
break;
case MotionEvent.ACTION_UP:
textView.animate().scaleX(1).scaleY(1).setDuration(500).start();
break;
}
return true;
}
});
网友评论