[英]Android display drawable on the map
我試圖在地圖視圖上顯示自定義可繪制對象,但是我可以觀察到非常奇怪的行為,可繪制對象多次繪制,最糟糕的是它一次顯示不正確。 另外,此可繪制對象與ImageView完美結合,但與MapView疊加層不一樣...
這是我的自定義可繪制對象:
public class CustomDrawable extends Drawable {
private Bitmap mBitmap;
private int mWidth;
private int mHeight;
private int mDrawLeft;
private int mDrawTop;
private int mColor;
public ChatIconDrawable(Resources res, int color) {
this.mColor = color;
this.mBitmap = BitmapFactory.decodeResource(res, R.drawable.chat_icon);
this.mWidth = mBitmap.getWidth();
this.mHeight = mBitmap.getHeight();
setBounds(-mWidth, -mHeight, 0, 0);
}
@Override
public void draw(Canvas canvas) {
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setColor(mColor);
shapeDrawable.setBounds(2 - mWidth, 3 - mHeight, mWidth - 2 - mWidth, mHeight - mHeight / 4 - mHeight);
shapeDrawable.draw(canvas);
canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, null);
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
mDrawLeft = left + (right - left - mWidth) / 2;
mDrawTop = top + (bottom - top - mHeight) / 2;
}
@Override
public void setBounds(Rect bounds) {
super.setBounds(bounds);
}
@Override
public void setAlpha(int alpha) {
// throw new UnsupportedOperationException(
// "Not supported with this drawable");
}
@Override
public void setColorFilter(ColorFilter cf) {
// throw new UnsupportedOperationException("Not supported with this drawable");
}
@Override
public void setDither(boolean dither) {
// throw new UnsupportedOperationException("Not supported with this drawable");
}
@Override
public void setFilterBitmap(boolean filter) {
// throw new UnsupportedOperationException("Not supported with this drawable");
}
@Override
public int getIntrinsicWidth() {
return mWidth;
}
@Override
public int getIntrinsicHeight() {
return mHeight;
}
@Override
public int getMinimumWidth() {
return mWidth;
}
@Override
public int getMinimumHeight() {
return mHeight;
}
}
基本上,自定義可繪制對象使用某種顏色繪制背景(它可能會有所不同),然后在頂部靜態位圖上繪制以產生正確的圖形。
要在地圖上繪制,請使用ItemizedOverlay。
如您所見,問題在於此可繪制對象繪制了多次且使用了不同的放置位置...請提出建議,因為對於我來說不清楚這是怎么回事。 謝謝。
忘記CustomDrawable類。 當您在ItemizedOverlay中使用此(或任何其他可繪制對象)時,疊加層將負責在地圖上繪制其“標記”。 使用OverlayItem.setMarker(Drawable marker)調用來設置標記。 因此,除了繪制標記的疊加層之外,您的自定義類也在對其進行繪制(在其draw()方法中)。 標記的位置可以在疊加層中進行調整,通常可以在自定義逐項疊加層的構造函數中使用,例如
super(boundCenter(defaultMarker));
這會將標記居中放置在地理位置上,而不是稍微偏移它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.