Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

onSensorChanged Android example



Sensor, getSystemService(), getSensorList(), getDefaultDisplay(), WindowManager,
Surface.ROTATION_0, onAccuracyChanged() Android sample.

public class _MotionActivity extends Activity implements SensorEventListener {
    private float mSensorX;
    private float mSensorY;
    private Display mDisplay;
    private SensorManager sm;
    private PowerManager mPowerManager;
    private WindowManager mWindowManager;
   
    TextView textview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get an instance of the SensorManager
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
        	Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        	sm.registerListener(this,s, SensorManager.SENSOR_DELAY_NORMAL);
        }

        // Get an instance of the PowerManager
        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);

        // Get an instance of the WindowManager
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mDisplay = mWindowManager.getDefaultDisplay();

 
        
        setContentView(R.layout.main);
        textview = (TextView)findViewById(R.id.textView1);
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {

    	
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;

        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
                textview.setText(String.valueOf( mSensorX));
                break;
        }

    }

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onResume() {
		super.onResume();

             if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
        	Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        	sm.registerListener(this,s, SensorManager.SENSOR_DELAY_NORMAL);
             }
	}

	@Override
	protected void onPause() {
		
		sm.unregisterListener(this);
		super.onStop();
	}       
    
}


397LW NO topic_id



Další témata ....(Topics)


226

Eclipse my templates store Android | eclipse-my-templates-store-android


Where file(s) does eclipse store Java code style settings to?
If you need copy any old templates to the new workspace.

workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.ui.prefs


You can edit the Eclipse Templates from menu Window - Preferences - Java - Editor - Templates.
369

View getId is null Android | view-getid-is-null-android


If you create a button or view programmatically with OnClickListener you can set a tag key before button in parent layout is added.
And get correct button by this tag getTag() instead getId() in OnClickListener etc.

Button button = new Button(getApplicationContext());
int idOfButton = button.getId(); // return -1

button.setTag("my_button");
String sTag = (String) button.getTag(); // return "my_button"

140

Create Bitmap with Linear Gradient Android example | create-bitmap-with-linear-gradient-android-example



public class ApokusActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			canvas.drawColor(Color.GREEN);
            
            Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
             paint.setColor(Color.BLUE);
            
            Shader mShader = new LinearGradient(0, 0, 100, 70, new int[] {
                    Color.RED, Color.GREEN, Color.BLUE },
                    null, Shader.TileMode.MIRROR);  // CLAMP MIRROR REPEAT
            Canvas c = new Canvas(b);
            paint.setShader(mShader);
            //c.drawCircle(60, 60, 30, paint);
            c.drawRect(0, 0, 200, 200, paint);
            canvas.drawBitmap(b, 10,10, paint);
		}

	}
}


linear gradient bitmap android example
90

Map TreeMap get values to array Java Android example | map-treemap-get-values-to-array-java-android-example



import java.util.Map;
import java.util.TreeMap;

public class MainClass {
	public static void main(String[] arg) {

		// english;germany dictionary
		String[] arrayOfString = { "one;eine", "two;zwei", "two sets of;zwei"
				, "three;drei", "four;vier" };

		Map<String, String> map = new TreeMap<String, String>();

		for (String s : arrayOfString) {
			String[] array = s.split(";");
			String sKey = "", sValue = "";
			if (array.length > 1) {
				sKey = array[0];
				sValue = array[1];
				map.put(sKey, sValue);
			}
		}
    Object[] objectArrayOfValues = map.values().toArray();
	for (int i = 0; i < objectArrayOfValues.length; i++) {
		System.out.println(objectArrayOfValues[i]);
	}
		 
		
	}// end main

}
/*
vier
eine
drei
zwei
zwei
 */
177

Statistics usability of mobile phones with Android – February 2012 | statistics-usability-of-mobile-phones-with-android-%e2%80%93-february-2012


Table of usability of mobile phones with Android – February 2012 - (application Sky Map)















1 Samsung Galaxy S2 6.3%
2 HTC Desire HD 3.5%
3 HTC Evo 4G 3.4%
4 Samsung Galaxy Ace 2.1%
5 Samsung Galaxy S (SCH-I500) 2.1%
6 Samsung Galaxy S (GT-I9000) 2.1%
7 HTC Incredible 2 2.0%
8 Motorola Droid X 1.9%
9 Motorola Droid RAZR 1.9%
10 HTC Sensation 4G 1.7%



Editace Jana Žákovská : 2011-11-07 14:05:30
Počet článků v kategorii: 397
Url:onsensorchanged-android-example
Share
Share
Tweet