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