Android Paint应用程序阵列

时间:2012-05-12 14:58:06

标签: java android android-canvas

我正在尝试制作一个简单的绘画应用程序。我在存储油漆的阵列上遇到了麻烦,我找不到解决方案。如果我注释掉与它运行的数组有关的部分,但是(显然)应用程序不能按预期工作。当我打开应用程序时,它崩溃了这段代码:

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;
}
}

1 个答案:

答案 0 :(得分:1)

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

更好地使用List,就像动态增长的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);
    }
}

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