具有BroadcastReceiver的Android模拟观看秒针

时间:2014-10-16 02:48:19

标签: java android wear-os

我无法弄清楚为什么startAnimation();没有开火。这曾经在Runnable上运行,但我已经转移到BroadcastReceiver。这与我之前提出的问题有关:

Android Wear not waking screen

import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.widget.TextView;
import android.content.BroadcastReceiver;
import java.util.Calendar;

public class ClockActivity extends WatchFaceActivity {

private ImageView img;

private final static IntentFilter INTENT_FILTER;
static {
    INTENT_FILTER = new IntentFilter();
    INTENT_FILTER.addAction(Intent.ACTION_TIME_TICK);
    INTENT_FILTER.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    INTENT_FILTER.addAction(Intent.ACTION_TIME_CHANGED);
}


private BroadcastReceiver mTimeInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
        updateUI();
    }
};


@Override
public void onScreenDim() {
    updateUI();
}

@Override
public void onScreenAwake() {
    updateUI();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clock);
    updateUI();

    mTimeInfoReceiver.onReceive(ClockActivity.this, null);
    registerReceiver(mTimeInfoReceiver, INTENT_FILTER);

}

private void updateUI() {
    Calendar calendar = Calendar.getInstance();
    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minutes = calendar.get(Calendar.MINUTE);
    int seconds = calendar.get(Calendar.SECOND);

    img = (ImageView)findViewById(R.id.hand_second);
    RotateAnimation rotateAnimation = new RotateAnimation((seconds-1)*6, seconds*6, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    rotateAnimation.setInterpolator(new LinearInterpolator());
    rotateAnimation.setDuration(1000);
    rotateAnimation.setFillAfter(true);

    img.startAnimation(rotateAnimation);
}

}

1 个答案:

答案 0 :(得分:2)

而不是使用RotateAnimation,而是使用ViewPropertyAnimators。它们更高效,更易于使用。有些事情应该有效:

imageView.animate()
    .setDuration(1000)
    .rotation((360f / 60f) * calendar.get(Calendar.SECOND))
    .setInterpolator(new LinearInterpolator());