Home  >  Q&A  >  body text

android - 如何以特定的时间间隔为图片添加动画效果?

问:StacyM
在Android的开发过程中,我用的是Coursera类别,以下是我想要实现的静态效果图:

目前,我采用的代码是:
XML:

<Button
        android:id="@+id/startbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leftfoot"
        android:layout_alignRight="@+id/leftfoot"
        android:onClick="startRhythmandAnimation"
        android:text="@string/start_button" />

Java:

public class Assignment3MainActivity extends Activity {

private View mMileTimeGoal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assignment3_main);
    mMileTimeGoal = findViewById(R.id.miletimegoal);
}

public void startRhythmandAnimation () {
    String MileTime = mMileTimeGoal.getContext().toString();
    String[] time_array = MileTime.split(":");
    int hours = Integer.parseInt(time_array[0]);
    int minutes = Integer.parseInt(time_array[1]);
    int seconds = Integer.parseInt(time_array[2]);
    int duration = 3600 * hours + 60 * minutes + seconds;
    int steps_per_second = 3;

    int running_rate = duration * steps_per_second;

    View rightfoot = findViewById(R.id.rightfoot);
    View leftfoot = findViewById(R.id.leftfoot);

    rightfoot.setVisibility(View.VISIBLE);
    Animation anim = AnimationUtils.makeInChildBottomAnimation(this);
    rightfoot.startAnimation(anim);

    leftfoot.setVisibility(View.VISIBLE);
    leftfoot.startAnimation(anim);
}

我应该采用什么算法,来完成左、右脚的淡入淡出的“运动”呢?需要设定一种循环运动的定时器吗?

原问题:Android - How can I make an animation of an image occur at a certain interval?

阿神阿神2766 days ago789

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:31:16

    Answer: Nick Cardoso
    I think you can use the following code:
    Activity

    private Handler mHandler;    
    private long mInterval = 1000;
    private View mLeftfoot;
    private Animation mFootAnim;
    
    public void onCreate(Bundle bundle) {
       ...
       mHandler = new Handler(); //.os package class when importing
       mLeftfoot = findViewById(R.id.leftfoot);
       mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);
       stepRecursive();
    }
    
    private void stepRecursive() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLeftFoot.startAnimation(mFootAnim );
                stepRecursive();
            }
        }, mInterval);
    }
    

    /res/anim/foot.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="-15" android:duration="400"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="400" />
    </set>
    

    This is the most direct solution in my opinion. Of course, you can improve the above code according to specific needs.

    reply
    0
  • Cancelreply