Android 滑动时钟-平移图像,但它会在时钟继续之前重置平移
我正在做一个类似算盘的钟。我的问题是,我无法理解为什么我的图像每秒钟都会重置,而不是继续向右。我的想法是,我可能需要使用不同类型的动画?以下是我目前的代码:Android 滑动时钟-平移图像,但它会在时钟继续之前重置平移,android,clock,translate-animation,Android,Clock,Translate Animation,我正在做一个类似算盘的钟。我的问题是,我无法理解为什么我的图像每秒钟都会重置,而不是继续向右。我的想法是,我可能需要使用不同类型的动画?以下是我目前的代码: public class MainActivity extends Activity implements AnimationListener { Button start, stop; TextView time; LinearLayout layout; Animation movement; private Handler m
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);
}
}
public类MainActivity扩展活动实现AnimationListener{
按钮启动、停止;
文本查看时间;
线性布局;
动画运动;
私有处理程序mHandler=新处理程序();
私人长启动时间;
私密时间长;
私有最终整数刷新率=1000;
私有字符串秒;
私人长secs;
私有布尔停止=false;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
开始=(按钮)findViewById(R.id.buttonStart);
start.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
//在每个图像上启动动画
如果(停止){
startTime=System.currentTimeMillis()-elapsedTime;
}
否则{
startTime=System.currentTimeMillis();
}
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer,0);
}
});
停止=(按钮)findViewById(R.id.buttonStop);
stop.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
mHandler.removeCallbacks(startTimer);
停止=真;
}
});
时间=(TextView)findViewById(R.id.textTime);
//获取布局
布局=(线性布局)findViewById(R.id.linearLayout1);
//为右图像创建动画
movement=AnimationUtils.loadAnimation(这个,R.layout.animation\u测试);
movement.setAnimationListener(此);
}
//收听要完成的动画
//还有更有效的方法,我只是在偷懒。
onAnimationEnd上的公共无效(动画){
//或者做你想在这里做的任何事情,比如启动另一项活动?
}
公共无效onAnimationRepeat(动画){
}
onAnimationStart(动画)上的公共无效{
}
private Runnable startTimer=new Runnable(){
公开募捐{
elapsedTime=System.currentTimeMillis()-startTime;
更新(elapsedTime);
mHandler.postDelayed(这是刷新率);
}
};
私有无效更新(浮动时间){
秒=(长)(时间/1000);
布局。启动(移动);
秒=秒%60;
秒=字符串.valueOf(秒);
如果(秒=0){
秒数=“00”;
}
如果(秒0){
秒=“0”+秒;
}
/*将计时器文本设置为经过的时间*/
((TextView)findViewById(R.id.textTime)).setText(秒);
}
}
我的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"/>
我处于停顿状态,一直在努力阅读开发部分,我就是找不到正确的解决方案,或者一定忽略了一些东西。尝试添加:
android:fillEnabled="true"
android:fillAfter="true"
如果您的目标是Android 3.0或更高版本,您可以使用新的动画框架:根据文档,fillEnabled与fillAfter无关。你能解释一下这个解决方案而不是仅仅粘贴它吗?问题并没有说明他的目标SDK级别是什么。因此,我提供了一个解决方案,尽管有一个警告。