目前我正在做一个涉及室内定位的项目,我负责的模块之一是导入地图,用坐标映射它并找到最短距离。因为图像预计会有一些超过屏幕分辨率的图像,因此我使其可滚动,然后我计划用路线/坐标覆盖它。但是在使用 canvas.drawline() 时,我发现坐标仅限于屏幕分辨率。示例:图像分辨率为 1024 * 768,手机分辨率为 480*800。我通过画一条从 (0,0) 到 (600,400) 的线来测试它,然后当我运行并滚动图像时,这条线就保持在那里并且不会移动。
代码示例
public class DrawView extends View {
Paint paint = new Paint();
private Bitmap bmp;
private Rect d_rect=null;
private Rect s_rect=null;
private float starting_x=0;
private float starting_y=0;
private float scroll_x=0;
private float scroll_y=0;
private int scrollRect_x;
private int scrollRect_y;
public DrawView(Context context) {
super(context);
paint.setColor(Color.RED);
d_rect=new Rect(0,0,d_width,d_height);
s_rect=new Rect(0,0,d_width,d_height);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.hd);
bmp.isMutable();
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
switch(me.getAction())
{
case MotionEvent.ACTION_DOWN:
starting_x=me.getRawX();
starting_y=me.getRawY();
case MotionEvent.ACTION_MOVE:
float x=me.getRawX();
float y=me.getRawY();
scroll_x=x-starting_x;
scroll_y=y-starting_y;
starting_x=x;
starting_y=y;
invalidate();
break;
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
int cur_scrollRectx=scrollRect_x-(int)scroll_x;
int cur_scrollRecty=scrollRect_y-(int)scroll_y;
if(cur_scrollRectx<0)cur_scrollRectx=0;
else if(cur_scrollRectx>(bmp.getWidth()-d_width))cur_scrollRectx=(bmp.getWidth()-d_width);
if(cur_scrollRecty<0)cur_scrollRecty=0;
else if(cur_scrollRecty>(bmp.getWidth()-d_width))cur_scrollRecty=(bmp.getWidth()-d_width);
s_rect.set(cur_scrollRectx,cur_scrollRecty,cur_scrollRectx+d_width,cur_scrollRecty+d_height);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bmp, s_rect,d_rect,paint);
canvas.drawLine(0, 0, 900, 500, paint);
scrollRect_x=cur_scrollRectx;
scrollRect_y=cur_scrollRecty;
}
}
关于如何获得图像上的实际坐标的任何想法?我在 android 应用程序开发方面还是很陌生。提前致谢 !p/s: 对不起我乱七八糟的代码>.<