SurfaceView Runnable с диспетчером расположения Android - PullRequest
0 голосов
/ 04 октября 2018

Привет У меня сейчас есть SurfaceView Runnable public class mapsLayout extends SurfaceView implements Runnable {, который рисует линии на основе текущей геолокации.Мое предыдущее решение состояло в том, чтобы выполнить основное действие, содержащее диспетчер местоположения Android - с использованием FusedLocationProviderClient, как рекомендовано учебными пособиями, предоставленным Google, - а затем вызвать SurfaceView.

mainActivity с помощью диспетчера местоположений Android -> 'SurfaceView'

Однако это означает, что SurfaceView сбрасывается при каждом изменении местоположения, которое происходит каждые 10000 тиков.

Есть лиспособ для SurfaceView содержать диспетчер местоположения Android.

Если это поможет, у меня есть код запуска SurfaceView ниже

public class mapsActivityLayout extends SurfaceView implements Runnable {


MyThread draw = null;
boolean canDraw = false;

Bitmap map;
SurfaceHolder surfaceHolder;
Context mContext;
Paint paint;
ArrayList<String> endNodes = new ArrayList<String>();

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

Paint red_paintbrush_fill, blue_paintbrush_fill, green_paintbrush_fill;
Paint red_paintbrush_stroke, blue_paintbrush_stroke, green_paintbrush_stroke;
Path line;
Path circle;

public mapsActivityLayout(Context context, ArrayList<String> endNodes) {
    super(context);
    this.endNodes = endNodes
    mContext = context;
    surfaceHolder = getHolder();
    paint = new Paint();
    paint.setColor(Color.DKGRAY);
}

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

    viewWidth = w;
    viewHeight = h;

    draw = new MyThread(viewWidth, viewHeight);

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

    map = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mapimage, 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();
            //int radius = draw.getRadius();
            canvas = surfaceHolder.lockCanvas();
            canvas.save();
            canvas.drawBitmap(map, bitmapX, bitmapY, paint);

            ArrayList<Double> currentLocation = convertGeoToPixel(currentLat, currentLon);
            ArrayList<Double> destination = convertGeoToPixel(destinationLat, destinationLat);

            int width = 2699;
            int height = 2699;

            canvas.drawCircle(currentX, currentY, 20, red_paintbrush_fill);
            canvas.drawCircle(destX, destY, 20, green_paintbrush_fill);

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


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

/**
 * Calculates a randomized location for the bitmap
 * and the winning bounding rectangle.
 */
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() {
    red_paintbrush_fill = new Paint();
    red_paintbrush_fill.setColor(Color.RED);
    red_paintbrush_fill.setStyle(Paint.Style.FILL);

    green_paintbrush_stroke = new Paint();
    green_paintbrush_stroke.setColor(Color.GREEN);
    green_paintbrush_stroke.setStyle(Paint.Style.STROKE);
    green_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();

            updateFrame((int) x, (int) y);
            invalidate();
            break;

        case MotionEvent.ACTION_MOVE:

            updateFrame((int) x, (int) y);
            invalidate();
            break;
        default:

    }
    return true;
}
}

код потока

public class MyThread extends Thread {

private int mX;
private int mY;

public MyThread(int viewWidth, int viewHeight) {
    //super()
    mX = viewWidth / 2;
    mY = viewHeight / 2;

}

/**
 * Update the coordinates of the map.
 *
 * @param newX Changed value for x coordinate.
 * @param newY Changed value for y coordinate.
 */
public void update(int newX, int newY) {
    mX = newX;
    mY = newY;
}

public int getX() {
    return mX;
}

public int getY() {
    return mY;
}

}
...