Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd textview in compass
    text
    copied!<p>I have to make an application where a compass indicate west point. Here is my code</p> <pre><code>public class CustomCompassActivity extends Activity implements SensorEventListener { Float azimut; // View to draw a compass public class CustomDrawableView extends View { Paint paint = new Paint(); TextView t1; public CustomDrawableView(Context context) { super(context); paint.setColor(0x8a2be2); paint.setStyle(Style.STROKE); paint.setStrokeWidth(4); paint.setAntiAlias(true); }; protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); int centerx = width/2; int centery = height/2; // Rotate the canvas with the azimut if (azimut != null) canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rose4); canvas.drawBitmap(bmp,centerx- bmp.getWidth()/2,centery - bmp.getHeight()/2, paint); canvas.drawText("W", centerx-0, centery-55, paint); paint.setColor(0xffd02090); } } CustomDrawableView mCustomDrawableView; private SensorManager mSensorManager; Sensor accelerometer; Sensor magnetometer; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCustomDrawableView = new CustomDrawableView(this); setContentView(mCustomDrawableView); // Register the sensor listeners mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } float[] mGravity; float[] mGeomagnetic; public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values; if (mGravity != null &amp;&amp; mGeomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); azimut = orientation[0]; // orientation contains: azimut, pitch and roll } } mCustomDrawableView.invalidate(); } } </code></pre> <p>I need to add textview.But i can not add them neither xml nor using textview inside the program.How can i add textview at the up level of the compass?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload