Android Open Source - helsinki-testbed2-android Animation View Scale And Gesture 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;
//  w ww  .ja  v a2  s. co  m
import android.content.Context;
import android.view.*;
import android.widget.TextView;
import android.widget.Toast;
import com.google.inject.Inject;
import com.googlecode.androidannotations.annotations.AfterInject;
import com.googlecode.androidannotations.annotations.EBean;
import com.jhlabs.map.Point2D;
import fi.testbed2.R;
import fi.testbed2.android.app.Logger;
import fi.testbed2.android.app.MainApplication;
import fi.testbed2.android.ui.view.AnimationView;
import fi.testbed2.android.ui.view.MapScaleInfo;
import fi.testbed2.domain.Municipality;
import fi.testbed2.service.SettingsService;
import lombok.Getter;
import lombok.Setter;

/**
 * Utility class for scaling and gesture related functions
 * in AnimationView.
 */
@EBean
public class AnimationViewScaleAndGestureUtil {

    /**
     * Defines how many pixels the user is allowed to click
     * off the municipality point. If the touch is withing this
     * threshold, the municipality info is shown.
     */
    public static final float MUNICIPALITY_SEARCH_THRESHOLD = 15.0f;

    @Inject
    SettingsService settingsService;

    @Setter
    AnimationView view;

    @Getter
    private ScaleListener scaleListener;

    @Getter
    private GestureListener gestureListener;

    private Toast municipalityToast;

    public AnimationViewScaleAndGestureUtil() {
        scaleListener = new ScaleListener();
        gestureListener = new GestureListener();
    }

    /*
    * ============
    * Listeners for pinch zooming
    * ============
    */

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            if (detector.getScaleFactor()>1.0+MapScaleInfo.MIN_SCALE_STEP_WHEN_PINCHING ||
                    detector.getScaleFactor()<1.0-MapScaleInfo.MIN_SCALE_STEP_WHEN_PINCHING) {

                float newScaleFactor = view.getScaleInfo().getScaleFactor() * detector.getScaleFactor();

                // Don't let the map to get too small or too large.
                newScaleFactor = Math.max(MapScaleInfo.MIN_SCALE_FACTOR,
                        Math.min(newScaleFactor, MapScaleInfo.MAX_SCALE_FACTOR));
                view.getScaleInfo().setScaleFactor(newScaleFactor);
                view.getScaleInfo().setPivotX(view.getMeasuredWidth() / 2);
                view.getScaleInfo().setPivotY(view.getMeasuredHeight() / 2);

                hideMunicipalityToast();
                view.invalidate();
                return true;

            }

            return false;
        }

    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            view.getScaleInfo().setPivotX(e.getX());
            view.getScaleInfo().setPivotY(e.getY());
            float newScaleFactor = view.getScaleInfo().getScaleFactor() *
                    MapScaleInfo.SCALE_STEP_WHEN_DOUBLE_TAPPING;

            if (newScaleFactor >= MapScaleInfo.MAX_SCALE_FACTOR) {
                newScaleFactor = MapScaleInfo.DEFAULT_SCALE_FACTOR;
            }

            view.getScaleInfo().setScaleFactor(newScaleFactor);

            hideMunicipalityToast();
            view.invalidate();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {

            float xCanvas = view.getCanvasUtil().convertRawXCoordinateToScaledCanvasCoordinate(e.getX(), view.getScaleInfo());
            float yCanvas = view.getCanvasUtil().convertRawYCoordinateToScaledCanvasCoordinate(e.getY(), view.getScaleInfo());

            Logger.debug("Long pressed x: " + e.getX());
            Logger.debug("Long pressed y: " + e.getY());
            Logger.debug("Long pressed x (canvas): " + xCanvas);
            Logger.debug("Long pressed y (canvas): " + yCanvas);
            Logger.debug("Scale factor: " + view.getScaleInfo().getScaleFactor());
            Logger.debug("Scale pivotX: " + view.getScaleInfo().getPivotX());
            Logger.debug("Scale pivotY: " + view.getScaleInfo().getPivotY());

            hideMunicipalityToast(); // always hide previous toast
            showInfoForMunicipality(xCanvas, yCanvas, e.getX(), e.getY());
        }

    }

    /**
     * Shows info toast about selected municipality if there is
     * a municipality close to the given x,y coordinates.
     *
     * @param canvasX X coordinate in canvas coordinate
     * @param canvasY Y coordinate in canvas coordinate
     * @param rawX Raw X coordinate from the touch event
     * @param rawY Raw Y coordinate from the touch event
     * @return True if the toast was shown, false if not shown.
     */
    public boolean showInfoForMunicipality(float canvasX, float canvasY, float rawX, float rawY) {

        for (Municipality municipality : view.getMunicipalities()) {

            if (municipality!=null) {

                Point2D.Double pos = view.getCanvasUtil().getMunicipalitiesOnScreen().get(municipality);

                if (pos!=null) {

                    Logger.debug("Pos: " + pos + ", for: " + municipality.getName());

                    final float scale = view.getContext().getResources().getDisplayMetrics().density;
                    int threshold = (int) ((settingsService.getMapPointSize()/2)*scale +
                            (MUNICIPALITY_SEARCH_THRESHOLD/view.getScaleInfo().getScaleFactor())*scale);

                    if (Math.abs(pos.x-canvasX)<=threshold && Math.abs(pos.y-canvasY)<=threshold) {

                        Logger.debug("Show info for municipality: " + municipality.getName());

                        LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        View layout = inflater.inflate(R.layout.toast_municipality,
                                (ViewGroup) view.findViewById(R.id.toast_layout_root));

                        TextView text = (TextView) layout.findViewById(R.id.text);
                        text.setText(municipality.getName());

                        municipalityToast = new Toast(view.getContext());
                        municipalityToast.setGravity(Gravity.TOP| Gravity.LEFT,
                                Double.valueOf(rawX+20*scale).intValue(),
                                Double.valueOf(rawY-40*scale).intValue());
                        municipalityToast.setDuration(Toast.LENGTH_LONG);
                        municipalityToast.setView(layout);
                        municipalityToast.show();
                        return true;

                    }

                }

            }

        }

        return false;

    }

    public void hideMunicipalityToast() {
        if (municipalityToast!=null) {
            municipalityToast.cancel();
        }
    }

    @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