我必须在地图视图上放置标记并在标记上写一个数字。我已经这样做了,但文本对齐方式因不同分辨率而异。下面是参考代码
float xVal = (float) curScreenCoords.x; // Point curScreenCoords
float yVal = (float) curScreenCoords.y-20; // Point curScreenCoords
Bitmap bitmap = BitmapFactory.decodeResource ( context.getResources() , ( R.drawable.pin_number ) ) ;
canvas.drawBitmap(bitmap, xVal, yVal, getInnerPaint());
public Paint getInnerPaint() {
if (innerPaint == null) {
innerPaint = new Paint();
}
innerPaint.setARGB(255, 117, 161, 220); // blue
innerPaint.setAntiAlias(true);
innerPaint.setStyle(Style.FILL);
return innerPaint;
}
canvas.drawText(String.valueOf(10), xVal+20, yVal+22, getCountPaint()); // 10 is just for example, it can vary to one digit to two to three
public Paint getCountPaint() {
if (innerPaint == null) {
innerPaint = new Paint();
}
innerPaint.setARGB(255, 255, 255, 255);
innerPaint.setAntiAlias(true);
innerPaint.setStyle(Style.FILL);
innerPaint.setTextSize(12f);
innerPaint.setTextAlign(Align.CENTER);
return innerPaint;
}
一切正常,除了文本对齐,此代码适用于480 * 800分辨率。文本在画布中完全居中对齐。 x,y位置在图像上是完美的,但在320 * 480上看起来并不完美。文本的x和y位置在此分辨率上看起来不同。任何人都可以建议我究竟发生了什么错误?在不同尺寸的设备上做同样的事情还有什么基础吗?提前致谢。
答案 0 :(得分:5)
我认为您可以测量文本在画布中写入后的宽度和高度,然后使用它来居中。类似的东西:
String text = "whatever";
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
canvas.drawText(text, (canvas.getWidth() - bounds.width()) / 2, (canvas.getHeight() - bounds.height()) / 2, paint);
答案 1 :(得分:5)
嗨,我猜上面给出的答案都不够好,所以我发布我的答案试试看,所有设备上的人都会工作,而且根本不复杂
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//paint.setTextAlign(Align.CENTER);
paint.setColor(activity.getResources().getColor(R.color.white));
paint.setTextSize(30);
// draw text to the Canvas center
Rect boundsText = new Rect();
paint.getTextBounds(String.valueOf(cluster.getMarkerList().size()), 0,
String.valueOf(cluster.getMarkerList().size()).length(), boundsText);
int x = (bitmap.getWidth() - boundsText.width()) / 2;
int y = (bitmap.getHeight() + boundsText.height()) / 2;
canvas.drawText(String.valueOf(cluster.getMarkerList().size()), x, y, paint);
答案 2 :(得分:2)
您的值curScreenCoords.y-20
和xVal+20, yVal+22
对所有分辨率都有恒定的像素偏移量,但它们应该取决于设备的像素密度,如下所示:
xOffset = (int) (13.33f * context.getResources().getDisplayMetrics().density + 0.5f);
yOffset = (int) (14.67f * context.getResources().getDisplayMetrics().density + 0.5f);
canvas.drawText(String.valueOf(10), xVal + xOffset, yVal + yOffset, getCountPaint());
答案 3 :(得分:1)
确保检查硬件加速是否已关闭。在4.1.2和其他设备(三星Galaxy Tag 2.0)上,您会收到致命信号11错误。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
这解决了我使用此代码时遇到的问题。 canvas.drawText导致错误。
答案 4 :(得分:0)
您可以扩展Image类并覆盖它的onDraw。如下
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.widget.ImageView;
/**
* @author amit
*
*/
public class CustomImageView extends ImageView {
private int notificationCount;
private Paint paint;
/**
* @param context
*/
public CustomImageView(Context context) {
super(context);
notificationCount = 0;
paint = new Paint();
paint.setColor(Color.RED);
ColorFilter cf = new ColorFilter();
paint.setColorFilter(cf);
paint.setStyle(Paint.Style.FILL);
paint.setFakeBoldText(true);
paint.setTextSize(15);
}
public synchronized void incrementNotification() {
notificationCount--;
this.invalidate();
}
public synchronized void decrementNotification() {
notificationCount++;
this.invalidate();
}
/**
* @return the notificationCount
*/
public synchronized int getNotificationCount() {
return notificationCount;
}
/**
* @param notificationCount
* the notificationCount to set
*/
public synchronized void setNotificationCount(int notificationCount) {
this.notificationCount = notificationCount;
this.invalidate();
}
/*
* (non-Javadoc)
*
* @see android.widget.ImageView#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
// System.out.println("OnDraw is called");
super.onDraw(canvas);
if (notificationCount == 0) {
return;
}
canvas.drawText(String.valueOf(notificationCount), 0, 0, paint);
}
}
比你可以调用以下任何一种方法
incrementNotification();
decrementNotification();
setNotification(int number);
在构造函数中选择您选择的颜色。祝您好运!
答案 5 :(得分:0)
我有同样的问题。我想在位图中绘制文本 - 用于在Google地图上进行聚类。实际上它在位图的CENTER上绘制文本。
用Kotlin编写的代码
public string Authenticate(LoginDetails loginDetails)
{
SecureString securePassword = new SecureString();
foreach (var c in loginDetails.Password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(loginDetails.UserName, securePassword);
return credentials.GetAuthenticationCookie(new Uri(loginDetails.SiteUrl));
}