如何在android中设置按钮动画?
我正在制作一个安卓应用程序,我有一个按钮,可以指向一个消息传递的地方。在使用按钮的活动中,我检查是否有未读消息,如果有,我想对按钮做一些操作,让用户知道有未读消息 我在考虑让按钮水平振动,就像每2秒或3秒振动3次一样 我知道如何在后台运行线程,该线程每x毫秒执行一次操作。但我不知道该怎么做,就是水平摇晃3次 有人能帮忙吗如何在android中设置按钮动画?,android,animation,button,shake,Android,Animation,Button,Shake,我正在制作一个安卓应用程序,我有一个按钮,可以指向一个消息传递的地方。在使用按钮的活动中,我检查是否有未读消息,如果有,我想对按钮做一些操作,让用户知道有未读消息 我在考虑让按钮水平振动,就像每2秒或3秒振动3次一样 我知道如何在后台运行线程,该线程每x毫秒执行一次操作。但我不知道该怎么做,就是水平摇晃3次 有人能帮忙吗 我在考虑使用sin函数,对于动画,我可以使用sin函数的输出来获得上下的值,我可以设置按钮的水平位置。。。但这似乎太极端了,有更好的方法吗?在anim文件夹中创建shake.x
我在考虑使用sin函数,对于动画,我可以使用sin函数的输出来获得上下的值,我可以设置按钮的水平位置。。。但这似乎太极端了,有更好的方法吗?在anim文件夹中创建shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle" />
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="4" />
如果您想要垂直动画,请将xDelta和toXdelta值更改为fromYdelta和toYdelta值我不能对@omega的评论发表评论,因为我没有足够的声誉,但这个问题的答案应该是: shake.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100" <!-- how long the animation lasts -->
android:fromDegrees="-5" <!-- how far to swing left -->
android:pivotX="50%" <!-- pivot from horizontal center -->
android:pivotY="50%" <!-- pivot from vertical center -->
android:repeatCount="10" <!-- how many times to swing back and forth -->
android:repeatMode="reverse" <!-- to make the animation go the other way -->
android:toDegrees="5" /> <!-- how far to swing right -->
<?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="10"
android:repeatMode="reverse"
android:toDegrees="5" />
这只是做你想做的事情的一种方法,可能有更好的方法
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class HeightAnimation extends Animation {
protected final int originalHeight;
protected final View view;
protected float perValue;
public HeightAnimation(View view, int fromHeight, int toHeight) {
this.view = view;
this.originalHeight = fromHeight;
this.perValue = (toHeight - fromHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (originalHeight + perValue * interpolatedTime);
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
}
uss to:
HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight());
heightAnim.setDuration(1000);
view.startAnimation(heightAnim);
Class.Java
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_the_button);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.milkshake);
Button myButton = (Button) findViewById(R.id.new_game_btn);
myButton.setAnimation(myAnim);
}
点击一下按钮
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(myAnim);
}
});
在res目录中创建anim文件夹 右键单击,res->New->Directory 命名新目录anim 创建一个新的xml文件名itmilkshake
milkshake.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100" <!-- how long the animation lasts -->
android:fromDegrees="-5" <!-- how far to swing left -->
android:pivotX="50%" <!-- pivot from horizontal center -->
android:pivotY="50%" <!-- pivot from vertical center -->
android:repeatCount="10" <!-- how many times to swing back and forth -->
android:repeatMode="reverse" <!-- to make the animation go the other way -->
android:toDegrees="5" /> <!-- how far to swing right -->
<?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="10"
android:repeatMode="reverse"
android:toDegrees="5" />
依赖关系 将其添加到存储库末尾的root build.gradle中:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}}
然后添加依赖项
依赖关系{
编译'com.github.varunest:sparkbutton:1.0.5'
}
用法
XML
你想要动画还是按键效果?这只会使它向右移动
,然后立即回到原来的位置4次,但我如何让它向右移动,然后向左移动,然后右
然后左
像震动效果一样回到原来的位置?如何让这个动画持续发生?这个答案对我在android中实现类似震动的动画很有帮助。嘿@RVG谢谢你的代码,但是我如何改变震动速度?我如何水平摇晃它?
SparkButton button = new SparkButtonBuilder(context)
.setActiveImage(R.drawable.active_image)
.setInActiveImage(R.drawable.inactive_image)
.setDisabledImage(R.drawable.disabled_image)
.setImageSizePx(getResources().getDimensionPixelOffset(R.dimen.button_size))
.setPrimaryColor(ContextCompat.getColor(context, R.color.primary_color))
.setSecondaryColor(ContextCompat.getColor(context, R.color.secondary_color))
.build();