When the phone is shaked it is supposed to display an animation. The animation works the first time but the next shake it wont do it. Everything else is working correctly because every time I shake it it display the new text each time. Its just the animation wont do it again after the first. I do have the animation set for oneshot but that shouldnt effect the animation from triggering again?
Here is the activity I'm working with this on. Followed by its xml layout.
public class Ask extends Activity{
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
final ImageView v = (ImageView)findViewById(R.id.talk);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
v.setBackgroundResource(R.anim.budtalk);
AnimationDrawable talking = (AnimationDrawable)v.getBackground();
开发者_如何学运维 talking.start();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/page1"
>
<ImageView android:background="@drawable/page2ani1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/talk"></ImageView>
</RelativeLayout>
Does this work?
final Animation ani = AnimationUtils.loadAnimation(this,R.anim.budtalk);
//later
v.startAnimation(ani);
This is how I have applied animations repeatedly to TextView elements.
Failing that, because you have changed the ImageView , perhaps you might need a v.postInvalidate() afterwards?
精彩评论