Android Sensor Rotation Angle - java

I'm trying to detect the y-axis rotation angle (forward-backward tilt when holding in landscape) but I'm obviously doing thins wrong as the onSensorChanged event never gets triggered
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSensorManager.registerListener(this.mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
}
...
private SensorEventListener mSensorEventListener = new SensorEventListener() {
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
float aX = event.values[1];
float aY = event.values[2];
double angle = aY * (Math.PI / 180);
btnStop.setText(Double.toString(angle));
};

Your device probably does not have Sensor.TYPE_ROTATION_VECTOR

Related

android studio sensor HEART Rate app

I created a simple app to learn how to use sensor heart beat with one button and label here is my code:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
Button show;
TextView showHeartRate;
SensorManager sensorMgr;
Sensor heartRate;
String heartRateValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.button);
show.setOnClickListener(displayHeartRate);
showHeartRate = (TextView) findViewById(R.id.showHeartRate);
sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
heartRate = sensorMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
showHeartRate.setText(heartRateValue);
}
};
#Override
protected void onResume() {
super.onResume();
sensorMgr.registerListener(this,heartRate,SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
sensorMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
I add to the manifest:
<uses-permission android:name="android.permission.BODY_SENSORS"/>
the problem is that the onSensorChanged() is not called.
i check a lot of solutions here but i did not find anything
First, why would you expect it to ask permissions to use your camera if its heartrate sensor that you are trying to use?
Second, I think by the time you click 'display heartrate', you just don't have any data from the sensor yet (heartRateValue is empty), and when data finally comes you don't really update UI, you only update your state. What I suggest is to update your UI state on every sensor change, for example:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
showHeartRate.setText(heartRateValue);
}
or you can avoid code duplication (even thought its only one line), create a setter and and call it from both click handler and sensor handler:
private void setHeartrate(String rate) {
showHeartRate.setText(rate);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
setHeartrate(heartRateValue);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
setHeartrate(heartRateValue);
}
};
Third, you are not using data from your sensor, look at SensorEvent, data comes in values array and you are only trying to displat sensor type, why? Try this:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.values.length > 0 ? sensorEvent.values[0] : 0.0f);
showHeartRate.setText(heartRateValue);
}

Sensor change doesn't trigger onSensorChanged

I just started out on android and java yesterday, and i'm having a problem. I'm trying to output a sensor's values to some TextViews. The text updates, but with the values set at definition. onSensorChanged should update these values right?
import [...]
public class MainActivity extends Activity implements SensorEventListener {
public float[] gravity = {0,0,0};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView gravxTxtView = (TextView) findViewById(R.id.gravxTxtView);
final TextView gravyTxtView = (TextView) findViewById(R.id.gravyTxtView);
final TextView gravzTxtview = (TextView) findViewById(R.id.gravzTxtView);
final Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
#Override
public void run() {
gravxTxtView.setText("Gravity x: " + Float.toString((gravity[0])));
gravyTxtView.setText("Gravity y: " + Float.toString((gravity[1])));
gravzTxtview.setText("Gravity z: " + Float.toString((gravity[2])));
}
}, 500);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
gravity[0] = event.values[0];
gravity[1] = event.values[1];
gravity[2] = event.values[2];
}
}
}
There are some serious steps you are missing
1. Get the instance of sensor manager
SensorManager sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
2. Get the instance of the sensor
Sensor accel=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
3. Register sensor to the lister class which implements SensorEventListener in your case you have implemented on the MainActivity so it will be this
sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
now when certain change will happen it will call the onSensorChanged method ,please let me know its working or not

How to implement pan and pinch zoom gesture into android GridView?

I want to make a board using android GridView as follows -
I need to make the GridView zoomable using two fingers and also pannable.
What is a standard and easy way to do this?
I'm able to make an imageview zoomable but not pannable using the following code-
public class MainActivity extends Activity {
private ImageView imageView;
private float scale = 1f;
private ScaleGestureDetector detector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
detector = new ScaleGestureDetector(this,new ScaleListener());
}
public boolean onTouchEvent(MotionEvent event) {
// re-route the Touch Events to the ScaleListener class
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
float onScaleBegin = 0;
float onScaleEnd = 0;
#Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
imageView.setScaleX(scale);
imageView.setScaleY(scale);
return true;
}
#Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
Toast.makeText(getApplicationContext(),"Scale Begin" ,Toast.LENGTH_SHORT).show();
onScaleBegin = scale;
return true;
}
#Override
public void onScaleEnd(ScaleGestureDetector detector) {
Toast.makeText(getApplicationContext(),"Scale Ended",Toast.LENGTH_SHORT).show();
onScaleEnd = scale;
if (onScaleEnd > onScaleBegin){
Toast.makeText(getApplicationContext(),"Scaled Up by a factor of " + String.valueOf( onScaleEnd / onScaleBegin ), Toast.LENGTH_SHORT ).show();
}
if (onScaleEnd < onScaleBegin){
Toast.makeText(getApplicationContext(),"Scaled Down by a factor of " + String.valueOf( onScaleBegin / onScaleEnd ), Toast.LENGTH_SHORT ).show();
}
super.onScaleEnd(detector);
}
}
}
I tried by replacing the imageview part with gridview in this code. But that didn't work.How do I implement this properly?

Nothing getting drawn

This code shows nothing on my screen when run. I need to draw a simple circle wherever you touch.
public class Touchevent extends AppCompatActivity {
float mLastTouchX = 0;
float mLastTouchY = 0;
Canvas zex=new Canvas();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchevent);
RelativeLayout mylayout = (RelativeLayout)findViewById(R.id.test);
mylayout.setOnTouchListener(
new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent m) {
onTouchEvent(m);
onDraw(zex);
return true;
}
}
);
}
public void onDraw(Canvas canvasx) {
canvasx.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvasx.drawCircle(mLastTouchX,mLastTouchY,100,paint);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
mLastTouchX = ev.getX();
mLastTouchY = ev.getY();
return true;
}
}
The code shows no error. Do I need to add a custom view? Then how to implement a click listener?

How to draw 2D animation and update it dynamically by the user?

I am new to android. Sorry if this question is very silly...
I am trying to create a simple app.
The rule is .. whenever i touch or click a button which is located at the center, the radius of circle should get increased. And whenever i stopped clicking , then the radius of the circle should be reduced slowly. Here i created a circle, but i can't update it. I used button click listner and onTouchListner but nothing works... Please help me to do this app... Thank you so much for your help..
public class TestcircleActivity extends Activity {
/** Called when the activity is first created. */
Button b;
int clicks = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View implements OnTouchListener {
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
}

Categories