我创建了一个很好的iOS震动近似值(当您长按一个图标从主屏幕删除应用程序时)。您必须以编程方式应用代码内部,因为它需要生成随机数:
int dur1 = 70 + (int)(Math.random() * 30);
int dur2 = 70 + (int)(Math.random() * 30);
Animation an = new RotateAnimation(-3, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
an.setDuration(dur1);
an.setRepeatCount(-1);
an.setRepeatMode(Animation.REVERSE);
an.setFillAfter(true);
Animation an2 = new TranslateAnimation(-TranslateAnimation.RELATIVE_TO_SELF,0.02f,
TranslateAnimation.RELATIVE_TO_SELF,0.02f,
-TranslateAnimation.RELATIVE_TO_SELF,0.02f,
TranslateAnimation.RELATIVE_TO_SELF,0.02f);
an2.setDuration(dur2);
an2.setRepeatCount(-1);
an2.setRepeatMode(Animation.REVERSE);
an2.setFillAfter(true);
AnimationSet s = new AnimationSet(false);
s.addAnimation(an);
s.addAnimation(an2);
itemView.setAnimation(s);
该代码设计为应用于适配器的gridview(getView)内部,但是您可以通过将最后一行更改为以下内容来应用于任何视图:
yourViewName.setAnimations(s);