Ich versteh absolut nicht warum invalidate() auf einmal meine onDraw() Methode in meiner Panel Klasse nicht mehr aufruft. Es hatte schon mal funktioniert aber jetzt wird onDraw nur noch einmal und dann nie wieder aufgerufen. obwohl invalidate() ständig aufgerufen wird.
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testtext" />
<org.phobos.kompass.Panel
android:id="@+id/panel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>[/XML]
Java:
package org.phobos.kompass;
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.os.Bundle;
import android.util.AttributeSet;
import android.view.Window;
import android.widget.TextView;
public class KompassActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mOrientation;
private Panel mPanel;
TextView textFenster;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
AttributeSet attrs = null;
mPanel = new Panel(this, attrs);
mPanel.setRoll_angle(0);
mPanel.setAzimuth_angle(0);
textFenster = (TextView) findViewById(R.id.textView1);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
// You must implement this callback in your code.
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mOrientation,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onSensorChanged(SensorEvent event) {
float azimuth_angle = event.values[0];
float pitch_angle = event.values[1];
float roll_angle = event.values[2];
// Text ausgeben
// "Azimuth:" + String.format("%.2f", azimuth_angle) + "°", 0, 50,
// "x-Neigung:" + String.format("%.2f", pitch_angle) + "°", 0,
// 100, mPaint);
// "y-Neigung:" + String.format("%.2f", roll_angle) + "°",
// 0, 150, mPaint);
textFenster.setText("Azimuth: " + String.format("%.2f", azimuth_angle)
+ "°");
mPanel.setRoll_angle(roll_angle);
mPanel.setPitch_angle(pitch_angle);
mPanel.setAzimuth_angle(azimuth_angle);
mPanel.invalidate();
}
}
Java:
package org.phobos.kompass;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
// Diese Klasse ist für das Zeichnen auf dem Display zuständig
public class Panel extends View {
// Variablen deklarieren
private int xpos; // Position der
private int ypos; // Kugel
private float pitch_angle; // Sensordaten
private float roll_angle;
private float azimuth_angle;
// Konstruktor
public Panel(Context context, AttributeSet attrs) {
super(context, attrs);
}
// In der onDraw Methode wird auf den Bildschirm gezeichnet
@Override
public void onDraw(Canvas canvas) {
// // kleine Pause zum Akku sparen
// try {
// Thread.sleep(33);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// Hintergrund zeichnen
Bitmap background = BitmapFactory.decodeResource(getResources(),
R.drawable.background);
Rect dst = new Rect();
dst.set(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(background, null, dst, null);
// Kompass drehen und zeichnen
Bitmap komp = BitmapFactory.decodeResource(getResources(),
R.drawable.kom_blatt);
Matrix mat = new Matrix();
mat.postRotate(360 - azimuth_angle);
Bitmap komp_gedr = Bitmap.createBitmap(komp, 0, 0, komp.getWidth(),
komp.getHeight(), mat, true);
int xversatz = (komp_gedr.getWidth() - komp.getWidth()) / 2;
int yversatz = (komp_gedr.getHeight() - komp.getHeight()) / 2;
canvas.drawBitmap(komp_gedr, canvas.getWidth() / 2 - komp.getWidth()
/ 2 - xversatz, canvas.getHeight() / 2 - komp.getHeight() / 2
- yversatz, null);
// Kugelposition berechnen und zeichnen
Bitmap kugel = BitmapFactory.decodeResource(getResources(),
R.drawable.fadenkreuz);
xpos = (canvas.getWidth() / 2)
+ Math.round((roll_angle / 180) * canvas.getWidth())
- kugel.getWidth() / 2;
ypos = (canvas.getHeight() / 2)
+ Math.round((pitch_angle / 180) * canvas.getHeight())
- kugel.getHeight() / 2;
canvas.drawBitmap(kugel, xpos, ypos, null);
}
// Getter und Setter
public float getPitch_angle() {
return pitch_angle;
}
public void setPitch_angle(float pitch_angle) {
this.pitch_angle = pitch_angle;
}
public float getRoll_angle() {
return roll_angle;
}
public void setRoll_angle(float roll_angle) {
this.roll_angle = roll_angle;
}
public int getXpos() {
return xpos;
}
public void setXpos(int xpos) {
this.xpos = xpos;
}
public int getYpos() {
return ypos;
}
public void setYpos(int ypos) {
this.ypos = ypos;
}
public float getAzimuth_angle() {
return azimuth_angle;
}
public void setAzimuth_angle(float azimuth_angle) {
this.azimuth_angle = azimuth_angle;
}
}
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testtext" />
<org.phobos.kompass.Panel
android:id="@+id/panel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>[/XML]