2011-12-30 4 views
0

저는이 코드를 화살표 (GPS 값 사용)를 사용하여 가리키는 나침반처럼 사용하고 있습니다. 이 코드는 화살표 이미지를 화살표 그리기로 바꾸고 싶다는 점을 제외하고는 훌륭하게 작동합니다. 그러나 어떻게 작동하는지는 알 수 없습니다. 어떤 도움을 주시겠습니까 ??그림 대신 그림을 사용하여 특정 위치 (나침반)를 가리키고 있습니다.

@Override 
public void draw(Canvas canvas) { 
    double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); 
    //Correction; 
    angle-=90; 

    //Correction for azimuth 
    angle-=azimuth; 

    if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90; 

    while(angle<0)angle=angle+360; 

    Rect rect = canvas.getClipBounds(); 

    int height = rect.bottom-rect.top; 
    int width = rect.right-rect.left; 
    int left = rect.left; 
    int top = rect.top; 

    if(height>width){ 
     top+=(height-width)/2; 
     height=width; 
    } 
    if(width>height){ 
     left+=(width-height)/2; 
     width=height; 
    } 

    float centerwidth = width/2f; 
    float centerheight = height/2f; 

    Paint p = new Paint(); 
    p.setColor(color); 
    p.setStyle(Paint.Style.FILL); 
    p.setAntiAlias(true); 

    float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0); 
    float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0); 

    Path path = new Path(); 
    path.moveTo(
      startX, 
      startY); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0)); 
    path.lineTo(
      left+(float)centerwidth, 
      top+(float)centerheight 
      ); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0) 
      ); 

    path.lineTo(
      startX, 
      startY 
      ); 

    canvas.drawPath(path, p); 
} 

public DirectionImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 

public DirectionImageView(Context context, AttributeSet attrs){ 
    super(context,attrs); 
    init(); 
} 

public DirectionImageView(Context context){ 
    super(context); 
    init(); 
} 


public void onSensorChanged(SensorEvent event) { 
    azimuth = event.values[0]; 
    invalidate(); 
} 

public static double calculateAngle(double x1, double y1, double x2, 
     double y2) { 
    double dx = x2 - x1; 
    double dy = y2 - y1; 

    return (Math.atan2(dx, dy) * 180)/Math.PI; 

} 

public float getAzimuth() { 
    return azimuth; 
} 

public void setAzimuth(float azimuth) { 
    this.azimuth = azimuth; 
    invalidate(); 
} 

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
/* :: This function converts decimal degrees to radians : */ 
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
public static double deg2rad(double deg) { 
    return (deg * Math.PI/180.0); 
} 

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
/* :: This function converts radians to decimal degrees : */ 
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
public static double rad2deg(double rad) { 
    return (rad * 180.0/Math.PI); 
} 

@Override 
protected void onAttachedToWindow() { 
    if(callback!=null)callback.onImageAttached(this); 
    super.onAttachedToWindow(); 
    Log.v(getClass().getName(),"onAttachedToWindow()"); 
} 

@Override 
protected void onDetachedFromWindow() { 
    if(callback!=null)callback.onImageDetached(this); 
    super.onDetachedFromWindow(); 
    Log.v(getClass().getName(),"onDetachedFromWindow()"); 
} 

public static abstract class AttachCallback { 
    public abstract void onImageAttached(DirectionImageView iv); 
    public abstract void onImageDetached(DirectionImageView iv); 
    public abstract void pauseAll(); 
    public abstract void resumeAll(); 
} 


public void setColor(int color){ 
    this.color = color; 
    invalidate(); 
} 

}

답변

1

당신은 다음 canvas.drawBitmap을 사용하여 고해상도 \ 당김 폴더 (각 화살표 각도 하나?) 당신의 \에서 비트 맵을로드 할 수 BitmapFactory.decodeResource()를 사용할 수()를 그려 , 반복 할 때마다 새로운 비트 맵을로드하면 비트 맵을 recycle()합니다 (그렇지 않으면 메모리가 누수됩니다).

+0

고마워요.하지만 이걸 넣을 곳을 말해 줄 수 있습니까? please – Reham

+0

코드 작성 방법은 당신에게 달려 있습니다. 앱 시작시 비트 맵 배열에 화살표 이미지를로드 한 다음 Draw() 메소드에서 올바른 화살표 이미지를 그릴 수 있습니다. 요점은 내가 당신의 작업을 수행하는 데 사용할 수있는 API를 제공했고 당신의 앱을 설계하는 것은 개발자의 몫입니다. 비트 맵을 recycle() 메서드로 명시 적으로 해제해야하거나 메모리가 실제로 소모 될 수 있다는 점을 기억하십시오. – BitBank