问题描述
我使用这个code喜欢这点使用箭头到特定的位置(使用GPS值)指南针。
除了我要替换的arrow..but我不知道怎么的图像绘制箭头这code的过得很好。
任何帮助,请??
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.de codeResource()从您的\\加载一个位图资源\\文件夹绘制(每个方向角?),然后使用canvas.drawBitmap()来绘制的,其次是回收的位图()如果您加载在每个迭代一个新的(否则会导致内存泄漏)。
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).
这篇关于指向一个特定位置(指南针)使用图像代替drwaing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!