Android Open Source - helsinki-testbed2-android Animation View Canvas Util






From Project

Back to project page helsinki-testbed2-android.

License

The source code is released under:

GNU General Public License

If you think the Android project helsinki-testbed2-android 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 fi.testbed2.android.ui.view.util;
/*from   ww w  .  jav a  2 s. c  o  m*/
import android.graphics.*;
import com.google.inject.Inject;
import com.googlecode.androidannotations.annotations.AfterInject;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.api.Scope;
import com.jhlabs.map.Point2D;
import com.larvalabs.svgandroid.SVGParser;
import fi.testbed2.android.app.MainApplication;
import fi.testbed2.android.ui.svg.LocationMarkerSVG;
import fi.testbed2.android.ui.svg.MunicipalityMarkerSVG;
import fi.testbed2.android.ui.view.MapScaleInfo;
import fi.testbed2.domain.MapLocationXY;
import fi.testbed2.domain.Municipality;
import fi.testbed2.service.LocationService;
import fi.testbed2.service.SettingsService;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

/**
 * Utility class for canvas drawing operations used in AnimationView.
 */
@EBean(scope = Scope.Singleton)
public class AnimationViewCanvasUtil {

    /**
     * Original map image dimensions.
     */
    public static final double MAP_IMAGE_ORIG_WIDTH = 600d;
    public static final double MAP_IMAGE_ORIG_HEIGHT = 508d;

    @Inject
    SettingsService settingsService;

    @Inject
    LocationService userLocationService;

    // For caching the images
    private Picture locationMarkerImage;
    private Picture municipalityMarkerImage;

    @Getter @Setter
    private Map<Municipality, Point2D.Double> municipalitiesOnScreen;


    public float convertRawXCoordinateToScaledCanvasCoordinate(float rawX, MapScaleInfo scaleInfo) {
        return (rawX-scaleInfo.getPivotX())/scaleInfo.getScaleFactor()+scaleInfo.getPivotX();
    }

    public float convertRawYCoordinateToScaledCanvasCoordinate(float rawY, MapScaleInfo scaleInfo) {
        return (rawY-scaleInfo.getPivotY())/scaleInfo.getScaleFactor()+scaleInfo.getPivotY();
    }

    public void drawUserLocation(Canvas canvas, Rect bounds) {
        MapLocationXY userLocation = userLocationService.getUserLocationXY();
        if (userLocation!=null) {
            drawPoint(userLocation, Color.BLACK, canvas, bounds, null);
        }
    }

    public void drawMunicipalities(Canvas canvas, Rect bounds, List<Municipality> municipalities) {

        for (Municipality municipality : municipalities) {
            if (municipality!=null) {
                drawPoint(municipality.getXyPos(), Color.BLACK, canvas, bounds, municipality);
            }
        }

    }

    public void resetMarkerAndPointImageCache() {
        locationMarkerImage = null;
        municipalityMarkerImage = null;
    }

    private void drawPoint(MapLocationXY point, int color, Canvas canvas, Rect bounds, Municipality municipality) {

        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setAlpha(200); // 0...255, 255 = no transparency, does not affect SVG transparency!

        double imgScaledWidth = bounds.width();
        double imgScaledHeight = bounds.height();

        double widthRatio = imgScaledWidth / MAP_IMAGE_ORIG_WIDTH;
        double heightRatio = imgScaledHeight / MAP_IMAGE_ORIG_HEIGHT;

        float xScaled = Double.valueOf(bounds.left + point.getX()*widthRatio).floatValue();
        float yScaled = Double.valueOf(bounds.top + point.getY()*heightRatio).floatValue();

        int xInt = Float.valueOf(xScaled).intValue();
        int yInt = Float.valueOf(yScaled).intValue();

        if (municipality==null) {

            Picture pic = getLocationMarkerImage();

            int markerImageHeight = settingsService.getMapMarkerSize();

            // Scale width a bit larger than original width (otherwise looks a bit too thin marker)
            float ratio = pic.getWidth()/pic.getHeight();
            int width = Float.valueOf(markerImageHeight*ratio+markerImageHeight/5).intValue();
            int height = markerImageHeight;

            /*
            * x, y coordinates are image's top left corner,
            * so position the marker to the bottom center
            */
            int left = xInt-width/2;
            int top = yInt-height;
            int right = xInt+width/2;
            int bottom = yInt;

            drawPicture(pic, canvas, new Rect(left,top,right,bottom));

        } else {

            // Save canvas coordinates for info toast
            this.municipalitiesOnScreen.put(municipality,
                    new Point2D.Double(xInt,yInt));

            Picture pic = getMunicipalityMarkerImage();

            int circleDiameter = settingsService.getMapPointSize();

            /*
            * x, y coordinates are image's top left corner,
            * so position the marker to the center
            */

            int left = xInt-circleDiameter/2;
            int top = yInt-circleDiameter/2;
            int right = xInt+circleDiameter/2;
            int bottom = yInt+circleDiameter/2;

            drawPicture(pic, canvas, new Rect(left, top, right, bottom));
        }

    }

