[英]How to animate button in android?
我正在制作一个 android 应用程序,并且我有一个通向消息传递位置的按钮。 在带有按钮的活动中,我检查是否有任何未读消息,如果有,我想对按钮执行一些操作以让用户知道有未读消息。
我正在考虑让按钮水平振动,例如每 2 或 3 秒振动 3 次。
我知道如何在后台运行一个线程,它每 x 毫秒执行一次。 但是我不知道该怎么做就是水平摇晃它3次。
有人能帮忙吗?
我正在考虑使用 sin 函数,对于动画,我可以使用 sin 函数的输出来获取上下的值,我可以设置按钮的水平位置......但这似乎太极端了,是有更好的方法吗?
我不能对@ 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 -->
Class.java
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);
这只是做你想做的事的一种方式,可能有更好的方法。
在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" />
和anim文件夹中的cycle.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="4" />
现在在代码上添加动画
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
anyview.startAnimation(shake);
如果需要垂直动画,请将Xdelta和Xdelta值更改为fromYdelta和Ydelta值
@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); }
对于按钮的onClick
myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(myAnim); } });
在res目录中创建anim文件夹
右键单击, res - >新建 - >目录
将新目录命名为anim
创建一个新的xml文件名,即奶昔
milkshake.xml
<?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;
}
}
你要:
HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight());
heightAnim.setDuration(1000);
view.startAnimation(heightAnim);
将其添加到存储库末尾的根build.gradle中:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}}
然后添加依赖项dependencies { compile 'com.github.varunest:sparkbutton:1.0.5' }
<com.varunest.sparkbutton.SparkButton
android:id="@+id/spark_button"
android:layout_width="40dp"
android:layout_height="40dp"
app:sparkbutton_activeImage="@drawable/active_image"
app:sparkbutton_inActiveImage="@drawable/inactive_image"
app:sparkbutton_iconSize="40dp"
app:sparkbutton_primaryColor="@color/primary_color"
app:sparkbutton_secondaryColor="@color/secondary_color" />
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();
在Kotlin<\/strong>中,这种方式在 XML 之后:
定义视图:
val playDel = findViewById<ImageView>(R.id.player_del)
首先创建shake.xml
<?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" />
类.java
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.