현재 저는 실내 위치 지정과 관련된 프로젝트를 진행하고 있으며 내가 책임지고있는 모듈 중 하나는지도를 가져 와서 좌표로 매핑하고 최단 거리를 찾습니다. 이미지가 화면 해상도를 초과하는 일부 이미지를 가질 것으로 예상되므로 스크롤 가능하게 만들고 경로/좌표로 오버레이 할 계획입니다. 하지만 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;
}
}
실제 이미지에 조정하는 방법에 대한 어떤 생각의 예? 나는 안드로이드 애플 리케이션 개발에 아직도 아주 새로운 것이다. 미리 감사드립니다! p/s : 내 지저분한 코드에 대해 죄송합니다.> <
고마워요, 지금은 스크롤 할 수 있지만 시작 선은 스크롤 할 때 고정되어 있지 않습니다. (0,0)에 머 무르지 않았으며 스크롤하는 동안 시작점에서부터 x 축과 같이 보입니다. 어떤 부분을 버렸을지도 모른다는 어떤 생각? 미리 감사드립니다! – Jun
y 좌표를 바꿨습니까? like : int draw_x = bmp_x_origin + x; int draw_y = bmp_y_origin + y; 죄송합니다. 정말 간단합니다. –
그래, 나도 그랬어. 이제 목적지로 스크롤하여 원하는 결과에 가까워졌습니다. 출발점의 문제는 내가 스크롤 할 때 따라옵니다. 내가 뭘 버렸는지 알아 내기 위해 몇 번 시행 착오를하겠습니다. – Jun