Android Open Source - 101AndroidApps Compass Details View






From Project

Back to project page 101AndroidApps.

License

The source code is released under:

Licenced under the Creative Commons Attribution 4.0 licence. For full text see http://creativecommons.org/licenses/by/4.0/

If you think the Android project 101AndroidApps 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.hulzenga.ioi.android.app_010;
/*from ww  w .ja v a 2 s . co m*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by jouke on 1-5-14.
 */
class CompassDetailsView extends View {

  private static final float   EPS             = Float.MIN_VALUE;
  private static final int     BUFFER_LENGTH   = 101;
  private static final  String  MEASUREMNTS_KEY = "MEASUREMENTS";
  private static final  String  STATE_KEY       = "STATE";
  private              float[] mMeasurements   = new float[BUFFER_LENGTH];
  private              float   mMinMeasurement = 0.0f;
  private              float   mMaxMeasurement = EPS;

  private Paint mLinePaint;
  private int   mHeight;
  private int   mWidth;
  private int   mHalfHeight;
  private float mStepWidth;
  private Paint mAxisPaint;
  private float mEstimatedFieldStrength;
  private Paint mTextPaint;
  private float mHeading = 0.0f;
  private float mTilt    = 0.0f;
  private int mDisplayRotation;

  public CompassDetailsView(Context context) {
    this(context, null);
  }

  public CompassDetailsView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CompassDetailsView(Context context, AttributeSet attrs, int style) {
    super(context, attrs, style);

    mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLinePaint.setColor(Color.RED);
    mLinePaint.setStrokeWidth(4.0f);

    mAxisPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mAxisPaint.setColor(Color.WHITE);
    mAxisPaint.setStrokeWidth(2.0f);

    mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setTextAlign(Paint.Align.LEFT);
    mTextPaint.setTextSize(20.0f);
    mTextPaint.setColor(Color.WHITE);
  }

  public void addMeasurement(float measurement) {

    float min = mMeasurements[1], max = mMeasurements[1];

    for (int i = 0; i < BUFFER_LENGTH - 1; i++) {
      mMeasurements[i] = mMeasurements[i + 1];

      if (mMeasurements[i] < min) {
        min = mMeasurements[i];
      } else if (mMeasurements[i] > max) {
        max = mMeasurements[i];
      }
    }

    mMeasurements[BUFFER_LENGTH - 1] = measurement;
    mMinMeasurement = min;
    mMaxMeasurement = max;

    invalidate();
  }


  public void updateOrientation(float[] eulerAngles) {

    if (Math.abs(eulerAngles[1]) > Math.abs(eulerAngles[2])) {
      mTilt = Math.abs(180.0f * (eulerAngles[1] / (float) Math.PI));
    } else {
      mTilt = Math.abs(180.0f * (eulerAngles[2] / (float) Math.PI));
    }

    switch (mDisplayRotation) {
      case Surface.ROTATION_0:
        mHeading = 180.0f * (eulerAngles[0] / (float) Math.PI);
        break;
      case Surface.ROTATION_90:
        mHeading = 180.0f * ((eulerAngles[0] + (float) Math.PI * 0.5f) / (float) Math.PI);
        break;
      case Surface.ROTATION_180:
        mHeading = 180.0f * ((eulerAngles[0] + (float) Math.PI) / (float) Math.PI);
        break;
      case Surface.ROTATION_270:
        mHeading = 180.0f * ((eulerAngles[0] + (float) Math.PI * 1.5f) / (float) Math.PI);
        break;
    }

    invalidate();
  }

  public void setEstimatedFieldStrength(float estimatedFieldStrength) {
    mEstimatedFieldStrength = estimatedFieldStrength;
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    mWidth = w - 2 * (int) MARGIN;
    mStepWidth = mWidth / (float) (BUFFER_LENGTH - 1);
    mHeight = h - 2 * (int) MARGIN;
    mHalfHeight = mHeight / 2;

    WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    mDisplayRotation = windowManager.getDefaultDisplay().getRotation();
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (state instanceof Bundle) {
      Bundle bundle = (Bundle) state;
      mMeasurements = bundle.getFloatArray(MEASUREMNTS_KEY);
      state = bundle.getParcelable(STATE_KEY);
    }
    super.onRestoreInstanceState(state);
  }

