0

我正在尝试制作一个简单的绘画应用程序。我在存储颜料的阵列上遇到问题,我找不到解决方案。如果我注释掉与它运行的数组有关的部分,但是(显然)该应用程序无法按预期工作。当我打开应用程序时,它会因以下代码而崩溃:

package com.example.paint.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class PaintView extends SurfaceView implements Callback {

PaintCircle[] circles = new PaintCircle[10000];

@Override
public boolean onTouchEvent(MotionEvent event) {
    // When touched

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Paint blue = new Paint();
        blue.setColor(Color.BLUE);
        blue.setStyle(Style.FILL);
        circles[circles.length] = new PaintCircle(event.getX(),
                event.getY(), 20, blue);
        break;
    case MotionEvent.ACTION_MOVE:
        Paint green = new Paint();
        green.setColor(Color.GREEN);
        green.setStyle(Style.FILL);
        circles[circles.length] = new PaintCircle(event.getX(),
                event.getY(), 20, green);
        break;
    }

    return true;
}

@Override
public void draw(Canvas c) {
    // Draw to the canvas
    super.draw(c);
    c.drawARGB(255, 255, 255, 255);
    for (int i = 0; i < circles.length; i++) {
        c.drawCircle(circles[i].cx, circles[i].cy, circles[i].radius,
                circles[i].paint);
    }
}

public PaintView(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // When the surface view is created
    Canvas c = getHolder().lockCanvas();
    draw(c);
    getHolder().unlockCanvasAndPost(c);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    Canvas c = getHolder().lockCanvas();
    draw(c);
    getHolder().unlockCanvasAndPost(c);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

}

这是我的 PaintCircle 课程:

package com.example.paint.views;

import android.graphics.Paint;

public class PaintCircle {
public Paint paint;
public float cx, cy;
public int radius;

public PaintCircle() {

}

public PaintCircle(float cx, float cy, int radius, Paint paint) {
    cx = this.cx;
    cy = this.cy;
    radius = this.radius;
    paint = this.paint;
}
}
4

1 回答 1

1

circles[circles.length]是数组的第 10001 个元素,因为长度是静态的。

最好使用动态增长的Listlike an ArrayList

List<PaintCircle> circles = new ArrayList<PaintCircle>();

@Override
public boolean onTouchEvent(MotionEvent event) {
    // When touched

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Paint blue = new Paint();
        blue.setColor(Color.BLUE);
        blue.setStyle(Style.FILL);
        circles.add(new PaintCircle(event.getX(),
                event.getY(), 20, blue));
     // ...

@Override
public void draw(Canvas c) {
    // Draw to the canvas
    super.draw(c);
    c.drawARGB(255, 255, 255, 255);
    for (PaintCircle circle : circles) {
        c.drawCircle(circle.cx, circle.cy, circle.radius,
                circle.paint);
    }
}

另一种可能性是存储数组的单独索引,告诉您哪个是数组的最后一个元素。当您添加一个圆圈时,增加索引并仅绘制那些已分配的。

于 2012-05-12T15:17:40.533 回答