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();
}
}
非常感謝...但你能告訴我在哪裏放這些東西嗎?請 – Reham 2011-12-30 19:14:02
這取決於你如何編寫你的代碼。將箭頭圖像加載到應用程序開始的位圖陣列中然後在Draw()方法中繪製正確的圖像可能會很有意義。我的觀點是,我提供了可用於完成任務的API,設計應用程序取決於您。請記住,位圖需要通過recycle()方法顯式釋放,否則將會非常快地耗盡內存。 – BitBank 2011-12-30 20:13:17