  @Override
  protected Parcelable onSaveInstanceState() {
    Bundle bundle = new Bundle();
    bundle.putFloatArray(MEASUREMNTS_KEY, mMeasurements);
    bundle.putParcelable(STATE_KEY, super.onSaveInstanceState());
    return bundle;
  }

  private static final float MARGIN = 3f;

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.translate(MARGIN, MARGIN);


//    canvas.scale(1.0f, -1.0f);
//    canvas.translate(MARGIN, -mHeight-MARGIN);

    canvas.drawLine(0.0f, mHeight, mWidth, mHeight, mAxisPaint);
    canvas.drawLine(0.0f, mHeight, 0.0f, mHalfHeight, mAxisPaint);

    float yScale = mHalfHeight / 100.0f;
//    //draw axis
//    canvas.drawLine(MARGIN, MARGIN, MARGIN, mHeight-MARGIN, mAxisPaint);
//    canvas.drawLine(MARGIN, )
//    float axisHeight;
//    if (mMaxMeasurement < 0.0f) {
//      axisHeight = 0.0f;
//    } else if (mMinMeasurement < 0.0f) {
//      axisHeight = (mMaxMeasurement / (mMaxMeasurement - mMinMeasurement)) * mHeight;
//    } else {
//      axisHeight = mHeight;
//    }
//    canvas.drawLine(0.0f, axisHeight, mWidth, axisHeight, mAxisPaint);
//
//    float yScale = mHeight / (mMaxMeasurement - mMinMeasurement - EPS);
    for (int i = 0; i < BUFFER_LENGTH - 1; i++) {
      canvas.drawLine(
          i * mStepWidth, mHeight - yScale * mMeasurements[i], (i + 1) * mStepWidth, mHeight - yScale * (mMeasurements[i + 1]),
          mLinePaint);
    }
    if (mEstimatedFieldStrength != 0.0f) {
      canvas.drawLine(0.0f, mHeight - yScale * mEstimatedFieldStrength, mWidth, mHeight - yScale * mEstimatedFieldStrength, mLinePaint);
    }

    canvas.drawText(String.format("Heading: %3.0f Tilt: %3.0f", mHeading, mTilt), 0.0f, mHalfHeight, mTextPaint);
//
//    if (mEstimatedFieldStrength != 0.0f) {
//      canvas.drawLine(0.0f, yScale * mEstimatedFieldStrength, 0.0f, yScale * mEstimatedFieldStrength, mLinePaint);
//    }
  }
}




Java Source Code List

