食神摇摇中图片的晃动效果

[img]http://dl.iteye.com/upload/attachment/0083/7558/366dea82-aac8-3a89-9052-1442e3d2aeda.jpg[/img]

可以是这样子实现滴:

btn_shake=(ImageView)findViewById(R.id.btn_shake);
startShakeAnimation(btn_shake);



private void startShakeAnimation(View v){
int pivot = Animation.RELATIVE_TO_SELF;
CycleInterpolator interpolator = new CycleInterpolator(3.0f);
RotateAnimation animation = new RotateAnimation(0, 15, pivot, 0.5f,pivot, 0.5f);
animation.setStartOffset(4000);
animation.setDuration(2000);
animation.setRepeatCount(Animation.INFINITE);
animation.setInterpolator(interpolator);
v.startAnimation(animation);
}


知道CycleInterpolator是干嘛用的就简单了,Api demo里有它的用法,是个摇头效果!

//////////////////////////////////////////////////////////////////////////////

抖动:

    android:duration="180"
    android:fromDegrees="-2"
    android:pivotX="100%"
    android:pivotY="100%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="2" />

Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
shake.reset();
shake.setFillAfter(true);
image.startAnimation(shake);

摇头效果:
shake.xml







cycle_7.xml







代码实现:

/**
* 晃动动画
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}

你可能感兴趣的:(android动画&效果)