run()中的for循环SurfaceView Runnable Android

时间:2018-10-09 08:10:55

标签: android draw surfaceview runnable

嗨,我正在尝试制作一个基于用户输入的点画线的应用程序。用户可以输入尽可能多的点,对象将根据其x和y值绘制这些线。我研究了许多绘图教程,发现使用SurfaceView Runnable是在Android中进行任何类型的绘图或动画的最佳方法。我遇到了一个问题,其中run()函数未使用for循环绘制。有没有办法让循环在run()或其他地方工作?我的代码在下面。

public class draw extends SurfaceView implements Runnable {

Thread thread = null;
updateDraw draw = null;
boolean canDraw = false;
Path path;

Bitmap bitmap;
SurfaceHolder surfaceHolder;
Context mContext;
Paint paint;


int bitmapX;
int bitmapY;
int viewWidth;
int viewHeight;

ArrayList<ArrayList<Double>> XY;


Paint blue_paintbrush_stroke

public draw(Context context, ArrayList<ArrayList<Double>> XY) {
    super(context);
    mContext = context;
    surfaceHolder = getHolder();
    paint = new Paint();
    path = new Path();

}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    viewWidth = w;
    viewHeight = h;

    draw = new updateDraw(viewWidth, viewHeight);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = true;
    options.inMutable = true;

    bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bitmap, options);
    setUpBitmap();
}


@Override
public void run() {
    Canvas canvas;
    prepPaintBrushes();
    while (canDraw) {
        //draw stuff
        if (surfaceHolder.getSurface().isValid()) {
            int x = draw.getX();
            int y = draw.getY();
            canvas = surfaceHolder.lockCanvas();
            canvas.save();
            canvas.drawBitmap(bitmap, bitmapX, bitmapY, paint);
            canvas.drawPath(path, blue_paintbrush_stroke);


            for(int i = 0; i < XY.size()-1; i++){
                float aX = (XY.get(i).get(0), XY.get(i).get(1)).get(0) + bitmapX;
                float aY = (XY.get(i).get(0), XY.get(i).get(1)).get(1) + bitmapY;

                float bX = (XY.get(i+1).get(0), XY.get(i+1).get(0)).get(0) + bitmapX;
                float bY = (XY.get(i+1).get(0), XY.get(i+1).get(1)).get(1) + bitmapY;

                canvas.drawLine(aX, aY, bX, bY, blue_paintbrush_stroke);
            }

            path.rewind();
            canvas.restore();
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}


private void updateFrame(int newX, int newY) {
    draw.update(newX, newY);
}


private void setUpBitmap() {
    bitmapX = (int) Math.floor(
            Math.random() * (viewWidth - backGround.getWidth()));
    bitmapY = (int) Math.floor(
            Math.random() * (viewHeight - backGround.getHeight()));
}


public void pause() {
    canDraw = false;
    while (true) {
        try {
            thread.join();
            break;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void resume() {
    canDraw = true;
    thread = new Thread(this);
    thread.start();
}

private void prepPaintBrushes() {
    blue_paintbrush_stroke = new Paint();
    blue_paintbrush_stroke.setColor(Color.BLUE);
    blue_paintbrush_stroke.setStyle(Paint.Style.STROKE);
    blue_paintbrush_stroke.setStrokeWidth(10);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            setUpBitmap();
            // Set coordinates of map.
            updateFrame((int) x, (int) y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            // Updated coordinates for map.
            updateFrame((int) x, (int) y);
            invalidate();
            break;
        default:
            // Do nothing.
    }
    return true;
}

}

2 个答案:

答案 0 :(得分:-1)

最好重写onDraw方法和draw directly from it

  1. 创建您的自定义类并从SurfaceView扩展,但不是 实现Runnable的必要条件
  2. 覆盖onDraw(画布画布)
  3. 删除run()方法(改为使用onDraw)

答案 1 :(得分:-1)

请确保您从父活动中调用resume()方法。 像

@Override
protected void onResume(){
    super.onResume();
    customDraw.resume();
}