com.hulzenga.ioi.android.AppActivity.java
com.hulzenga.ioi.android.AppDetailsDialog.java
com.hulzenga.ioi.android.App.java
com.hulzenga.ioi.android.TableOfContentsActivity.java
com.hulzenga.ioi.android.app_001.HelloWorldActivity.java
com.hulzenga.ioi.android.app_002.Ball.java
com.hulzenga.ioi.android.app_002.BouncyBallsActivity.java
com.hulzenga.ioi.android.app_002.BouncyBallsView.java
com.hulzenga.ioi.android.app_003.MonsterDatabaseActivity.java
com.hulzenga.ioi.android.app_003.MonsterEditDialog.java
com.hulzenga.ioi.android.app_003.MonsterGenerator.java
com.hulzenga.ioi.android.app_003.MonsterListAdapter.java
com.hulzenga.ioi.android.app_003.database.MonsterContract.java
com.hulzenga.ioi.android.app_003.database.MonsterDatabaseHelper.java
com.hulzenga.ioi.android.app_003.database.MonsterProvider.java
com.hulzenga.ioi.android.app_004.BouncyBall3dActivity.java
com.hulzenga.ioi.android.app_004.BouncyBall3dGLSurfaceView.java
com.hulzenga.ioi.android.app_004.BouncyBall3dRenderer.java
com.hulzenga.ioi.android.app_004.BouncyBallGame.java
com.hulzenga.ioi.android.app_005.ElementActivity.java
com.hulzenga.ioi.android.app_005.ElementAdapter.java
com.hulzenga.ioi.android.app_005.ElementAnimator.java
com.hulzenga.ioi.android.app_005.ElementSnakeView.java
com.hulzenga.ioi.android.app_005.ElementView.java
com.hulzenga.ioi.android.app_005.Element.java
com.hulzenga.ioi.android.app_006.EmptyFragment.java
com.hulzenga.ioi.android.app_006.ExposureFragment.java
com.hulzenga.ioi.android.app_006.SettingChangeListener.java
com.hulzenga.ioi.android.app_006.SettingIconFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuLvl2Fragment.java
com.hulzenga.ioi.android.app_006.Setting.java
com.hulzenga.ioi.android.app_006.SimpleCameraActivity.java
com.hulzenga.ioi.android.app_006.SimpleCameraPreview.java
com.hulzenga.ioi.android.app_007.ButtonsFragment.java
com.hulzenga.ioi.android.app_007.HighScores.java
com.hulzenga.ioi.android.app_007.LinksFragment.java
com.hulzenga.ioi.android.app_007.Menu.java
com.hulzenga.ioi.android.app_007.Review.java
com.hulzenga.ioi.android.app_007.Score.java
com.hulzenga.ioi.android.app_007.StatusFragment.java
com.hulzenga.ioi.android.app_007.WikiGameActivity.java
com.hulzenga.ioi.android.app_007.Wiki.java
com.hulzenga.ioi.android.app_008.OverheidActivity.java
com.hulzenga.ioi.android.app_008.OverheidFeedAdapter.java
com.hulzenga.ioi.android.app_008.OverheidFeedFragment.java
com.hulzenga.ioi.android.app_008.OverheidFeedItem.java
com.hulzenga.ioi.android.app_008.OverheidFeed.java
com.hulzenga.ioi.android.app_008.OverheidPagerAdapter.java
com.hulzenga.ioi.android.app_009.ThumbsUpActivity.java
com.hulzenga.ioi.android.app_009.ThumbsUpServer.java
com.hulzenga.ioi.android.app_009.ThumbsUpService.java
com.hulzenga.ioi.android.app_010.CompassActivity.java
com.hulzenga.ioi.android.app_010.CompassDetailsView.java
com.hulzenga.ioi.android.app_010.CompassView.java
com.hulzenga.ioi.android.util.Constrain.java
com.hulzenga.ioi.android.util.DeveloperTools.java
com.hulzenga.ioi.android.util.FileManager.java
com.hulzenga.ioi.android.util.ScreenMetrics.java
com.hulzenga.ioi.android.util.open_gl.ColorFunctionFactory.java
com.hulzenga.ioi.android.util.open_gl.ColorFunction.java
com.hulzenga.ioi.android.util.open_gl.ShaderTools.java
com.hulzenga.ioi.android.util.open_gl.engine.NodeController.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneGraph.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneNode.java
com.hulzenga.ioi.android.util.open_gl.geometry.Box.java
com.hulzenga.ioi.android.util.open_gl.geometry.Cylinder.java
com.hulzenga.ioi.android.util.open_gl.geometry.Geometry.java
com.hulzenga.ioi.android.util.open_gl.geometry.Grid.java
com.hulzenga.ioi.android.util.open_gl.geometry.Sphere.java
com.hulzenga.ioi.android.util.open_gl.geometry.Transform.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec3.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec4.java
fi.iki.elonen.NanoHTTPD.java