I am trying to display custom drawable on the map view but I can observe very strange behavior, the drawable draws multiple times and the worst thing it displays one time incorrectly. Also this drawable displays perfect with ImageView, but not as MapView overlay...
Here is my custom drawable:
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;
}
}
Basically custom drawable draws the background with some color (it may vary) and then draws on top static bitmap to produce right figure.
To draw this on the map I use ItemizedOverlay.
As you can see the problem in that this drawable draws multiple times and with different placement... please advice because it is not clear for me whats wrong. Thank you.
Forget the CustomDrawable class. When you use this (or any other drawable) in an ItemizedOverlay, the overlay will take responsibility for drawing its 'marker' on the map. The marker is set using the OverlayItem.setMarker(Drawable marker) call. So, as well as the overlay drawing the marker, your custom class is also drawing it (in its draw() method). The placement of the marker can be adjusted in the overlay, usually in the constructor of a custom itemized overlay using eg
super(boundCenter(defaultMarker));
This centres the marker on the geo-location rather than offsetting it slightly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.