1、
代码:
TranslateAnimation animation = new TranslateAnimation(0, -5, 0, 0);
animation.setInterpolator(new OvershootInterpolator());
animation.setDuration(100);
animation.setRepeatCount(3);
animation.setRepeatMode(Animation.REVERSE);
usecar_Apply_text.startAnimation(animation);
左边抖动的代码:
public static ObjectAnimator tada(View view) {
return tada(view, 1f);
}
public static ObjectAnimator tada(View view, float shakeFactor) {
PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
Keyframe.ofFloat(0f, 1f),
Keyframe.ofFloat(.1f, .9f),
Keyframe.ofFloat(.2f, .9f),
Keyframe.ofFloat(.3f, 1.1f),
Keyframe.ofFloat(.4f, 1.1f),
Keyframe.ofFloat(.5f, 1.1f),
Keyframe.ofFloat(.6f, 1.1f),
Keyframe.ofFloat(.7f, 1.1f),
Keyframe.ofFloat(.8f, 1.1f),
Keyframe.ofFloat(.9f, 1.1f),
Keyframe.ofFloat(1f, 1f)
);
PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
Keyframe.ofFloat(0f, 1f),
Keyframe.ofFloat(.1f, .9f),
Keyframe.ofFloat(.2f, .9f),
Keyframe.ofFloat(.3f, 1.1f),
Keyframe.ofFloat(.4f, 1.1f),
Keyframe.ofFloat(.5f, 1.1f),
Keyframe.ofFloat(.6f, 1.1f),
Keyframe.ofFloat(.7f, 1.1f),
Keyframe.ofFloat(.8f, 1.1f),
Keyframe.ofFloat(.9f, 1.1f),
Keyframe.ofFloat(1f, 1f)
);
PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
Keyframe.ofFloat(0f, 0f),
Keyframe.ofFloat(.1f, -3f * shakeFactor),
Keyframe.ofFloat(.2f, -3f * shakeFactor),
Keyframe.ofFloat(.3f, 3f * shakeFactor),
Keyframe.ofFloat(.4f, -3f * shakeFactor),
Keyframe.ofFloat(.5f, 3f * shakeFactor),
Keyframe.ofFloat(.6f, -3f * shakeFactor),
Keyframe.ofFloat(.7f, 3f * shakeFactor),
Keyframe.ofFloat(.8f, -3f * shakeFactor),
Keyframe.ofFloat(.9f, 3f * shakeFactor),
Keyframe.ofFloat(1f, 0)
);
return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate).setDuration(1000);
}
ObjectAnimator animator = tada(usecar_Apply_text);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.start();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
animator.end();
}
};
public static ObjectAnimator nope(View view) {
int delta = view.getResources().getDimensionPixelOffset(R.dimen.spacing_medium);
PropertyValuesHolder pvhTranslateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
Keyframe.ofFloat(0f, 0),
Keyframe.ofFloat(.10f, -delta),
Keyframe.ofFloat(.26f, delta),
Keyframe.ofFloat(.42f, -delta),
Keyframe.ofFloat(.58f, delta),
Keyframe.ofFloat(.74f, -delta),
Keyframe.ofFloat(.90f, delta),
Keyframe.ofFloat(1f, 0f)
);
return ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX).
setDuration(500);
}
final ObjectAnimator nopeAnimator = nope(plugin_handle_text);
nopeAnimator.setRepeatCount(ValueAnimator.INFINITE);
nopeAnimator.start();
第一步:准备两个动画效果的XML文件,加入到 res/anim/目录下:
shake.xml文件,抖动的方式:
第二步: //代码使用动画效果:
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);//加载动画资源文件
usecar_Apply_text.startAnimation(shake); //给组件播放动画效果