Android Open Source - capture-the-flag Marker Factory Base






From Project

Back to project page capture-the-flag.

License

The source code is released under:

Copyright ? 2014 Microsoft Mobile Oy. All rights reserved. Microsoft is a registered trademark of Microsoft Corporation. Nokia and HERE are trademarks and/or registered trademarks of Nokia Corporati...

If you think the Android project capture-the-flag 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

/*
 * Copyright (c) 2014 Microsoft Mobile. All rights reserved.
 * See the license text file provided with this project for more information.
 *//*from   w  w w.  j  a v  a 2 s  .c  o m*/

package com.nokia.example.capturetheflag.map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;

import com.nokia.example.capturetheflag.R;
import com.nokia.example.capturetheflag.Settings;
import com.nokia.example.capturetheflag.network.model.Player;

/**
 * Base class for Here and Google-specific map marker factory classes.
 */
public class MarkerFactoryBase {

    private static final int PLAYER_NAME_MAX_LENGTH = 10;
    private static final int TEXT_MARGIN = 6;
    protected static final int PLAYER_NAME_SIZE = 15; // In dp

    /**
     * Creates and returns a bitmap for player marker.
     *
     * @param player  {@link Player}.
     * @param metrics {@link DisplayMetrics} for marker bitmap size calculation.
     * @param res     {@link Resources} to use.
     * @return {@link Bitmap} {@link Player} marker bitmap.
     */
    static protected Bitmap getBitmapForPlayer(final Player player, DisplayMetrics metrics, Resources res) {
        Bitmap base = null;

        if (player.getTeam().equals(Player.BLUE)) {
            base = BitmapFactory.decodeResource(res, R.drawable.playername);
        } else {
            base = BitmapFactory.decodeResource(res, R.drawable.playername_red);
        }

        int y = dpToPx(5, metrics);
        int textSize = (int) (PLAYER_NAME_SIZE * metrics.density + 0.5f);

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setStyle(Style.FILL);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setTextSize(textSize);

        String name = player.getName();

        if (name.length() > PLAYER_NAME_MAX_LENGTH) {
            name = name.substring(0, PLAYER_NAME_MAX_LENGTH);
            name += "...";
        }

        Bitmap img = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(img);
        canvas.drawBitmap(base, 0, 0, null);
        paint.setColor(Color.WHITE);
        canvas.drawText(name, TEXT_MARGIN, y + textSize, paint);

        return img;
    }

    /**
     * Calculates marker size for given {@link DisplayMetrics} and meters per pixel.
     *
     * @param displayMetrics         {@link DisplayMetrics}.
     * @param currentMetersPerPixels Meters per pixel.
     * @return Marker size in pixels.
     */
    public static int calculateMarkerSize(DisplayMetrics displayMetrics, double currentMetersPerPixels) {
        int size = (int) (Settings.BASE_SIZE / currentMetersPerPixels);
        int minimumSize = dpToPx(Settings.MINIMUM_MARKER_SIZE, displayMetrics);

        if (size < minimumSize) {
            size = minimumSize;
        }

        return size;
    }

    /**
     * Converts given density-independent pixel value to pixel value value using
     * density from given display metrics.
     *
     * @param dp      The device-independent pixel value to convert.
     * @param metrics {@link DisplayMetrics} to use for density.
     * @return Converted pixel value.
     */
    protected static int dpToPx(double dp, DisplayMetrics metrics) {
        return (int) (dp * metrics.density + 0.5f);
    }

}




Java Source Code List

com.nokia.example.capturetheflag.AboutActivity.java
com.nokia.example.capturetheflag.Controller.java
com.nokia.example.capturetheflag.CreateGameFragment.java
com.nokia.example.capturetheflag.GameEndedDialogFragment.java
com.nokia.example.capturetheflag.GameMenuFragment.java
com.nokia.example.capturetheflag.HelpActivity.java
com.nokia.example.capturetheflag.JoinGameFragment.java
com.nokia.example.capturetheflag.MainActivity.java
com.nokia.example.capturetheflag.PauseDialog.java
com.nokia.example.capturetheflag.PurchasePremiumFragment.java
com.nokia.example.capturetheflag.ServerSettingsDialog.java
com.nokia.example.capturetheflag.Settings.java
com.nokia.example.capturetheflag.location.LocationManagerBase.java
com.nokia.example.capturetheflag.location.LocationManagerFactory.java
com.nokia.example.capturetheflag.location.LocationManagerInterface.java
com.nokia.example.capturetheflag.location.LocationManagerListener.java
com.nokia.example.capturetheflag.location.LocationUtils.java
com.nokia.example.capturetheflag.location.google.LocationManagerGoogle.java
com.nokia.example.capturetheflag.location.here.LocationManagerHere.java
com.nokia.example.capturetheflag.map.GameMapFactory.java
com.nokia.example.capturetheflag.map.GameMapInterface.java
com.nokia.example.capturetheflag.map.GameMapUtils.java
com.nokia.example.capturetheflag.map.MarkerFactoryBase.java
com.nokia.example.capturetheflag.map.google.GameMapGoogle.java
com.nokia.example.capturetheflag.map.google.MarkerFactoryGoogle.java
com.nokia.example.capturetheflag.map.here.GameMapHere.java
com.nokia.example.capturetheflag.map.here.MarkerFactoryHere.java
com.nokia.example.capturetheflag.network.FlagCapturedResponse.java
com.nokia.example.capturetheflag.network.GameListRequest.java
com.nokia.example.capturetheflag.network.GameListResponse.java
com.nokia.example.capturetheflag.network.JSONRequest.java
com.nokia.example.capturetheflag.network.JSONResponse.java
com.nokia.example.capturetheflag.network.JoinRequest.java
com.nokia.example.capturetheflag.network.JoinedResponse.java
com.nokia.example.capturetheflag.network.NetworkClient.java
com.nokia.example.capturetheflag.network.OfflineClient.java
com.nokia.example.capturetheflag.network.SocketIONetworkClient.java
com.nokia.example.capturetheflag.network.UpdatePlayerRequest.java
com.nokia.example.capturetheflag.network.UpdatePlayerResponse.java
com.nokia.example.capturetheflag.network.model.Flag.java
com.nokia.example.capturetheflag.network.model.Game.java
com.nokia.example.capturetheflag.network.model.ModelConstants.java
com.nokia.example.capturetheflag.network.model.Player.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerBase.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.java
com.nokia.example.capturetheflag.notifications.NotificationsUtils.java
com.nokia.example.capturetheflag.notifications.google.GcmBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.google.GcmIntentService.java
com.nokia.example.capturetheflag.notifications.google.NotificationsManagerGoogle.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsIntentService.java
com.nokia.example.capturetheflag.notifications.nokia.NotificationsManagerNokia.java