1,有时候在项目开发中遇到一些按钮的点击动画,下面就写一个比较简单的按钮缩放效果
- 首先我们的在res目录下amin文件下创建一个Xml文件夹用来设置动画的效果,
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0"
android:toXScale="0.9"
android:fromYScale="1.0"
android:toYScale="0.9"
android:pivotX="50%"
android:pivotY="50%"
android:duration="100"/>
</set>
我们在点击事件中去应用
(延时跳转)
Animation animation = AnimationUtils.loadAnimation(MainFirst_Acticity.this, R.anim.set_scale);
card_fragment_a_1.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intentCH = new Intent(MainFirst_Acticity.this, ChangeActivity.class);
startActivity(intentCH);
}
}, 300);
2,还有一种简便的写法 就是继承OnTouchListener,重写OnTouch方法:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
/*按下操作*/
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
animation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(100);
animation.setFillAfter(true);
view.startAnimation(animation);
}
/*抬起操作*/
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
animation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(100);
animation.setFillAfter(true);
view.startAnimation(animation);
}
return false;
}
并且按钮必须要添加这两个监听:
card_fragment_a_1.setOnClickListener(this);
card_fragment_a_1.setOnTouchListener(this);