Android 使用图像而不是图形指向特定位置(指南针)
我使用的代码就像指南针一样,使用箭头指向特定位置(使用GPS值)。 这段代码做得很好,除了我想用箭头的图像来代替绘制箭头……但是我不知道如何做。 有什么帮助吗Android 使用图像而不是图形指向特定位置(指南针),android,gps,compass-geolocation,Android,Gps,Compass Geolocation,我使用的代码就像指南针一样,使用箭头指向特定位置(使用GPS值)。 这段代码做得很好,除了我想用箭头的图像来代替绘制箭头……但是我不知道如何做。 有什么帮助吗 @Override public void draw(Canvas canvas) { double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); //Correction; a
@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();
}
@覆盖
公共空白绘制(画布){
双角度=计算角度(当前经度、当前纬度、目标长度、目标高度);
//纠正;
角度-=90;
//方位角校正
角度-=方位角;
如果((getContext()instanceof Activity)和((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION\u Grait)角度-=90;
while(角度宽度){
顶部+=(高宽)/2;
高度=宽度;
}
如果(宽度>高度){
左+=(宽高)/2;
宽度=高度;
}
浮动中心宽度=宽度/2f;
浮动中心高度=高度/2f;
油漆p=新油漆();
p、 设置颜色(颜色);
p、 设置样式(油漆、样式、填充);
p、 setAntiAlias(真);
浮动起点x=左+(浮动)(中心宽度+数学坐标(deg2rad(角度))*宽度/3.0);
浮动起点=顶部+(浮动)(中心高度+数学sin(deg2rad(角度))*高度/3.0);
路径路径=新路径();
path.moveTo(
startX,
星体);
path.lineTo(
左+(浮动)(中心宽度+数学坐标(deg2rad(角度+140))*宽度/4.0),
顶部+(浮动)(中心高度+数学sin(deg2rad(角度+140))*高度/4.0);
path.lineTo(
左+(浮动)中心宽度,
顶部+(浮动)中心高度
);
path.lineTo(
左+(浮动)(中心宽度+数学坐标(deg2rad(角度+220))*宽度/4.0),
顶部+(浮动)(中心高度+数学sin(deg2rad(角度+220))*高度/4.0)
);
path.lineTo(
startX,
斯塔蒂
);
canvas.drawPath(路径,p);
}
public DirectionImageView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
public DirectionImageView(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共方向图像视图(上下文){
超级(上下文);
init();
}
传感器更改时的公共无效(传感器事件){
方位角=事件值[0];
使无效();
}
公共静态双计算角(双x1,双y1,双x2,
双y2){
双dx=x2-x1;
双dy=y2-y1;
返回(Math.atan2(dx,dy)*180)/Math.PI;
}
公共浮点数(){
返回方位角;
}
公共空间设置方位角(浮动方位角){
这个方位角=方位角;
使无效();
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/*::此函数用于将十进制度数转换为弧度:*/
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
公共静态双deg2rad(双deg){
返回值(deg*Math.PI/180.0);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/*::此函数用于将弧度转换为十进制度数:*/
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
公共静态双rad2deg(双rad){
返回值(rad*180.0/Math.PI);
}
@凌驾
受保护的无效数据附加到DOWINDOW(){
if(callback!=null)callback.onImageAttached(this);
super.onAttachedToWindow();
Log.v(getClass().getName(),“onAttachedToWindow()”;
}
@凌驾
受保护的无效onDetachedFromWindow(){
if(callback!=null)callback.onImageDetached(this);
super.onDetachedFromWindow();
Log.v(getClass().getName(),“OnDepachedFromWindow()”;
}
公共静态抽象类AttachCallback{
随附的公开摘要(DirectionImageView iv);
公开摘要无效(DirectionImageView iv);
公共摘要无效pauseAll();
公共摘要全部无效();
}
公共void setColor(int-color){
这个颜色=颜色;
使无效();
}
}您可以使用BitmapFactory.decodeResource()从\res\drawable文件夹加载位图(每个箭头角度一个),然后使用canvas.drawBitmap()绘制位图,如果在每次迭代中加载新的位图,则循环()位图(否则会泄漏内存).非常感谢……但是你能告诉我这些东西放在哪里吗?请告诉我如何编写代码。在应用程序开始时将箭头图像加载到位图数组中,然后在draw()方法中绘制正确的箭头图像可能是有意义的。我的观点是,我已经提供了API,你可以使用它来完成你的任务,这取决于你来设计你的应用程序。请记住,位图需要使用recycle()方法显式释放,否则会很快耗尽内存。