zoukankan      html  css  js  c++  java
  • 如何以特定的时间间隔为图片添加动画效果?

    本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解在Android中,如何以特定的时间间隔为图片添加动画效果。

    问:StacyM

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

    目前,我采用的代码是:

    XML:

    1
    2
    3
    4
    5
    6
    7
    8
    <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:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    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);
    }
    我应该采用什么算法,来完成左、右脚的淡入淡出的“运动”呢?需要设定一种循环运动的定时器吗?

    答:Nick Cardoso

    我觉得,你可以采用如下代码:

    Activity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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
    1
    2
    3
    4
    5
    <?xml version="1.0" encoding="utf-8"?>
        <translate android:fromYDelta="0" android:toYDelta="-15" android:duration="400"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="400" />
    </set>

    这是我认为最直接的解决办法,当然,你可以针对特定的需要,再完善以上的代码。

    原文链接:http://stackoverflow.com/questions/21667916/android-how-can-i-make-an-animation-of-an-image-occur-at-a-certain-interval 

  • 相关阅读:
    C#简单操作XML文件的增、删、改、查
    一个感觉还算可以的验证码生成程序
    安装aclocal1报错问题
    php中soap 的使用实例无需手写WSDL文件,提供自动生成WSDL文件类
    ofstream和ifstream详细用法[转]
    [原]C++ Soap客户端实例
    PHP中文件读、写、删的操作
    C++ Boost Thread 编程指南
    (转)虚函数和纯虚函数区别
    strcpy和memcpy的区别
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3697322.html
Copyright © 2011-2022 走看看