package org.osmdroid.samplefragments.drawing;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Polyline;
import java.util.ArrayList;
import java.util.List;
/**
* A very simple borrowed from Android's "Finger Page" example, modified to generate polylines that
* are geopoint bound after finger up.
* created on 1/13/2017.
*
* @author Alex O'Ree
*/
public class CustomPaintingSurface extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private MapView map;
private List<Point> pts = new ArrayList<>();
private EmbossMaskFilter mEmboss;
private Paint mPaint;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private BlurMaskFilter mBlur;
public CustomPaintingSurface(Context context, AttributeSet attrs) {
super(context,attrs);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
mCanvas = new Canvas(mBitmap);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
canvas.drawPath(mPath, mPaint);
}
public void init(MapView mapView) {
map=mapView;
}
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
if (map!=null){
Projection projection = map.getProjection();
List<GeoPoint> geoPoints = new ArrayList<>();
for (int i=0; i < pts.size(); i++) {
GeoPoint iGeoPoint = (GeoPoint) projection.fromPixels(pts.get(i).x, pts.get(i).y);
geoPoints.add(iGeoPoint);
}
//TODO run the double pucker algorithm to reduce the points for performance reasons
if (geoPoints.size() > 2) {
//only plat a line unless there's at least one item
Polyline line = new Polyline();
line.setPoints(geoPoints);
line.setOnClickListener(new Polyline.OnClickListener() {
@Override
public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {
Toast.makeText(mapView.getContext(), "polyline with " + polyline.getPoints().size() + "pts was tapped", Toast.LENGTH_LONG).show();
return false;
}
});
map.getOverlayManager().add(line);
map.invalidate();
}
}
pts.clear();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
pts.add(new Point((int)x,(int)y));
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}