Last active
March 25, 2022 22:03
-
-
Save Jawnnypoo/fcceea44be628c2d5ae1 to your computer and use it in GitHub Desktop.
Accelerometer example that shows moving a circle based on accelerometer input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jawnnypoo.example; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.pm.ActivityInfo; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
/** | |
* Example activity that contains a view that reads accelerometer sensor input and | |
* translates a circle based on the changes. | |
*/ | |
public class AccelerometerActivity extends Activity implements SensorEventListener { | |
private SensorManager mSensorManager; | |
private Sensor mAccelerometer; | |
private AnimatedView mAnimatedView = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
mAnimatedView = new AnimatedView(this); | |
//Set our content to a view, not like the traditional setting to a layout | |
setContentView(mAnimatedView); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mSensorManager.unregisterListener(this); | |
} | |
@Override | |
public void onAccuracyChanged(Sensor arg0, int arg1) { } | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
mAnimatedView.onSensorEvent(event); | |
} | |
} | |
public class AnimatedView extends View { | |
private static final int CIRCLE_RADIUS = 25; //pixels | |
private Paint mPaint; | |
private int x; | |
private int y; | |
private int viewWidth; | |
private int viewHeight; | |
public AnimatedView(Context context) { | |
super(context); | |
mPaint = new Paint(); | |
mPaint.setColor(Color.MAGENTA); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
viewWidth = w; | |
viewHeight = h; | |
} | |
public void onSensorEvent (SensorEvent event) { | |
x = x - (int) event.values[0]; | |
y = y + (int) event.values[1]; | |
//Make sure we do not draw outside the bounds of the view. | |
//So the max values we can draw to are the bounds + the size of the circle | |
if (x <= 0 + CIRCLE_RADIUS) { | |
x = 0 + CIRCLE_RADIUS; | |
} | |
if (x >= viewWidth - CIRCLE_RADIUS) { | |
x = viewWidth - CIRCLE_RADIUS; | |
} | |
if (y <= 0 + CIRCLE_RADIUS) { | |
y = 0 + CIRCLE_RADIUS; | |
} | |
if (y >= viewHeight - CIRCLE_RADIUS) { | |
y = viewHeight - CIRCLE_RADIUS; | |
} | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
canvas.drawCircle(x, y, CIRCLE_RADIUS, mPaint); | |
//We need to call invalidate each time, so that the view continuously draws | |
invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I am new to android and I found this code of yours, I want to set the background image, How do I do that dynamically ?. Because we are not setting the layout file in the above example..