Android Open Source - GreenDroid Map Pin Drawable






From Project

Back to project page GreenDroid.

License

The source code is released under:

Apache License

If you think the Android project GreenDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package greendroid.graphics.drawable;
/*from   w  w w .j  a va  2  s . co  m*/
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;

import com.cyrilmottier.android.greendroid.R;
import com.google.android.maps.MapView;

/**
 * A {@link Drawable} dedicated to {@link MapView}s. A {@link MapPinDrawable}
 * displays a rounded pin with a dot in the middle. This class lets you easily
 * change the color of the pin as well as the dot in the center of the pin.
 * 
 * @author Cyril Mottier
 */
public class MapPinDrawable extends Drawable {

    private static final int COLOR_MODE_UNKNOWN = -1;
    private static final int COLOR_MODE_COLOR = 1;
    private static final int COLOR_MODE_COLOR_STATE_LIST = 2;

    private static final Paint sClearerPaint;

    static {
        sClearerPaint = new Paint();
        sClearerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    private int mColorMode = COLOR_MODE_UNKNOWN;
    private int mPinColor;
    private int mDotColor;
    private ColorStateList mPinColorStateList;
    private ColorStateList mDotColorStateList;

    private int mCurrentPinColor;
    private int mCurrentDotColor;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mMapPinPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Bitmap mBase;
    private Bitmap mPin;
    private Bitmap mDot;
    private Bitmap mMapPin;

    private boolean mNeedRebuild;

    /**
     * Create a new {@link MapPinDrawable} that has a single color.
     * 
     * @param res The application resources
     * @param color The color of the pin/dot
     */
    public MapPinDrawable(Resources res, int color) {
        this(res, color, color);
    }

    /**
     * Create a new {@link MapPinDrawable}.
     * 
     * @param res The application resources
     * @param pinClor The color of the pin
     * @param dotColor The color of the dot
     */
    public MapPinDrawable(Resources res, int pinColor, int dotColor) {
        initBitmaps(res);
        setColors(pinColor, dotColor);
    }

    /**
     * Create a new {@link MapPinDrawable} that may change color depending on
     * its current state.
     * 
     * @param res The application resources
     * @param color A {@link ColorStateList} object giving a set of colors
     *            changing depending on the current {@link Drawable}'s state
     */
    public MapPinDrawable(Resources res, ColorStateList color) {
        this(res, color, color);
    }

    /**
     * Create a new {@link MapPinDrawable} that may change color depending on
     * its current state.
     * 
     * @param res The application resources
     * @param pinColor A {@link ColorStateList} object giving a set of colors
     *            for the pin changing depending on the current {@link Drawable}
     *            's state
     * @param dotColor A {@link ColorStateList} object giving a set of colors
     *            for the dot changing depending on the current {@link Drawable}
     *            's state
     */
    public MapPinDrawable(Resources res, ColorStateList pinColor, ColorStateList dotColor) {
        initBitmaps(res);
        setColors(pinColor, dotColor);
    }

    private void initBitmaps(Resources res) {
        // TODO Cyril: Share those Bitmaps between all instances of
        // MapPinDrawable in order to save memory
        mBase = BitmapFactory.decodeResource(res, R.drawable.gd_map_pin_base);
        mPin = BitmapFactory.decodeResource(res, R.drawable.gd_map_pin_pin);
        mDot = BitmapFactory.decodeResource(res, R.drawable.gd_map_pin_dot);
    }

    /**
     * Set the color for the pin/dot
     * 
     * @param pinClor The color of the pin
     * @param dotColor The color of the dot
     */
    public void setColors(int pinColor, int dotColor) {
        if (mColorMode != COLOR_MODE_COLOR || mPinColor != pinColor || mDotColor != dotColor) {
            mColorMode = COLOR_MODE_COLOR;
            mPinColor = mCurrentPinColor = pinColor;
            mDotColor = mCurrentDotColor = dotColor;
            mNeedRebuild = true;
        }
    }

    /**
     * Set the color for the pin/dot
     * 
     * @param pinClor The color of the pin
     * @param dotColor The color of the dot
     */
    public void setColors(ColorStateList pinColor, ColorStateList dotColor) {
        if (mColorMode != COLOR_MODE_COLOR_STATE_LIST || mPinColorStateList != pinColor || mDotColorStateList != dotColor) {
            mColorMode = COLOR_MODE_COLOR_STATE_LIST;
            mPinColorStateList = pinColor;
            mDotColorStateList = dotColor;
            mNeedRebuild = true;
        }
    }

    @Override
    public boolean isStateful() {
        return true;
    }

    @Override
    public int getIntrinsicWidth() {
        return (mBase != null) ? mBase.getWidth() : -1;
    }

    @Override
    public int getIntrinsicHeight() {
        return (mBase != null) ? mBase.getHeight() : -1;
    }

    @Override
    protected boolean onStateChange(int[] stateSet) {
        if (mColorMode == COLOR_MODE_COLOR_STATE_LIST) {
            int pinColor = (mPinColorStateList != null) ? mPinColorStateList.getColorForState(stateSet, Color.BLACK) : Color.BLACK;
            int dotColor = (mDotColorStateList != null) ? mDotColorStateList.getColorForState(stateSet, Color.BLACK) : Color.BLACK;
            if (mCurrentPinColor != pinColor || mCurrentDotColor != dotColor) {
                mCurrentPinColor = pinColor;
                mCurrentDotColor = dotColor;
                mNeedRebuild = true;
                return true;
            }
        }
        return false;
    }

    @Override
    public void draw(Canvas canvas) {
        if (mNeedRebuild) {

            if (mMapPin == null) {
                mMapPin = Bitmap.createBitmap(mBase.getWidth(), mBase.getHeight(), Bitmap.Config.ARGB_8888);
            }
            Canvas c = new Canvas(mMapPin);
            c.drawRect(0, 0, mMapPin.getWidth(), mMapPin.getHeight(), sClearerPaint);

            // 1 - Draw the base
            c.drawBitmap(mBase, 0, 0, null);
            // 2 - Draw the pin on top of it
            mPaint.setColorFilter(new LightingColorFilter(Color.BLACK, mCurrentPinColor));
            c.drawBitmap(mPin, 0, 0, mPaint);
            // 3 - Draw the dot on top of everything
            mPaint.setColorFilter(new LightingColorFilter(Color.BLACK, mCurrentDotColor));
            c.drawBitmap(mDot, 0, 0, mPaint);

            mNeedRebuild = false;
        }

        canvas.drawBitmap(mMapPin, null, getBounds(), mMapPinPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        mMapPinPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mMapPinPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}




Java Source Code List

com.cyrilmottier.android.gdcatalog.AboutActivity.java
com.cyrilmottier.android.gdcatalog.ActionBarActivity.java
com.cyrilmottier.android.gdcatalog.AsyncImageViewListActivity.java
com.cyrilmottier.android.gdcatalog.BasicItemActivity.java
com.cyrilmottier.android.gdcatalog.CatalogActivity.java
com.cyrilmottier.android.gdcatalog.CatalogApplication.java
com.cyrilmottier.android.gdcatalog.InfoTabActivity.java
com.cyrilmottier.android.gdcatalog.MapPinMapActivity.java
com.cyrilmottier.android.gdcatalog.PagedViewActivity.java
com.cyrilmottier.android.gdcatalog.QuickActionActivity.java
com.cyrilmottier.android.gdcatalog.SegmentedActivity.java
com.cyrilmottier.android.gdcatalog.SimpleAsyncImageViewActivity.java
com.cyrilmottier.android.gdcatalog.TabbedActionBarActivity.java
com.cyrilmottier.android.gdcatalog.TweakedItemViewActivity.java
com.cyrilmottier.android.gdcatalog.WebContentActivity.java
com.cyrilmottier.android.gdcatalog.XmlItemActivity.java
com.cyrilmottier.android.gdcatalog.widget.HeadedTextItemView.java
com.cyrilmottier.android.gdcatalog.widget.HeadedTextItem.java
greendroid.app.ActionBarActivity.java
greendroid.app.GDActivity.java
greendroid.app.GDApplication.java
greendroid.app.GDExpandableListActivity.java
greendroid.app.GDListActivity.java
greendroid.app.GDMapActivity.java
greendroid.app.GDTabActivity.java
greendroid.graphics.drawable.ActionBarDrawable.java
greendroid.graphics.drawable.DrawableStateSet.java
greendroid.graphics.drawable.MapPinDrawable.java
greendroid.image.ChainImageProcessor.java
greendroid.image.ImageCache.java
greendroid.image.ImageLoader.java
greendroid.image.ImageProcessor.java
greendroid.image.ImageRequest.java
greendroid.image.MaskImageProcessor.java
greendroid.image.ScaleImageProcessor.java
greendroid.util.Config.java
greendroid.util.GDUtils.java
greendroid.util.Md5Util.java
greendroid.util.Time.java
greendroid.widget.ActionBarHost.java
greendroid.widget.ActionBarItem.java
greendroid.widget.ActionBar.java
greendroid.widget.AsyncImageView.java
greendroid.widget.ItemAdapter.java
greendroid.widget.LoaderActionBarItem.java
greendroid.widget.NormalActionBarItem.java
greendroid.widget.PageIndicator.java
greendroid.widget.PagedAdapter.java
greendroid.widget.PagedView.java
greendroid.widget.QuickActionBar.java
greendroid.widget.QuickActionGrid.java
greendroid.widget.QuickActionWidget.java
greendroid.widget.QuickAction.java
greendroid.widget.SegmentedAdapter.java
greendroid.widget.SegmentedBar.java
greendroid.widget.SegmentedHost.java
greendroid.widget.item.DescriptionItem.java
greendroid.widget.item.DrawableItem.java
greendroid.widget.item.Item.java
greendroid.widget.item.LongTextItem.java
greendroid.widget.item.ProgressItem.java
greendroid.widget.item.SeparatorItem.java
greendroid.widget.item.SubtextItem.java
greendroid.widget.item.SubtitleItem.java
greendroid.widget.item.TextItem.java
greendroid.widget.item.ThumbnailItem.java
greendroid.widget.itemview.DescriptionItemView.java
greendroid.widget.itemview.DrawableItemView.java
greendroid.widget.itemview.ItemView.java
greendroid.widget.itemview.LongTextItemView.java
greendroid.widget.itemview.ProgressItemView.java
greendroid.widget.itemview.SeparatorItemView.java
greendroid.widget.itemview.SubtextItemView.java
greendroid.widget.itemview.SubtitleItemView.java
greendroid.widget.itemview.TextItemView.java
greendroid.widget.itemview.ThumbnailItemView.java