在AlertDialog中未调用ImageView上的StartAnimation

时间:2016-06-08 14:19:35

标签: android imageview android-animation alertdialog

我在StartAnimation方法上遇到了问题。当我试图在AlertDialog.Builder中的ImageView上启动动画(布局膨胀)时,动画正确启动,但在第一个“帧”之后它不会继续他的cicle。 这是我的代码:

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.loading_dialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setView(layout);


    ImageView img = (ImageView) layout.findViewById(R.id.imgLoadingDialog);
    Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake_animation);
    img.startAnimation(shake);

    return new AlertDialog.Builder(context)
            .setView(layout);

这是我的animation.xml

 <?xml version="1.0" encoding="utf-8"?>
 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="100"
   android:fromDegrees="-20"
   android:pivotX="50%"
   android:pivotY="50%"
   android:repeatCount="infinite"
   android:repeatMode="reverse"
   android:toDegrees="20" />

1 个答案:

答案 0 :(得分:0)

如果您使用DialogFragment,则必须覆盖onCreateDialog而不是onCreateView,如下所示:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View layout = getActivity().getLayoutInflater().inflate(R.layout.list_item, null);
        builder.setView(layout);
        ImageView img = (ImageView) layout.findViewById(R.id.iv);
        Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
        img.startAnimation(anim);
        return builder.create();
}