Question

Currently i'm doing a project that involve in indoor positioning and one of the module that i'm responsible for is import the map, mapped it with coordinate and find shortest distance. For the image is expected to have some image that will exceed the screen resolution therefore i make it scroll-able, then i plan to overlay it with route/coordinate. But when using canvas.drawline() i found out that the coordinate is limited to the screen resolution. Example : the image resolution is 1024 * 768 and the phone resolution is 480*800. I test it by drawing a line start from (0,0) till (600,400),and then when i run and scroll the image, the line just remain there and won't move.

example of the code

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

}

Any idea on how to get the actual coordinate on the image ? I'm still quite new at android apps development. Thanks in advance ! p/s: sorry for my messy code >.<

Was it helpful?

Solution

I think the information you need s stored in s_rect, as s_rect stores the offset of the canvas into the bitmap (?)

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

So then to draw at x , y (where x and y are bitmap coords )

int draw_x = bmp_x_origin + x;

I haven't tested the code but I think it's on the right track.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top