如何从图像生成多脉冲动画

时间:2019-05-22 19:24:51

标签: android animation

[图像链接] https://media.giphy.com/media/LSRm7w1EKbrFxlFS3m/giphy.gif我创建了带有脉冲动画的样本。它按预期工作,但在生成多个脉冲时需要帮助。 以下是我使用的代码。 非常感谢您的帮助。

如图所示,我想在环膨胀时产生多个脉冲。

动画文件:pulse.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="200"
        android:repeatMode="reverse"
        android:toXScale="1.1"
        android:toYScale="1.1"
        />
</set>

activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/progressbar"
        android:text="2569**"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:max="100"
        android:progress="100"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_centerInParent="true"
        />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="2678**"
        tools:text="asdasdasdas"
        android:background="#07000000"
        android:textColor="#000000"
        android:textSize="20sp" />
</RelativeLayout>

Mainactivity.java:

val pulse = AnimationUtils.loadAnimation(this, R.anim.pulse)
        val textview = findViewById<View>(R.id.textview) as TextView
        val progressBar = findViewById<View>(R.id.progressbar) as ProgressBar progressBar.startAnimation(pulse)

1 个答案:

答案 0 :(得分:0)

尝试一下:

bounce.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="1000"
        android:fromXScale="0.8"
        android:toXScale="1"
        android:fromYScale="0.8"
        android:toYScale="1"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/progressbar"
        android:text="2569**"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:max="100"
        android:progress="100"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_centerInParent="true"
        />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="2678**"
        tools:text="asdasdasdas"
        android:background="#07000000"
        android:textColor="#000000"
        android:textSize="20sp" />
</RelativeLayout>

SpringBounceInterpolator

public class SpringBounceInterpolator implements android.view.animation.Interpolator {
    private double mAmplitude = 1;
    private double mFrequency = 10;

    public SpringBounceInterpolator(double amplitude, double frequency) {
        mAmplitude = amplitude;
        mFrequency = frequency;
    }

    public float getInterpolation(float time) {
        return (float) (-1 * Math.pow(Math.E, -time/ mAmplitude) *
                Math.cos(mFrequency * time) + 1);
    }
}
View view = findViewById(R.id.textview);
final Animation animation = AnimationUtils.loadAnimation(view.getContext(), R.anim.bounce);
        SpringBounceInterpolator interpolator = new SpringBounceInterpolator(0.2, 20);
        animation.setInterpolator(interpolator);
        view.startAnimation(animation);