6

嗨,我想创建一个图像并从下到上设置动画,并将 alpha 从 1.0 更改为 0.0。我有这段代码(我在 onCreate 方法中调用了 3 次以制作 3 个动画图像):

/**
 * Starts to make fog
 */
private void startFogGenerator() {

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            final ImageView img = new ImageView(Activity.this);

            boolean showN1 = r.nextBoolean();
            if (showN1) {
                img.setImageResource(R.drawable.nube_01);
            } else {
                img.setImageResource(R.drawable.nube_02);
            }

            Animation animation = AnimationUtils.loadAnimation(Activity.this, R.anim.translate_and_alpha);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    fogLayout.removeView(img);
                    fogLayout.invalidate();
                    // always true? Maybe but just check for for concurrency safe.
                    if (setFogN(false) < MAX_CLOUDS_ON_SCREEN) {
                        startFogGenerator();
                    }

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });


            int pxWidth = Tools.toPx(140, getResources()); // Width of layout container
            int paddingWidth = Tools.toPx(20, getResources());
            int x = r.nextInt(pxWidth - paddingWidth - paddingWidth) + paddingWidth;

            int range = Tools.toPx(50, getResources());

            int width;
            int height;
            if (showN1) { // nube01: 167x226
                width = r.nextInt(Tools.toPx(167, getResources()) - range) + range;
                height= r.nextInt(Tools.toPx(226, getResources()) - range) + range;
            } else { // nube01: 144x177
                width = r.nextInt(Tools.toPx(144, getResources()) - range) + range;
                height= r.nextInt(Tools.toPx(177, getResources()) - range) + range;
            }

            Log.d("Animation", "X: " + x + ", Width: " + width + ", Height: " + height);

            //img.setPadding(x, 0, 0, 0);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
            params.leftMargin = x;
            fogLayout.addView(img, params);
            img.setBackgroundColor(Color.argb(255, r.nextInt(255),r.nextInt(255),r.nextInt(255))); // just for debug
            img.startAnimation(animation);
            setFogN(true);

        }
    }, r.nextInt(2500 - 200) + 200);

}
private final static int MAX_CLOUDS_ON_SCREEN = 3;
private AtomicInteger currentFogs = new AtomicInteger(0);
private synchronized int setFogN(boolean increment) {

    int toReturn = increment ? currentFogs.incrementAndGet() : currentFogs.decrementAndGet();

    Log.d("TeaAnim", "ToReturn: "+ toReturn);

    return toReturn;

}

工作正常,但是当删除第三个视图并重新开始时,它会因 NullPointerException 而崩溃,可能是并发问题?我该如何解决?

例外:

ERROR/AndroidRuntime(10736): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2796)
    at android.view.View.getDisplayList(View.java:12648)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5204)
    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:799)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at dalvik.system.NativeStart.main(Native Method)

注意:fogLayout 是一个具有固定宽度和高度(以 dp 为单位)的 RelativeLayout,Tools.toPx 是一种从 DP 转换为 PX 的方法

4

2 回答 2

10

已解决,只需通知我正在删除视图的处理程序:

handler.post(new Runnable() {
    public void run() {
        fogLayout.removeView(img);
    }
});
于 2013-03-26T12:56:15.887 回答
0

由于我找不到如何发表评论,这是我的答案

  1. 如果你做了,你没有告诉我们

    相对布局雾布局;

    onCreate(Bundle savedInstance){fogLayout = (RelativeLayout) findViewById(R.id.idOfFogLayout); }

  2. 尚不清楚您的处理程序是否不为空;

  3. 您正在多次重用某些变量,为什么不定义一次,然后重用它们而不是每次都完全重新定义它们

  4. 而不是添加和删除视图,为什么不只是在对它们应用动画时将它们设置为不可见

于 2013-03-26T12:59:45.517 回答