Android绘制循环

时间:2014-04-23 14:59:52

标签: android view nullpointerexception countdowntimer ondraw

这是我用于动画绘制的类,它需要曲线的起点和终点以及要渲染的曲线数。

public class DrawIt extends View {
    Context context;
    int[] z;
    int[] k;
    Path[] patharr;
    Path[] drawarr;
    Paint paint = new Paint();
    Paint paint2 = new Paint();
    Paint paint3 = new Paint();
    Random r = new Random();
    float x1;
    float y1;
    float x3;
    float y3;
    float x2;
    float y2;
    int ref;
    public boolean finito = false;

    public DrawIt(Context context, int a, int b, int c, int d,
            final int num) {

        super(context);
        this.context = context;
        setBackgroundResource(com.example.test.R.drawable.endturn);

        this.ref = num;
        x1 = a;
        y1 = b;
        x3 = c;
        y3 = d;
        if (x1 < x3) {
            x2 = (x1 + x3) / 2 - 50;
        } else {
            x2 = (x1 + x3) / 2 + 50;
        }

        y2 = (y1 + y3) / 2 - 350;

        paint.setAntiAlias(true);
        paint.setAlpha(255);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        paint.setPathEffect(new DashPathEffect(new float[] { 2, 4 }, 50));

        paint2.setAntiAlias(true);
        paint2.setAlpha(55);
        paint2.setStrokeWidth(1);
        paint2.setColor(Color.WHITE);
        paint2.setStyle(Style.FILL);

        paint3.setAntiAlias(true);
        paint3.setAlpha(255);
        paint3.setStrokeWidth(2);
        paint3.setColor(Color.RED);
        paint3.setStyle(Style.STROKE);

        drawarr = new Path[num];
        patharr = new Path[num];
        z = new int[num];
        k = new int[num];
        for (int p = 0; p < num; p++) {
            k[p] = r.nextInt(50 - 1) - 1;
            z[p] = r.nextInt(50 - 1) - 1;
            patharr[p] = new Path();
            patharr[p].moveTo(x1, y1);
            patharr[p].quadTo(x2, y2, x3 + k[p], y3 + z[p]);

        }

        final long DRAW_TIME = 800;
        CountDownTimer timer = new CountDownTimer(DRAW_TIME, 100) {
            PathMeasure[] measarr = new PathMeasure[num];
            Path[] segmarr = new Path[num];
            float[] startarr = new float[num];
            float[] lengtharr = new float[num];

            @Override
            public void onTick(long millisUntilFinished) {

                for (int p = 0; p < num; p++) {
                    measarr[p] = new PathMeasure();
                    segmarr[p] = new Path();
                    measarr[p].setPath(patharr[p], false);
                    startarr[p] = 0;
                }
                float percent = ((float) (DRAW_TIME - millisUntilFinished))
                        / (float) DRAW_TIME;

                for (int p = 0; p < num; p++) {
                    lengtharr[p] = measarr[p].getLength() * percent;
                }

                for (int p = 0; p < num; p++) {
                    measarr[p].getSegment(startarr[p], lengtharr[p],
                            segmarr[p], true);
                }

                for (int p = 0; p < num; p++) {
                    startarr[p] = lengtharr[p];
                }

                for (int p = 0; p < num; p++) {
                    drawarr[p] = new Path();
                    drawarr[p].addPath(segmarr[p]);
                }

                invalidate();
            }

            @Override
            public void onFinish() {
                for (int p = 0; p < num; p++) {
                    measarr[p].getSegment(startarr[p], measarr[p].getLength(),
                            segmarr[p], true);
                }
                for (int p = 0; p < num; p++) {
                    drawarr[p].addPath(segmarr[p]);
                }
                if (measarr[0].getSegment(startarr[0], measarr[0].getLength(),
                        segmarr[0], false)) {
                    finito = true;
                }
                invalidate();
            }
        };
        timer.start();

    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("Costruttore", "" + drawarr[0]);

        for (int p = 0; p < drawarr.length; p++) {
            canvas.drawPath(drawarr[p], paint);
        }

        if (finito == true) {
            for (int m = 0; m < drawarr.length; m++) {
                canvas.drawCircle(x3 + k[m], y3 + z[m], 10, paint2);
                canvas.drawCircle(x3 + k[m], y3 + z[m], 10, paint3);
            }

        }

    }
}
在对游戏进行结束转弯计算之后,在CountDownTimer中以这种方式调用DrawIt:

@Override
            public void onTick(long millisUntilFinished) {
                if (l < players.length) {
                    while (l < players.length && players[l].mess.equals(".")) {
                        l++;
                        }
                    if (l >= players.length) {
                        this.cancel();
                    } else {
                        azioni.setText(players[l].mess);
                        players[l].vista = new DrawIt(TurnActivity.this, x1,y1,x2,y2);
                        addContentView(players[l].vista, layout);
                        l++;

                    }
                }
            }

这导致Ondraw上出现NullPointerException。我不明白为什么。我是Android的新手,也许这可能是了解更多关于View和使用CountDownTimer制作的动画的机会。

这是Logcat:

04-23 16:00:26.175: W/dalvikvm(1135): threadid=1: thread exiting with uncaught exception (group=0xa4dcab20)
04-23 16:00:26.179: E/AndroidRuntime(1135): FATAL EXCEPTION: main
04-23 16:00:26.179: E/AndroidRuntime(1135): Process: com.example.test, PID: 1135
04-23 16:00:26.179: E/AndroidRuntime(1135): java.lang.NullPointerException
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.GLES20Canvas.drawPath(GLES20Canvas.java:1059)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at com.example.test.DrawIt.onDraw(DrawIt.java:154)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.draw(View.java:14465)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13362)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13404)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.draw(View.java:14182)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13357)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13404)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.draw(View.java:14182)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13357)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13404)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.draw(View.java:14182)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.draw(View.java:14468)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.widget.FrameLayout.draw(FrameLayout.java:472)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13362)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.View.getDisplayList(View.java:13404)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2381)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2253)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1883)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.Choreographer.doFrame(Choreographer.java:544)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.os.Handler.handleCallback(Handler.java:733)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.os.Looper.loop(Looper.java:136)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invoke(Method.java:515)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-23 16:00:26.179: E/AndroidRuntime(1135):     at dalvik.system.NativeStart.main(Native Method)
04-23 16:00:27.983: I/Process(1135): Sending signal. PID: 1135 SIG: 9

0 个答案:

没有答案