使用Android位置管理器运行SurfaceView

时间:2018-10-04 03:34:16

标签: java android location surfaceview

您好,我目前有一个SurfaceView Runnable public class mapsLayout extends SurfaceView implements Runnable {,可根据当前地理位置绘制线。我以前的解决方案涉及一个主要活动,该活动包含一个Android位置管理器-按照Google提供的教程的建议使用FusedLocationProviderClient-然后调用SurfaceView。

mainActivity与Android位置管理器->'SurfaceView'

但是,这意味着每次更改位置时,SurfaceView都会重置,每10000个滴答发生一次。

SurfaceView是否可以包含Android位置管理器。

如果有帮助,请在下面输入我的SurfaceView Runnable代码

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

}

0 个答案:

没有答案