安卓 : map coordinate to a scroll-able image

标签 android image overlay scrollable coordinate

目前我正在做一个涉及室内定位的项目,我负责的模块之一是导入 map ,用坐标映射它并找到最短距离。 因为图像预计会有一些超过屏幕分辨率的图像,所以我让它可以滚动,然后我计划用路线/坐标覆盖它。但是当使用 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: 对不起,我乱七八糟的代码 >.<

最佳答案

认为您需要的信息存储在 s_rect 中,因为 s_rect 将 Canvas 的偏移量存储到位图中(?)

int bmp_x_origin = 0 - s_rect.left;
int bmp_y_origin = 0 - s_rect.top;

然后在 x , y 处绘制(其中 x 和 y 是位图坐标)

int draw_x = bmp_x_origin + x;

我没有测试代码,但我认为它是在正确的轨道上。

关于安卓 : map coordinate to a scroll-able image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923405/

相关文章:

image - 如何将RGB图像转换为单 channel 灰度图像,并使用python保存它?

html - 模态叠加层出现在固定的 div 上

android - 如何在 Android 设备上启用磁盘挂起?

安卓工作室 : What is the right way to change targetSdkVersion?

android - 使用 PhoneGap 的谷歌云消息传递

Android - 安全存储

python - numpy 数组中存储的图像中像素的方向是什么?从左到右还是从上到下?

css - 在另一个图片上显示图片

android - 如何在操作栏 Sherlock 上放置叠加 View

python-2.7 - 在 OPENCV Python 中叠加图像时的颜色强度