android中的抖动/抖动视图动画
我创建了一个anim.xml文件,如下所示,它可以像android中的IOS图标一样摇动imageview。 然而,它并没有给我同样的结果。 还有更好的办法吗android中的抖动/抖动视图动画,android,android-animation,Android,Android Animation,我创建了一个anim.xml文件,如下所示,它可以像android中的IOS图标一样摇动imageview。 然而,它并没有给我同样的结果。 还有更好的办法吗 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromDegrees="-2
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromDegrees="-2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="2" />
您可以尝试以下方法:
shake.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle_7" />
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toDegrees="5" />
<translate
android:duration="70"
android:fromXDelta="-10"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10" />
</set>
cycle_7.xml
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />
尝试设置
android:repeatMode=“reverse”
。下面的动画非常合理地模拟了我的银河系。显然,您可以根据自己的喜好微调参数
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="5" />
IOS抖动动画并不是那么简单,旋转时尝试随机更改轴x和y。不过,您应该通过编程更改该值。可能您也可以同时使用翻译动画尝试使用此动画:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="@android:anim/linear_interpolator"
android:toDegrees="5" />
<translate
android:fromXDelta="-10"
android:toXDelta="10"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="@android:anim/linear_interpolator"
android:duration="70" />
</set>
我在Android上创建了一个震动效果,并发布在GitHub上。看看是否效果更好 相关代码:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/overshoot_interpolator"
android:fillAfter="true">
<translate
android:startOffset="100"
android:fromXDelta="0%p"
android:toXDelta="10%p"
android:duration="50" />
<translate
android:startOffset="150"
android:fromXDelta="0%p"
android:toXDelta="-25%p"
android:duration="110" />
<translate
android:startOffset="260"
android:fromXDelta="0%p"
android:toXDelta="25%p"
android:duration="120" />
<translate
android:startOffset="380"
android:fromXDelta="0%p"
android:toXDelta="-20%p"
android:duration="130" />
<translate
android:startOffset="510"
android:fromXDelta="0%p"
android:toXDelta="10%p"
android:duration="140" />
</set>
我的头撞了两个多小时,我知道如何摇晃一个视图 不幸的是,除了片段的onCreateView之外,接受的答案将不起作用 例如,如果您有onClick方法及其内部。你有下面这样的动画,它不会工作 请把代码通读一遍
DoneStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
register(view);
}
});
register方法有如下一些检查代码
private void register(View view) {
String type = typedThings.getText.toString();
String km = Km_Now.getText().toString();
if (serviceType == null) {
animationServiceList = AnimationUtils.loadAnimation(getActivity(), R.anim.shake_wobble);
silverServiceButton.setAnimation(animationServiceList);
generalServiceButton.setAnimation(animationServiceList);
platinumServiceButton.setAnimation(animationServiceList);
animationServiceList.start();
} else if (km == null) {
animationEditText = AnimationUtils.loadAnimation(getActivity(), R.anim.shake_wobble);
Km_Now.setAnimation(animationEditText);
animationEditText.start();
}
调用animationServiceList.start();永远不会被称为
解决方案:使用类似于ObjectAnimator的PropertyAnimator。漂亮的抖动动画 res/anim/shake.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toDegrees="5" />
<translate
android:duration="70"
android:fromXDelta="-10"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10" />
</set>
如何使用它(更简单的版本):
btn_done.startAnimation(AnimationUtils.loadAnimation(this,R.anim.shake));
TextView tv;
makeMeShake(tv,20,5); // it will shake quite fast
myView.startAnimation(AnimationUtils.loadAnimation(view!!.context, R.anim.shake))
myView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake))
使震动效果像这样 首先将anim文件夹中的shake动画定义为shake.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toDegrees="5" />
<translate
android:duration="70"
android:fromXDelta="-10"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10" />
</set>
然后在代码中
if (TextUtils.isEmpty(phone.getText())
|| phone.getText().length() < 10)
{
//shake animation
phone.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.shake));
}
if(TextUtils.isEmpty(phone.getText())
||phone.getText().length()<10)
{
//抖动动画
phone.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.shake));
}
其他答案也都是正确的,但这比它们要平滑一些,因为它使用插值器来生成平滑的数字,用于前后移动
public class WobblyView extends ImageView implements ValueAnimator.AnimatorUpdateListener {
private final ValueAnimator va = ValueAnimator.ofInt(-10, 10);
public WobblyView(Context context) {
this(context, null);
}
public WobblyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WobblyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setAdjustViewBounds(true);
setImageResource(R.drawable.ic_logo);
va.setInterpolator(new AccelerateDecelerateInterpolator());
va.setRepeatMode(ValueAnimator.REVERSE);
va.setRepeatCount(ValueAnimator.INFINITE);
va.setDuration(1000);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
va.addUpdateListener(this);
va.start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
va.removeUpdateListener(this);
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int heading = (int) animation.getAnimatedValue();
setRotation(heading);
}
}
我创建了一个非常好的近似iOS震动(当你长按图标从主屏幕上删除应用程序时)。您必须以编程方式在代码内部应用,因为它需要生成随机数:
int dur1 = 70 + (int)(Math.random() * 30);
int dur2 = 70 + (int)(Math.random() * 30);
// Create an animation instance
Animation an = new RotateAnimation(-3, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// Set the animation's parameters
an.setDuration(dur1); // duration in ms
an.setRepeatCount(-1); // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE);
an.setFillAfter(true); // keep rotation after animation
// Create an animation instance
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);
// Set the animation's parameters
an2.setDuration(dur2); // duration in ms
an2.setRepeatCount(-1); // -1 = infinite repeated
an2.setRepeatMode(Animation.REVERSE);
an2.setFillAfter(true); // keep rotation after animation
AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(an);
s.addAnimation(an2);
// Apply animation to image view
itemView.setAnimation(s);
此代码被设计为应用于适配器的gridview(getView)中,但您可以通过将最后一行更改为:
yourViewName.setAnimations 这一个作为iOS“不正确PIN”的克隆非常有效(尽管不是完美的):
final float FREQ = 3f;
final float DECAY = 2f;
// interpolator that goes 1 -> -1 -> 1 -> -1 in a sine wave pattern.
TimeInterpolator decayingSineWave = new TimeInterpolator() {
@Override
public float getInterpolation(float input) {
double raw = Math.sin(FREQ * input * 2 * Math.PI);
return (float)(raw * Math.exp(-input * DECAY));
}
};
shakeField.animate()
.xBy(-100)
.setInterpolator(decayingSineWave)
.setDuration(500)
.start();
使用:
btn_done.startAnimation(AnimationUtils.loadAnimation(this,R.anim.shake));
TextView tv;
makeMeShake(tv,20,5); // it will shake quite fast
myView.startAnimation(AnimationUtils.loadAnimation(view!!.context, R.anim.shake))
myView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake))
对于Kotlin用户:
首先创建一个名为shake.xml的动画资源文件。右键单击Android Studio中的res文件夹,然后单击New>Android资源文件>输入shake作为文件名,并选择Animation作为资源类型下拉列表。单击“确定”
在shake.xml内部粘贴以下内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200"
android:fromXDelta="-5%"
android:repeatCount="3"
android:repeatMode="reverse"
android:toXDelta="5%"/>
</set>
在活动中:
btn_done.startAnimation(AnimationUtils.loadAnimation(this,R.anim.shake));
TextView tv;
makeMeShake(tv,20,5); // it will shake quite fast
myView.startAnimation(AnimationUtils.loadAnimation(view!!.context, R.anim.shake))
myView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake))
(注意-myView
是指定给要设置动画的视图的ID)
如果要微调动画,只需修改shake.xml
Kotlin版本的answer中的值即可
嘿,我想cycle_7.xml
可以替换为android:repeatCount=“7”
中的属性
我是android开发新手,我使用的是Xamarin,请告诉我如何将此动画应用于任何事件(如单击等)上的图像/按钮@N.K:其工作原理应与Java中的相同:通过其资源ID加载动画,并通过将其作为参数传递给视图来启动动画。应该类似于:view.StartAnimation(AnimationUtils.LoadAnimation(Resource.Animation.wobble))
。更多细节和细节。我们如何增加震动之间的延迟。例如,显示抖动500毫秒,然后延迟1000毫秒,然后再次重复?对我来说,这是最好的答案,因为它从中心开始设置动画,不会像其他建议那样立即偏移图像。赞成的意见!似乎什么都没有请记住,只有当itemView
尚未添加到ViewGroup时,这才有效。如果已经添加了,请使用itemView.startAnimation
。它可以完美且非常快速地工作。android:duration=“50”
在我看来看起来不那么起伏不定,看起来更好startAnimation
和anim
(在R.anim.shake
中)都是未解决的引用,请您提供帮助?感谢您需要在res目录@Maff中创建动画文件夹