【发布时间】:2015-02-08 08:40:37
【问题描述】:
我希望在按下按钮时发生两件事 1. 两个图像应该一遍又一遍地播放,赋予它动画和非静态的外观。 2. 循环中的声音。
当按钮未按下且手指向上时 1.声音应该停止 2. 动画应该停止。
我现在使用的方法如代码所示:
package com.example.newanimation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MainActivity extends Activity {
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView rocketImage = (ImageView) findViewById(R.id.imageView1);
rocketImage.setBackgroundResource(R.drawable.ball_animation);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
}
XML 就像
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/ball01" android:duration="200" />
<item android:drawable="@drawable/ball02" android:duration="200" />
<item android:drawable="@drawable/ball03" android:duration="200" />
</animation-list>
我保留 android:oneshot="false"> 而不是 "true" 因为我希望动画来回播放而不会停止直到按下按钮,但我需要它在手指从按钮。
【问题讨论】:
标签: java android xml animation