使用图像而不是绘图指向特定位置(指南针)

时间:2011-12-30 14:06:53

标签: android gps compass-geolocation

我使用此代码就像指南针一样,使用箭头指向特定位置(使用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 个答案:

答案 0 :(得分:1)

您可以使用BitmapFactory.decodeResource()从\ res \ drawable文件夹加载一个位图(每个箭头角度一个?),然后使用canvas.drawBitmap()绘制它,然后使用位图的recycle()如果你在每次迭代时加载一个新的(否则它会泄漏内存)。