我正在尝试在我的应用程序中绘制文本和位图图像。我希望将文本绘制在位图图标上方,但难以实现。
如何修改或更改我的代码,使其显示在每个图标的顶部
我的代码:
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
xCanvas = canvas.getWidth();
yCanvas = canvas.getHeight();
Paint textPaint2 = new Paint();
textPaint2.setStyle(Paint.Style.FILL_AND_STROKE);
textPaint2.setAntiAlias(true);
textPaint2.setColor(Color.WHITE);
textPaint2.setTextSize(30);
textPaint2.setTextAlign(Align.CENTER);
destination = new Location("manual");
for (int j = 0; j < placesListItems.size(); j++){
song = placesListItems.get(j);
this.lat = myLat.get(j);
this.lng = myLng.get(j);
this.name=song.get(KEY_NAME);
try {
this.icon = ICON.get(j);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log.d("the latitude",(String.valueOf(this.lat)));
// Log.d("the longitude",(String.valueOf(this.lng)));
// Double.parseDouble(song.get(KEY_LNG));
destination.setLatitude(this.lat);
destination.setLongitude(this.lng);
//Log.d("Place name",name );
this.location.distanceTo(destination);
// compute rotation matrix
float rotation[] = new float[9];
float identity[] = new float[9];
if (lastAccelerometer != null && lastCompass != null) {
boolean gotRotation = SensorManager.getRotationMatrix(rotation,
identity, lastAccelerometer, lastCompass);
if (gotRotation) {
float cameraRotation[] = new float[9];
// remap such that the camera is pointing straight down the
// Y
// axis
SensorManager.remapCoordinateSystem(rotation,
SensorManager.AXIS_X, SensorManager.AXIS_Z,
cameraRotation);
// orientation vector
orientation = new float[3];
SensorManager.getOrientation(cameraRotation, orientation);
canvas.save();
// Translate, but normalize for the FOV of the camera --
// basically, pixels per degree, times degrees == pixels
float dx = (float) ((canvas.getWidth() / horizontalFOV) * (Math
.toDegrees(orientation[0]) - this.location
.bearingTo(destination)));
float dy = (float) ((canvas.getHeight() / verticalFOV) * Math
.toDegrees(orientation[1]));
// wait to translate the dx so the horizon doesn't get
// pushed off
canvas.translate(0.0f, 0.0f - dy);
// now translate the dx
canvas.translate(0.0f - dx, 0.0f);
canvas.drawText((truncate(this.name,10).concat("...")), canvas.getWidth()/2 - 50, canvas.getHeight() / 2 - 100,
textPaint2);
canvas.drawBitmap(icon, canvas.getWidth()/2 - icon.getWidth()/2, canvas.getHeight()/2 - icon.getHeight()/2, null);
canvas.restore();
}
}
}
}