Using an animation to make the control shake is actually to make the horizontal movement (which can be mixed with other types of rotation) of the animation loop playback, using the interpolator class.
The principle is very simple. I will not talk about it anymore,CodeAs follows:
Activity Code:
This. BTN. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {textview yearandmouth = (textview) jgworklogdateandoperapanel. this. findviewbyid (R. id. year_mounth_text); animation cycleanim = animationutils. loadanimation (context, R. anim. img_anim); yearandmouth. startanimation (cycleanim );}});
Imag_anim.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Set xmlns: Android = "http://schemas.android.com/apk/res/android"> <translate Android: Duration = "100" Android: fromydelta = "0" Android: toydelta = "500" Android: fromxdelta = "0" Android: toxdelta = "1" Android: repeatcount = "5" Android: repeatmode = "restart" Android: interpolator = "@ anim/cylce"/> <rotate Android: Duration = "300" Android: effectx = "100% P" Android: effecty = "100% P" Android: fromdegrees = "90" Android: interpolator = "@ anim/cylce"/> </set>
Cylce. xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Officially parses the animation cycle accelerator: -- accelerateinterpolator: the animation changes from the start to the end, which is a process of acceleration. -- Decelerateinterpolator: the animation starts from the beginning to the end, and the change rate is a deceleration process. -- Cycleinterpolator: the animation starts from the beginning to the end, and the change rate is the sine curve of the given number of cycles. -- Acceleratedecelerateinterpolator: the animation starts from the beginning to the end, and the change rate is the process of accelerating and slowing down. -- Linearinterpolator: the animation changes linearly from start to end. --> <Cycleinterpolator xmlns: Android = "http://schemas.android.com/apk/res/android" Android: cycles = "5"/>