I want to log an event for telemetry when user zooms in the screen of an app in android.
Based on my research I could not find a system event that I can subscribe to determine if user zoomed the screen.
Any pointers to detect that?
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.accessibilityservice.AccessibilityService;
import android.graphics.Region;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements AccessibilityService.MagnificationController.OnMagnificationChangedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onMagnificationChanged(#NonNull AccessibilityService.MagnificationController magnificationController, #NonNull Region region, float v, float v1, float v2) {
float Scale = v;
// Scale will be changed as magnification is done on app
}
}
You are looking for Android Gestures
Here is a documentation about it:
https://developer.android.com/training/gestures
Here is a zoom/scale gesture:
https://developer.android.com/training/gestures/scale
and
Can we use scale gesture detector for pinch zoom in Android?
Related
I'm creating an animation bulb app in which there are two image views having blue and green bulb and two buttons(named blue and green).
On pressing them the image1's opacity increases and images2's decreases, the opposite happens when the other button is tapped(alpha values are 1 for both initially), but the animate keyword is showing up in red and the code is not compiling.
MainActivity :
package com.example.honey1.animatedbulb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public void greenTapped(View view)
{
ImageView blue=(ImageView)findViewById(R.id.bluebulb);
ImageView green=(ImageView)findViewById(R.id.greenbulb);
blue.animate.alpha(1).setDuration(2000);
green.animate.alpha(0).setDuration(2000);
}
public void blueTapped(View view)
{
ImageView blue=(ImageView)findViewById(R.id.bluebulb);
ImageView green=(ImageView)findViewById(R.id.greenbulb);
blue.animate.alpha(0).setDuration(2000);
green.animate.alpha(1).setDuration(2000);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Main activity screenshot :
XML file screenshot :
You just forgot () after animate()
And animate() is method not a property
Use this
blue.animate().alpha(1).setDuration(2000);
green.animate().alpha(0).setDuration(2000);
Instead of this
blue.animate.alpha(0).setDuration(2000);
green.animate.alpha(0).setDuration(2000);
View.animate() is a method, not a property or field.
It should be use like below code, should use .animate() not .animate:
The animate() method on a View object returns an ViewPropertyAnimator
object for the view.It provides a fluent API to typical animations
which can be performed on views.
Your code :
blue.animate.alpha(1).setDuration(2000);
green.animate.alpha(0).setDuration(2000);
Replace to :
blue.animate().alpha(1).setDuration(2000);
green.animate().alpha(0).setDuration(2000);
I'm trying to add an activity as a feature to an app I'm building where, the API will return a lat long, and with this lat long I will load google street view. Which with the movement of the device, will rotate the 360 degree angle of the position. I'm struggling on the movement part of the device. Using your fingers on the screen you can rotate. I wonder if anyone can point me in the right direction in getting the device movement to affect the position of the street view?
The code I have so far is:
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaFragment;
import com.google.android.gms.maps.StreetViewPanoramaOptions;
import com.google.android.gms.maps.StreetViewPanoramaView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.StreetViewPanoramaCamera;
import com.google.android.gms.maps.model.StreetViewPanoramaLocation;
public class MainActivity extends FragmentActivity
implements OnStreetViewPanoramaReadyCallback {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StreetViewPanoramaFragment streetViewPanoramaFragment =
(StreetViewPanoramaFragment) getFragmentManager()
.findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
}
#Override
public void onStreetViewPanoramaReady(final StreetViewPanorama panorama) {
final long duration = 1000;
float tilt = 30;
float bearing = 90;
final StreetViewPanoramaCamera camera = new StreetViewPanoramaCamera.Builder()
.zoom(panorama.getPanoramaCamera().zoom)
.bearing(bearing)
.tilt(tilt)
.build();
panorama.setPosition(new LatLng(52.208818, 0.090587));
panorama.setStreetNamesEnabled(false);
panorama.setZoomGesturesEnabled(false);
panorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
#Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
if (streetViewPanoramaLocation != null) {
panorama.animateTo(camera, duration);
}
Log.d(TAG, "TESTINGGGGGGGGGG");
}
});
}
}
I'm not sure given your question, so comment if i'm wrong but it seems you're able to rotate through this instruction
panorama.animateTo(camera, duration);
and you're moving to a specific location using the "camera" variable you built before.
So, if i understand correctly what you are trying to do, you have to check for mobilephone sensors (accelerometer & position) to get the motion then apply the correct motion to the panorama. Take a look at android sensor documentation in order to get the proper listeners (or how to register a sensor usage) then build the correct camera object according to the acceleration registered by the phone (left acceleration -> rotating left, right acceleration --> rotating right).
If you need a code example i'd suggest you to look this question which has some other links to help you using sensors and getting more doc.
If this does not help, comment and/or clarify the question.
As far as I know , moto360 doesn't have a sensor of type : TYPE_HEART_RATE, it's called passive wellness sensor.
The problem is that this wellness sensor is not giving me any data, as opposed to every other sensor that I've tried (like gravity, accelerometer...)
I've been waiting for more than 5 min but this sensor gives me data only when I start the app.
I've tried sdk20,sdk21,sdk22,sdk23 ... still no result
I also have the android.permission.BODY_SENSORS in my manifest
Question : How to get the sensor working, what can I do?
package com.x.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.WindowManager;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mHeartSensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
}
});
// keep watch screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Toast.makeText(getApplicationContext(), "Hi Oleg", Toast.LENGTH_LONG).show();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mHeartSensor = mSensorManager.getDefaultSensor(65538); //wellness sensor
mSensorManager.registerListener(this, mHeartSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == 65538) {
String msg = "" + (int) event.values[0];
Log.d("Main Activity", msg);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d("Main Activity", "accuracy : " + accuracy + " sensor : " + sensor.getName());
}
#Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this);
}
}
only output out of this "wellness" sensor (only when app starts) :
D/Main Activity: accuracy : 3 sensor : Wellness Passive Sensor
D/Main Activity: 0
As you know ,the value of TYPE_HEART_RATE is not equal to the Wellness Passive Sensor, but when I set as below:
Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
I find, I get the heart beat values.
By the way, don't forget to add permission.
I remember having huge problems specifically on the Moto 360 (first gen) when trying to read heart rate data. You're doing the correct thing using the passive wellness sensor as there isn't a heart rate monitor reported in the list of sensors.
Have a look at the Github repo here for inspiration (that's what I did to figure it out): https://github.com/pocmo/SensorDashboard
It's worth noting that even if you get it working, it's incredibly flaky. It's even flaky through the built in apps - have a play around with the Google Fit and Moto Body to take your heart rate, you'll see what I mean.
Google have API for fitness sensors, look at the following links
https://developers.google.com/fit/android/sensors
And project for example
https://github.com/googlesamples/android-fit/tree/master/BasicSensorsApi
I just started learning how to program for android and cant seem to get the x positions for when someone clicks on the screen to appear in log cat. (im using an emulator if that has to do with anything) when i do click on the screen nothing happens. Here is my program on startup i get no errors. also using a relative layout
package com.practice;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class PracticeActivity extends Activity implements OnTouchListener{
int touchX;
int touchY;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
onTouchEvent(event);
touchX = (int) event.getX();
touchY = (int) event.getY();
Log.d( "MOUSE", String.valueOf(touchX) );
return true;
}
}
Your OnTouchListener won't get called until you register it with a view. For example:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find a view in the layout
View view = findViewById(android.id.content);
// register the listener
view.setOnTouchListener(this);
}
Try using Integer.toString(touchX) and return false instead of true, other than that your code seems fine.
Emulator do register the touch events.
Also there is a utility called as DDMS, located in the tools folder of the SDK. When your emulator is running, use this utility to get the graphical logcat. You can filter messages as well.
alright so I'm new to programming for android, and I think I did something wrong, but I don't know what. I've looked at 3 different tutorials and my code seems to look like theirs. Can anyone tell me what I'm doing wrong? here is my src (altered from a android nehe tutorial).
package cypri.games;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
/**
* The initial Android Activity, setting and initiating
* the OpenGL ES Renderer Class #see Lesson02.java
*
* #author Savas Ziplies (nea/INsanityDesign)
*/
public class DGearActivity extends Activity {
/** The OpenGL View */
private GLSurfaceView glSurface;
DGear dGear = new DGear();
private static final String TAG = "DEBUG";
SensorManager sensorManager;
private final SensorEventListener sensorListener = new SensorEventListener(){
public void onSensorChanged(SensorEvent se) {
if (se.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
dGear.playerX = se.values[0];
Log.v(TAG, "se.values[0] =" + se.values[0]);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
/**
* Initiate the OpenGL View and set our own
* Renderer (#see Lesson02.java)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
//Create an Instance with this Activity
glSurface = new GLSurfaceView(this);
//Set our own Renderer
glSurface.setRenderer(dGear);
//Set the GLSurface as View to this Activity
setContentView(glSurface);
}
/**
* Remember to resume the glSurface
*/
#Override
protected void onResume() {
Log.v(TAG, "or");
super.onResume();
glSurface.onResume();
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
/**
* Also pause the glSurface
*/
#Override
protected void onPause() {
super.onPause();
glSurface.onPause();
Log.v(TAG, "op");
sensorManager.unregisterListener(sensorListener);
}
}
Are you running this in the emulator or on an actual device?
If you're running it on an actual device are you sure it has a gyroscope? There are lots of different sensor types and the gyroscope is only one of them. It could very well be one of the others.
Instead of only writing to the log if it's a gyroscope type, try writing the name of the se.sensor when that event fires. That way you'll at least know the event is firing.