动画在Dialog中不起作用

时间:2017-01-12 12:04:07

标签: android android-animation android-alertdialog

当我启动我的应用程序时,会出现一个对话框,在此对话框中我想要一个动画!但动画(讲话的巫师)不起作用。 这是我的MainActivity

public class MainActivity extends AppCompatActivity {

SharedPreferences mPrefs;

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

    Context mContext = this.getApplicationContext();
    mPrefs = mContext.getSharedPreferences("myAppPrefs", 0);        

    if(getFirstRun()) {

        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.activity_main2);

        setRan();
        dialog.show();
    }
}

public boolean getFirstRun() {
    return mPrefs.getBoolean("firstRun", true);
}

public void setRan() {
    SharedPreferences.Editor edit = mPrefs.edit();
    edit.putBoolean("firstRun", false);
    edit.apply();
}
}

这是我的Main2Activity

public class Main2Activity extends AppCompatActivity {

private ImageView img;

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

    img = (ImageView) findViewById(R.id.img);

    img.post(new Runnable() {
        @Override
        public void run() {
            ((AnimationDrawable) img.getBackground()).start();
        }
    });
}

}

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

添加:

   dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

left_slide.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p" android:toXDelta="0"
android:duration="500" />

right_slide.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
样式文件中的

。 DilaogAnimation

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/left_slide</item>
    <item name="android:windowExitAnimation">@anim/right_slide</item>
</style>