Android: canvas.drawBitmap with Orientation(MagnetField)Sensor
- by user368374
hallo, i am developing Android app with orientation sensor.
now i got problem. What i want to do is, change scale and position of bitmap which
read from sd card. The scale and position depend on value from orientation sensor.
i use canvas.drawBitmap(), then it cause problem. the app is just shut down. other drawXXX()methods have no problem..any suggestion?
public class AnMagImgtestActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private MySurfaceView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
view = new MySurfaceView(this);
///make it fullscreen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
///give screen a hiropon
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(view);
}
@Override
protected void onResume() {
super.onResume();
List sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() 0) {
Sensor sensor = sensors.get(0);
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
view.onValueChanged(event.values);
}
class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
public MySurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onValueChanged(new float[3]);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
void onValueChanged(float[] values) {
Canvas canvas = getHolder().lockCanvas();
//String imgfn = "/sdcard/seekcamera"+"/"+regcode+"/to"+"/"+tempnum+".jpg";
String imgfn = "/sdcard/seekcamera"+"/de"+"/to"+"1.jpg";
//String imgfn = "/sdcard/seekcamera"+"/00"+"/00"+"/"+"42.jpg";
File mf = new File(imgfn);
Bitmap bitmap0 = BitmapFactory.decodeFile(mf.getPath());
if (canvas != null) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setTextSize(12);
canvas.drawColor(Color.BLACK);
paint.setAlpha(200);
for (int i = 0; i < values.length; i++) {
canvas.drawText("values[" + i + "]: " + values[i], 0, paint.getTextSize() * (i + 1), paint);
}
paint.setColor(Color.WHITE);
paint.setTextSize(100+values[2]);
paint.setAlpha( (int) (255-values[2]));
canvas.drawText("Germany", values[0]*1+20, paint.getTextSize() * 1+values[2]-80, paint);
///here is the problem..
canvas.drawBitmap(bitmap0, values[0],values[1], paint);
getHolder().unlockCanvasAndPost(canvas);
}
}
}
}