    private void drawPicture(Picture pic, Canvas canvas, Rect rect) {

        try {
            canvas.drawPicture(pic, rect);
        } catch (Throwable e) {
            /*
             * Some user's seem to use Force GPU setting which does not support
             * the drawPicture method (throws java.lang.UnsupportedOperationException).
             * Do not show anything to those users but an alert dialog
             * that they should disabled Force GPU settings.
             */
        }
    }

    private Picture getLocationMarkerImage() {

        if (locationMarkerImage ==null) {
            String color = settingsService.getMapMarkerColor();
            locationMarkerImage = SVGParser.getSVGFromString(new LocationMarkerSVG(color).getXmlContent()).getPicture();
        }
        return locationMarkerImage;

    }

    private Picture getMunicipalityMarkerImage() {

        if (municipalityMarkerImage ==null) {
            String color = settingsService.getMapPointColor();
            municipalityMarkerImage = SVGParser.getSVGFromString(new MunicipalityMarkerSVG(color).getXmlContent()).getPicture();
        }
        return municipalityMarkerImage;

    }

    @AfterInject
    void injectRoboGuiceDependencies() {
        MainApplication.getApplication().getInjector().injectMembers(this);
    }

}




Java Source Code List

com.larvalabs.svgandroid.ParserHelper.java
com.larvalabs.svgandroid.SVGParseException.java
com.larvalabs.svgandroid.SVGParser.java
com.larvalabs.svgandroid.SVG.java
com.robobunny.SeekBarPreference.java
com.threefiftynice.android.preference.ListPreferenceMultiSelect.java
fi.testbed2.MainModule.java
fi.testbed2.android.activity.AbstractActivity.java
fi.testbed2.android.activity.AnimationActivity.java
fi.testbed2.android.activity.MainActivity.java
fi.testbed2.android.activity.ParsingActivity.java
fi.testbed2.android.activity.TestbedPreferenceActivity.java
fi.testbed2.android.app.Logger.java
fi.testbed2.android.app.MainApplication.java
fi.testbed2.android.task.AbstractTask.java
fi.testbed2.android.task.DownloadImagesTask.java
fi.testbed2.android.task.ParseAndInitTask.java
fi.testbed2.android.task.Task.java
fi.testbed2.android.task.exception.DownloadTaskException.java
fi.testbed2.android.task.exception.TaskCancelledException.java
fi.testbed2.android.ui.dialog.AlertDialogBuilder.java
fi.testbed2.android.ui.dialog.DialogBuilder.java
fi.testbed2.android.ui.svg.LocationMarkerSVG.java
fi.testbed2.android.ui.svg.MunicipalityMarkerSVG.java
fi.testbed2.android.ui.view.AnimationViewPlayer.java
fi.testbed2.android.ui.view.AnimationView.java
fi.testbed2.android.ui.view.MapScaleInfo.java
fi.testbed2.android.ui.view.util.AnimationViewBoundsUtil.java
fi.testbed2.android.ui.view.util.AnimationViewCanvasUtil.java
fi.testbed2.android.ui.view.util.AnimationViewScaleAndGestureUtil.java
fi.testbed2.domain.MapLocationGPS.java
fi.testbed2.domain.MapLocationXY.java
fi.testbed2.domain.Municipality.java
fi.testbed2.domain.TestbedMapImage.java
fi.testbed2.domain.TestbedParsedPage.java
fi.testbed2.robotium.MainActivityRobotiumTest.java
fi.testbed2.service.BitmapService.java
fi.testbed2.service.CoordinateService.java
fi.testbed2.service.HttpUrlService.java
fi.testbed2.service.LocationService.java
fi.testbed2.service.MunicipalityService.java
fi.testbed2.service.PageService.java
fi.testbed2.service.SettingsService.java
fi.testbed2.service.impl.ApacheHttpUrlService.java
fi.testbed2.service.impl.InlineMunicipalityService.java
fi.testbed2.service.impl.LruCacheBitmapService.java
fi.testbed2.service.impl.LruCachePageService.java
fi.testbed2.service.impl.MercatorCoordinateService.java
fi.testbed2.service.impl.PreferenceBasedLocationService.java
fi.testbed2.service.impl.SharedPreferenceSettingsService.java
fi.testbed2.util.ColorUtil.java
fi.testbed2.util.MathUtil.java
fi.testbed2.util.SeekBarUtil.java
fi.testbed2.util.TimeUtil.java
net.margaritov.preference.colorpicker.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.ColorPickerDialog.java
net.margaritov.preference.colorpicker.ColorPickerPanelView.java
net.margaritov.preference.colorpicker.ColorPickerPreference.java
net.margaritov.preference.colorpicker.ColorPickerView.java