在对话框中为 ImageView 设置动画

Animate ImageView in dialog box

我正在尝试在 dialog 框内制作动画,这样看起来动画就在 activity 前面并在 2 秒后消失,但它总是崩溃并且只显示layout,不是动画,这是我的代码:

方法在我MainActivity.java

public void connectedAnim(){
    Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.connected);
    dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    IVcon = (ImageView)findViewById(R.id.IVcon);
    IVcon.setBackgroundResource(R.anim.connected);

    final AnimationDrawable animcon = (AnimationDrawable)IVcon.getDrawable();
    dialog.setCancelable(true);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            animcon.start();
        }
    });
    dialog.show();
}

layout/connected.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/IVcon"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

anim/connected.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/connected" android:duration="500"/>
    <item android:drawable="@drawable/disconnected" android:duration="500"/>

</animation-list>

LogCat

java.lang.NullPointerExceptionat com.ardudroid.sample.bluetoothswitch.MainActivity.onShow(MainActivity.java:205) at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1260) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5071) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method)

Line 205: animcon.start();

您必须在 ImageView.so 更改

中扩展对话框
  IVcon = (ImageView)findViewById(R.id.IVcon); 

进入

  IVcon = (ImageView) dialog.findViewById(R.id.IVcon);  

还有

阅读更多详情

试试这个

dialog.setContentView(R.layout.connected);
    ImageView IVcon = (ImageView)dialog.findViewById(R.id.IVcon);

getDrawable() is returning null. You can only use it to get a drawable if you previously assigned a drawable to it. Unfortunately, you only assigned a background drawable, which is not the same thing. Try using getBackground() 代替。

不过,我不确定您是否可以简单地在背景可绘制对象上调用 start()。

使用 dialog.setContentView 而不仅仅是 setContentView

anim/connected.xml移动到drawable/connected.xml并保持原样。

然后在你的 connectedAnim() 中,而不是

IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
IVcon.setBackgroundResource(R.anim.connected); 

使用

IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
IVcon.setBackgroundResource(R.drawable.connected);  

并使用这个

AnimationDrawable animcon = (AnimationDrawable) IVcon.getBackground();  

而不是

AnimationDrawable animcon = (AnimationDrawable)IVcon.getDrawable();

首先将 connected.xmlanim 移动到 drawable 文件夹。 我在边代码中评论的其他更改。

public void connectedAnim(){
       Dialog dialog = new Dialog(MainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.connected); // change to dialog.setContentView
            dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

            IVcon = (ImageView)dialog.findViewById(R.id.IVcon);

            IVcon.setBackgroundResource(R.drawable.connected);// Drawable file instead of anim move same file from anim folder to Drawable before use..

            final AnimationDrawable animcon = (AnimationDrawable) IVcon.getBackground(); // instead of getDrawable() use this
            dialog.setCancelable(true);

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    animcon.start();
                }
            });
            dialog.show();
}