简体   繁体   English

Animation 无法正常工作

[英]Animation not working correctly

When the phone is shaked it is supposed to display an animation.当手机摇晃时,它应该显示 animation。 The animation works the first time but the next shake it wont do it. animation 第一次工作,但下一次摇晃就不行了。 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.它只是 animation 在第一次之后不会再这样做了。 I do have the animation set for oneshot but that shouldnt effect the animation from triggering again?我确实为一次性设置了 animation 但这不应该影响 animation 再次触发?

Here is the activity I'm working with this on.这是我正在处理的活动。 Followed by its xml layout.其次是其 xml 布局。

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.这就是我反复将动画应用于 TextView 元素的方式。

Failing that, because you have changed the ImageView, perhaps you might need a v.postInvalidate() afterwards?做不到这一点,因为你已经改变了 ImageView,也许你之后可能需要一个 v.postInvalidate() ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM