Android Open Source - trifork-ibeacon-demo Beacon View






From Project

Back to project page trifork-ibeacon-demo.

License

The source code is released under:

Apache License

If you think the Android project trifork-ibeacon-demo 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 com.trifork.ibeacon.widgets;
/*from  ww w  .  ja v  a  2 s. c o  m*/
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class BeaconView extends View {

    private View parentView;
    private int backgroundColor = Color.BLACK;
    private float pixelsPrMeter;
    private float xPositionMeters;
    private float yPositionMeters;
    private OnPositionChangedListener listener;
    private float distance;

    public BeaconView(Context context) {
        super(context);
        init();
    }

    public BeaconView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BeaconView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        parentView = (View) getParent();
        createBackgroundDrawable();
        configureOnTouchListener();
    }

    private void configureOnTouchListener() {
        setOnTouchListener(new OnTouchListener() {

            int startX;
            int startY;

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        startX = X;
                        startY = Y;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int diffX = X - startX;
                        int diffY = Y - startY;
                        setRoomCoordinate(xPositionMeters + diffX/pixelsPrMeter, yPositionMeters + diffY/pixelsPrMeter);
                        startX = X;
                        startY = Y;
                        break;
                }

                return true;
            }
        });
    }

    private void createBackgroundDrawable() {
        ShapeDrawable background = new ShapeDrawable(new OvalShape());
        background.getPaint().setColor(backgroundColor);
        background.getPaint().setStyle(Paint.Style.FILL);
        setBackground(background);
    }

    public void updatePixelsPrMeter(float pixelsPrMeter) {
        this.pixelsPrMeter = pixelsPrMeter;
        requestLayout();
    }

    public void setRoomCoordinate(float xInMeters, float yInMeters) {
        this.xPositionMeters = xInMeters;
        this.yPositionMeters = yInMeters;
        notifyPositionChanged();
        invalidatePosition();
    }

    private void notifyPositionChanged() {
        if (listener != null) {
            listener.positionChanged(xPositionMeters, yPositionMeters);
        }
    }

    private void invalidatePosition() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
        layoutParams.leftMargin = (int) (xPositionMeters * pixelsPrMeter);
        layoutParams.topMargin = (int) (yPositionMeters * pixelsPrMeter);
        setLayoutParams(layoutParams);
    }

    public void setBeaconColor(int color) {
        this.backgroundColor = color;
        createBackgroundDrawable();
        invalidate();
    }

    public void setOnPositionChangedListener(OnPositionChangedListener listener) {
        this.listener = listener;
    }

    public void updateDistance(float distance) {
        this.distance = distance;
    }

    public float getDistance() {
        return distance;
    }

    public interface OnPositionChangedListener {
        public void positionChanged(float xMeters, float yMeters);
    }
}




Java Source Code List

com.echo.holographlibrary.BarGraph.java
com.echo.holographlibrary.Bar.java
com.echo.holographlibrary.LineGraph.java
com.echo.holographlibrary.LinePoint.java
com.echo.holographlibrary.Line.java
com.echo.holographlibrary.PieGraph.java
com.echo.holographlibrary.PieSlice.java
com.echo.holographlibrary.Utils.java
com.trifork.ibeacon.ApplicationContext.java
com.trifork.ibeacon.ApplicationModule.java
com.trifork.ibeacon.BaseActivity.java
com.trifork.ibeacon.BaseApplication.java
com.trifork.ibeacon.BaseFragment.java
com.trifork.ibeacon.BootBroadcastReceiver.java
com.trifork.ibeacon.MainActivity.java
com.trifork.ibeacon.database.Dao.java
com.trifork.ibeacon.database.Database.java
com.trifork.ibeacon.database.RegionHistoryCursorLoader.java
com.trifork.ibeacon.database.RegionHistoryEntry.java
com.trifork.ibeacon.detectors.BeaconController.java
com.trifork.ibeacon.detectors.IBeaconParser.java
com.trifork.ibeacon.eventbus.FullScanCompleteEvent.java
com.trifork.ibeacon.eventbus.NewBeaconSelectedEvent.java
com.trifork.ibeacon.eventbus.OttoEvent.java
com.trifork.ibeacon.eventbus.RangeScanCompleteEvent.java
com.trifork.ibeacon.eventbus.RequestBeaconScanEvent.java
com.trifork.ibeacon.eventbus.RequestBeaconTransmit.java
com.trifork.ibeacon.eventbus.RequestFullScanEvent.java
com.trifork.ibeacon.eventbus.StopFullScanEvent.java
com.trifork.ibeacon.eventbus.StopScanEvent.java
com.trifork.ibeacon.eventbus.StopTransmitEvent.java
com.trifork.ibeacon.ui.BeaconDataFragment.java
com.trifork.ibeacon.ui.LocationFragment.java
com.trifork.ibeacon.ui.NotificationFragment.java
com.trifork.ibeacon.ui.RangingFragment.java
com.trifork.ibeacon.ui.RegionLogFragment.java
com.trifork.ibeacon.ui.ScanFragment.java
com.trifork.ibeacon.util.CircularBuffer.java
com.trifork.ibeacon.util.PersistentState.java
com.trifork.ibeacon.util.Utils.java
com.trifork.ibeacon.widgets.BeaconView.java
com.trifork.ibeacon.widgets.LocationTrackerView.java