Android - 在动画结束时删除View

时间:2015-01-27 21:42:55

标签: java android animation user-interface

嘿,我有一个方法可以将TextView添加到我的FrameLayout并启动动画。应该能够更频繁地调用该方法(对于多个动画)。这样可以正常工作,因为我可以同时运行多个动画,但我想在动画结束时删除动画视图。

public void showMoneyAnimation(int amoutAddedOrRemoved){
    //Adding Money
        float textSize = Math.round((global.getDisplaySize().y / scaleValue) * 34.0);
        final TextView temp = new TextView(context);
        temp.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
        temp.setText("+ " + amoutAddedOrRemoved + "$");
        temp.setTextAppearance(context, R.style.tsMoney1);
        temp.setTypeface(global.getFont("Grobold"));
        temp.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        sb_money.measure(0, 0);
        temp.measure(0,0);
        int xtemp = ((int) sb_money.getX() + sb_money.getMeasuredWidth()) - temp.getMeasuredWidth();
        temp.setX(xtemp);
        temp.setY(0);

        //Adding My Text to my FrameLayout
        bgFl.addView(temp);

        Animation atemp = AnimationUtils.loadAnimation(context, R.anim.addanim);
        temp.startAnimation(atemp);
        atemp.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // This is the problem's origin!
                bgFl.removeView(temp);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

但是我得到NullPointerException,可能是因为视图名称相同? 我该如何解决这个问题?请帮助:)

这里是例外:

01-27 22:52:25.633  25975-25975/de.cookedapps.needforweed E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.cookedapps.needforweed, PID: 25975
java.lang.NullPointerException
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3146)
        at android.view.View.getDisplayList(View.java:14283)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3284)
        at android.view.View.getDisplayList(View.java:14225)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3284)
        at android.view.View.getDisplayList(View.java:14225)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3284)
        at android.view.View.getDisplayList(View.java:14225)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3284)
        at android.view.View.getDisplayList(View.java:14225)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3284)
        at android.view.View.getDisplayList(View.java:14225)
        at android.view.View.getDisplayList(View.java:14330)
        at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1588)
        at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1467)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2754)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2620)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2188)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:573)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您可能会遇到以这种方式删除视图的问题,因为您应该只尝试从UI线程触摸任何视图(以避免其他线程同时尝试更改UI元素导致问题)。出于这个原因,Android提供了一个名为runOnUiThread()的方法,它将指示您的代码在正确的线程中执行操作。

这样的事情会让你指向正确的方向。将其放在onAnimationEnd方法中。

//get the parentView...
parentView.post(new Runnable() {
    public void run () {
        // it works without the runOnUiThread, but all UI updates must 
        // be done on the UI thread
        activity.runOnUiThread(new Runnable() {
            public void run() {
                parentView.removeView(view);
            }
        });
    }
}

答案 1 :(得分:0)

试试这个:

  1. 确保bgFl不为空
  2. 您可以尝试:temp.setVisibility(View.GONE)而不是删除
  3. 或者

    1. 使用处理程序在UI线程中安全地删除视图。