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


AD MOB

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




AD

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


94

Is NaN isNaN isInfinite number value Java Android example | is-nan-isnan-java-example


float, double is NaN isNaN() isInfinite() Java Android example.

float f = 0.f;
boolean bIsNaN = Float.isNaN(f);
boolean bIsInfinite = Float.isInfinite(f);

 double d = Math.sqrt(-10);
 boolean b = Double.isNaN(d);

322

View overrides onTouchEvent but not performClick | view-overrides-ontouchevent-but-not-performclick



public class Panel extends SurfaceView implements SurfaceHolder.Callback {
//............... code
//............... some code
   /**
     * Process the MotionEvent.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
        synchronized (getHolder()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
            	performClick();
             } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            	if(_currentGraphic==null)return true;
                
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
            }
            return true;
        }
    }
/////////////////////////////
 @Override
 public boolean performClick() {
  // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
 }

}

147

Resize a bitmap image Android example | resize-a-bitmap-image-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.YELLOW);
            
	         
	         paint.setFilterBitmap(true);
	         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.flower_blue);
	         int targetWidth  = bitmapOrg.getWidth() * 2;
	         int targetHeight = bitmapOrg.getHeight() * 2;
	         Bitmap bmp = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
	         RectF rectf = new RectF(0, 0, targetWidth, targetHeight);
	         Canvas c = new Canvas(bmp);
	         Path path = new Path();
	         path.addRect(rectf, Path.Direction.CW);
	         c.clipPath(path);
	         c.drawBitmap( bitmapOrg, new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()),
	                         new Rect(0, 0, targetWidth, targetHeight), paint);
	         Matrix matrix = new Matrix();
	         matrix.postScale(1f, 1f);
	         Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, targetWidth, targetHeight, matrix, true);
	         int h = bitmapOrg.getHeight();
           canvas.drawBitmap(bitmapOrg, 10,10, paint);
           canvas.drawBitmap(resizedBitmap, 10,10 + h + 10, paint);
           
		}

	}
}


resize bitmap android
247

Snow Text Effects Paint.NET | snow-text-effects-paint-net


For some graphics you can use Paint.NET editor.
//www.getpaint.net/
How create Snow Effects on Text by 1 minute show this video:


79

ArrayList Collections Add new Item Sort Find Item Java example | arraylist-collection-sort-add-java-example


How add item to ArrayList, sort ArrayList, search find index of item in ArrayList, min(), max() Java basic example.

import java.util.ArrayList;
import java.util.Collections;

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = {"nothing", "Hello", "people"
				, "bye-bye", "hello", "world!", "End" };
		   ArrayList<String> arrayList = new ArrayList<String>();
            for(String s: arrayOfString)
		     arrayList.add(s);

		    Collections.sort(arrayList);
		    // foreach
		    for (String str: arrayList)
		      System.out.println(str);
		    
		    Object objMin = Collections.min(arrayList);
		    System.out.println("Min is: " + objMin);
		    
		    Object objMax = Collections.max(arrayList);
		    System.out.println("Max is: " + objMax);
		    
		    int index = Collections.binarySearch(arrayList, "people");
		    System.out.println("Index of people is: " + index);
		    
		  }

		
	
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
*/


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

AD
11 Share
Share
Tweet