Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PaintEngine extends Engine implements SensorEventListener {
private final Paint[] mCardinalPaints = new Paint[4];
private final Runnable mDrawWP = new Runnable(){
public void run() {
drawFrame();
}
};
private SensorManager mSensMgr =
(SensorManager) getSystemService(SENSOR_SERVICE);
private boolean visible;
private float tiltValue;
private float rollValue;
private int[] paint0Loc;
private int[] paint1Loc;
private int[] paint2Loc;
private int[] paint3Loc;
PaintEngine(){
super();
setPaintProperties(mCardinalPaints);
}
private void setPaintProperties(Paint[] paints) {
paints[0].setARGB(255, 255, 0, 0);
paints[1].setARGB(255, 0, 0, 255);
paints[2].setARGB(255, 255, 255, 0);
paints[3].setARGB(255, 0, 255, 0);
for(Paint p : paints){
p.setAntiAlias(true);
p.setStrokeWidth(16);
p.setStrokeCap(Paint.Cap.ROUND);
p.setStyle(Paint.Style.FILL);
}
}
protected void drawFrame() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try{
canvas = holder.lockCanvas();
if(canvas!= null){
paint(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
handler.removeCallbacks(mDrawWP);
if(visible){
handler.postDelayed(mDrawWP, 1000/30);
}
}
private void paint(Canvas c){
paint0Loc[0] = paint0Loc[0] + (int)rollValue;
paint0Loc[1] = paint0Loc[1] + (int)tiltValue;
c.drawPoint(paint0Loc[0], paint0Loc[1], mCardinalPaints[0]);
paint1Loc[0] = paint1Loc[0] + (int)rollValue;
paint1Loc[1] = paint1Loc[1] + (int)tiltValue;
c.drawPoint(paint1Loc[0], paint1Loc[1], mCardinalPaints[1]);
paint2Loc[0] = paint2Loc[0] + (int)rollValue;
paint2Loc[1] = paint2Loc[1] + (int)tiltValue;
c.drawPoint(paint2Loc[0], paint2Loc[1], mCardinalPaints[2]);
paint3Loc[0] = paint3Loc[0] + (int)rollValue;
paint3Loc[1] = paint3Loc[1] + (int)tiltValue;
c.drawPoint(paint3Loc[0], paint3Loc[1], mCardinalPaints[3]);
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
this.visible = visible;
if(visible){
drawFrame();
}else{
handler.removeCallbacks(mDrawWP);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
visible=false;
handler.removeCallbacks(mDrawWP);
mSensMgr.unregisterListener(this);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
visible=true;
drawFrame();
Sensor snsr = mSensMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mSensMgr.registerListener(this, snsr, SensorManager.SENSOR_DELAY_UI);
paint0Loc[0] = holder.getSurfaceFrame().width() / 2;
paint0Loc[1] = 0;
paint1Loc[0] = holder.getSurfaceFrame().width();
paint1Loc[1] = holder.getSurfaceFrame().height() / 2;
paint2Loc[0] = holder.getSurfaceFrame().width() / 2;
paint2Loc[1] = holder.getSurfaceFrame().height();
paint3Loc[0] = 0;
paint3Loc[1] = holder.getSurfaceFrame().height() / 2;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
tiltValue = event.values[0];
rollValue = event.values[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.