2

我正在做一个类似于算盘的时钟。我的问题是我无法理解为什么我的图像每秒都会重置,而不是继续向右。我的想法是我可能需要使用不同类型的动画?到目前为止,这是我的代码:

public class MainActivity extends Activity implements AnimationListener {
Button start, stop;

TextView time;

LinearLayout layout;

Animation movement;

private Handler mHandler = new Handler();
private long startTime;
private long elapsedTime;
private final int REFRESH_RATE = 1000;
private String seconds;
private long secs;
private boolean stopped = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start = (Button) findViewById(R.id.buttonStart);

    start.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Start animation on each image

            if(stopped){
                startTime = System.currentTimeMillis() - elapsedTime;
            }
            else{
                startTime = System.currentTimeMillis();
            }
            mHandler.removeCallbacks(startTimer);
            mHandler.postDelayed(startTimer, 0);

        }

    });

    stop = (Button) findViewById(R.id.buttonStop);

    stop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            mHandler.removeCallbacks(startTimer);
            stopped = true;

        }

    });

    time = (TextView) findViewById(R.id.textTime);

    // Get the layouts
    layout = (LinearLayout) findViewById(R.id.linearLayout1);

    // Create animation for right image
    movement = AnimationUtils.loadAnimation(this, R.layout.animation_test);
    movement.setAnimationListener(this);   

    }

// Listen for animations to be finished
// There are more efficient ways, I'm just being lazy.
public void onAnimationEnd(Animation animation) {
    // or do whatever it is you wanted to do here, like launch another activity?
} 

public void onAnimationRepeat(Animation animation) {    
} 

public void onAnimationStart(Animation animation) { 
} 

private Runnable startTimer = new Runnable() {
       public void run() {
           elapsedTime = System.currentTimeMillis() - startTime;
           update(elapsedTime);
           mHandler.postDelayed(this,REFRESH_RATE);
        }
    };

private void update (float time){
    secs = (long)(time/1000);

    layout.startAnimation(movement);

    secs = secs % 60;
    seconds=String.valueOf(secs);
    if(secs == 0){
        seconds = "00";
    }
    if(secs <10 && secs > 0){
        seconds = "0"+seconds;
    }

    /* Setting the timer text to the elapsed time */
    ((TextView)findViewById(R.id.textTime)).setText(seconds);
}
}

我的animation.xml如下

<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromXDelta="0" 
 android:toXDelta="15%p" 
 android:fromYDelta="0"
 android:toYDelta="0%" 
 android:duration="500"
 android:zAdjustment="top"/>

我处于停滞状态,一直在尝试通读开发部分,但我找不到正确的解决方案,或者必须忽略某些东西。

4

2 回答 2

1

尝试添加:

android:fillEnabled="true"
android:fillAfter="true"

到你的动画。

于 2012-11-15T16:18:17.660 回答
-1

如果您的目标是 Android 3.0 或更高版本,则可以改用新的动画框架:http: //developer.android.com/guide/topics/graphics/prop-animation.html

于 2012-11-15T16:37:26.337 回答