android google map 实现拉框功能

主要的类


package com.activity.control;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.os.Handler;
import android.util.FloatMath;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.activity.MyMapActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
* <b>类名称:</b>RectangularBoxOverlay<br/>
* <b>类描述:</b>矩形框控件<br/>
* <b>创建人:</b>ZhengJie<br/>
* <b>修改人:</b>ZhengJie<br/>
* <b>修改时间:</b>2012-7-11 上午11:41:24<br/>
* <b>修改备注:</b><br/>
* @version 1.2.0<br/>
*/
public class RectangularBoxOverlay extends Overlay implements
OnTouchListener{
private static final String TAG = "RectangularBoxOverlay";


PointF start = new PointF();
PointF mid = new PointF();
public float oldDist;
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public int mode = NONE;
private Handler mHandler;
public float x=0;
public float y=0;
public float end_x=0;
public float end_y=0;

public GeoPoint startPoint;
public GeoPoint endPoint;
/* 相关变量声明 */
//private GestureDetector mGestureDetector = new GestureDetector(this);
private Projection myProject;
private Context context;
private final Paint paint;
private Paint box_paint;
private Canvas canvas;
private MapView mapView;


//画笔状态 0绘画中 1绘画完成
private int boxState=0;


public RectangularBoxOverlay(Context context,Projection project,MapView mapView){
this.context=context;
this.myProject=project;
this.mapView=mapView;
// 设置画笔
paint = new Paint();
paint.setColor(Color.RED);
// 画笔的粗细
paint.setStrokeWidth(3);
paint.setAntiAlias(true);
paint.setStrokeMiter(3);
// paint.setStyle(Style.STROKE);
paint.setAlpha(50);
paint.setStyle(Paint.Style.FILL);//设置填满

box_paint = new Paint();
box_paint.setColor(Color.RED);
box_paint.setStyle(Style.STROKE);
box_paint.setStrokeWidth(3);
box_paint.setAntiAlias(true);
box_paint.setStrokeMiter(3);




}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
// 得到当前屏幕的高*宽

if(shadow)
{
return;
}
this.canvas=canvas;
int width = canvas.getWidth();
int height = canvas.getHeight();
//canvas.drawColor(Color.BLACK);
android.graphics.Point pixelPoint = new android.graphics.Point();


if(startPoint!=null){
pixelPoint= myProject.toPixels(startPoint,pixelPoint);
x=pixelPoint.x;
y=pixelPoint.y;
}
if(endPoint!=null){
pixelPoint= myProject.toPixels(endPoint,pixelPoint);
end_x=pixelPoint.x;
end_y=pixelPoint.y;
}
canvas.save();
if(boxState==0){
canvas.drawRect(0, 0, width, height, paint);
//System.out.println(x+"+"+ y+"+"+ end_x+"+"+ end_y);
canvas.drawRect(x, y, end_x, end_y, box_paint);
}else if(boxState==1){
paint.setAlpha(0);
canvas.drawRect(0, 0, width, height, paint);
paint.setAlpha(50);
canvas.drawRect(x+3, y+3, end_x-3, end_y-3, paint);
canvas.drawRect(x, y, end_x, end_y, box_paint);

}
canvas.restore();
//System.out.println("我在画画");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event, MapView mapView) {
//System.out.println("-------onKeyDown");
return super.onKeyDown(keyCode, event, mapView);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
//System.out.println("-------进入拖拉模式");
//System.out.println(x+"+"+ y+"+"+ end_x+"+"+ end_y);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
// 设置拖拉模式
case MotionEvent.ACTION_DOWN:
//start.set(event.getX(), event.getY());
mode = DRAG;
x=event.getX();
y=event.getY();
//this.canvas=new Canvas();
//System.out.println("x:"+x+" y:"+y);
startPoint=myProject.fromPixels(new Double(x).intValue() , new Double(y).intValue());
break;

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
//System.out.println("弹起了");
boxState=1;
mode = NONE;
((MyMapActivity)this.context).lock_map=false;
mapView.invalidate();
break;
// 设置多点触摸模式
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
mode = ZOOM;
}
break;
// 若为DRAG模式,则点击移动图片
case MotionEvent.ACTION_MOVE:
end_x=event.getX();
end_y=event.getY();
endPoint=myProject.fromPixels(new Double(end_x).intValue() , new Double(end_y).intValue());
canvas.drawRect(x, y, end_x, end_y, box_paint);
if (mode == DRAG) {
mapView.invalidate();
//this.draw(new Canvas(), mapView, true);
//System.out.println(x+"+"+ y+"+"+ end_x+"+"+ end_y);
}
// 若为ZOOM模式,则多点触摸缩放
else if (mode == ZOOM) {

float newDist = spacing(event);
if (newDist > 10f) {


float scale = newDist / oldDist;
// System.out.println(scale+"+"+ scale+"+"+ mid.x+"+"+ mid.y);
// 设置缩放比例和图片中点位置
}
}
break;
}
return false;
}


// 计算移动距离
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}

// 计算中点位置
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值