最近做音乐播放相关的开发,需要
实现一个较为简单的图片转动的一个功能,同时转动的时候在
暂停播放的时候,需要
停止在当前转动到的角度,所以使用了ObejctA
nima
tor来
实现,记录下
实现的方法:
ObjectA
nima
tor mMusicA
nimation =
ObjectA
nima
tor.ofFloat(mMusicImage, "rotation", 0f,360f);
ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能。这使得为任何对象设置动画更加的容易。你不再需要实现 ValueAnimator.AnimatorUpdateListener接口,因为ObjectAnimator动画自己会自动更新相应的属性值。
ObjectAnimator的实例和ValueAnimato
<ProgressBar
android:id="@+id/progress_voice"
style="?
android:attr/progressBarStyleHorizontal"
android:layout_centerInParent="true"
private TextView timerTextView;
private Button startButton, pauseButton, resumeButton, resetButton;
private Handler handler;
private long startTime, timeInMilliseconds = 0;
private boolean timerRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerTextView = findViewById(R.id.timerTextView);
startButton = findViewById(R.id.startButton);
pauseButton = findViewById(R.id.pauseButton);
resumeButton = findViewById(R.id.resumeButton);
resetButton = findViewById(R.id.resetButton);
handler = new Handler();
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerRunning) {
startTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
timerRunning = true;
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timerRunning) {
handler.removeCallbacks(runnable);
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
timerRunning = false;
resumeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerRunning) {
startTime = SystemClock.uptimeMillis() - timeInMilliseconds;
handler.postDelayed(runnable, 0);
timerRunning = true;
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerRunning) {
timeInMilliseconds = 0;
timerTextView.setText("00:00:00");
private Runnable runnable = new Runnable() {
@Override
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
int seconds = (int) (timeInMilliseconds / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (timeInMilliseconds % 1000);
timerTextView.setText(String.format("%02d:%02d:%03d", minutes, seconds, milliseconds));
handler.postDelayed(this, 0);
这个代码包含了开始、
暂停、
继续和重置按钮,可以
实现秒表的基本功能。