我正在制作一款安卓应用程序,其中有一个按钮可以进入消息界面。在该按钮所在的活动页面上,我会检查是否有未读消息,如果有,我希望对按钮进行一些操作,以便提醒用户有未读消息。
我想让按钮横向震动,每2到3秒钟震动3次左右。
我知道如何在后台运行一个线程,每x毫秒做一些事情。但是我不知道如何让它横向震动3次。
有人能帮忙吗?
我考虑使用正弦函数来进行动画设计,我可以利用正弦函数的输出值获取上下波动的数值,并将其设置为按钮的水平位置... 但这似乎过于复杂了,还有更好的方法吗?
我正在制作一款安卓应用程序,其中有一个按钮可以进入消息界面。在该按钮所在的活动页面上,我会检查是否有未读消息,如果有,我希望对按钮进行一些操作,以便提醒用户有未读消息。
我想让按钮横向震动,每2到3秒钟震动3次左右。
我知道如何在后台运行一个线程,每x毫秒做一些事情。但是我不知道如何让它横向震动3次。
有人能帮忙吗?
我考虑使用正弦函数来进行动画设计,我可以利用正弦函数的输出值获取上下波动的数值,并将其设置为按钮的水平位置... 但这似乎过于复杂了,还有更好的方法吗?
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);
如果你想要垂直动画,请将 fromXdelta 和 toXdelta 的值改为 fromYdelta 和 toYdelta 的值。
@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 -> New -> Directory
命名新目录为anim
创建一个名为milkshake的新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" />
将以下代码添加到根目录的build.gradle文件末尾的repositories中:
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();
在 XML 中定义一个 View:
val playDel = findViewById<ImageView>(R.id.player_del)
在 Android Lib 中找到一个动画:
val imgAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out)
连接到视图
playDel.animation=imgAnim
首先创建 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" />
Class.java
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);
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);