2012-08-12 13 views
1

目前我正在做一個涉及室內定位的項目,我負責的一個模塊是導入地圖,用座標映射它並找到最短距離。 對於圖像預計會有一些圖像,將超過屏幕分辨率,因此我讓它可以滾動,然後我打算覆蓋它與路線/座標。但是當使用canvas.drawline()時,我發現座標僅限於屏幕分辨率。例如:圖像分辨率爲1024 * 768,手機分辨率爲480 * 800。我通過從(0,0)到(600,400)繪製一條線開始測試它,然後當我運行並滾動圖像時,該線只停留在那裏,不會移動。Android:地圖座標到可滾動的圖像

例如代碼

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:抱歉我的亂碼>。 <

回答

1

認爲你需要存儲在s_rect S中的信息,s_rect存儲偏移畫布成位圖(?)

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; 

我還沒有測試代碼,但我認爲它是在正確的軌道上。

+0

謝謝老兄,我現在可以滾動它,但起始行不是固定的,因爲我滾動時,它沒有停留在(0,0)上,看起來像從起始點的X軸在滾動時不停地變化。任何想法,我可能會遺漏哪部分?提前致謝 ! – Jun 2012-08-12 16:17:20

+0

你確實改變了y座標嗎?像:int draw_x = bmp_x_origin + x; int draw_y = bmp_y_origin + y;對不起,如果這似乎很簡單。 – 2012-08-12 16:22:02

+0

是的,我也是這麼做的。現在,我可以滾動到目的地越來越接近我想要的結果。當我滾動時,只是出發點的問題隨之而來。我現在會做一些試驗和錯誤,以找出我所忽略的。 – Jun 2012-08-12 16:32:10