使用图像指向特定位置(指南针),而不是绘图 [英] Pointing to a specific location (compass) using an image instead of drawing

查看:186
本文介绍了使用图像指向特定位置(指南针),而不是绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,就像指南针一样,使用指向特定位置的箭头(使用GPS值)。
这段代码做得很好,除了我想用箭头的图像替代绘制箭头..但我不知道如何。
有任何帮助吗?

I'm using this code like a compass which point using the arrow to a specific location(Using GPS Values). This code is doing great except i want to replace drawing the arrow by an image of the arrow..but i don't know how. any help please??

@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();
}

}

}

推荐答案

你可以使用BitmapFactory.decodeResource()来从你的\\ ,如果你在每一次迭代中加载一个新的,那么后面跟着位图的recycle()(否则它会泄漏内存)。

You can use BitmapFactory.decodeResource() to load a bitmap from your \res\drawable folder (one for each arrow angle?), then use canvas.drawBitmap() to draw it, followed by recycle() of the bitmap if you load a new one on each iteration (otherwise it will leak memory).

这篇关于使用图像指向特定位置(指南针),而不是绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