Skip to content

Instantly share code, notes, and snippets.

@yihongyuelan
Created August 8, 2014 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihongyuelan/596c9945756dd384c44e to your computer and use it in GitHub Desktop.
Save yihongyuelan/596c9945756dd384c44e to your computer and use it in GitHub Desktop.
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;
public class CommonUtils {
private static final float ONE_DIP = 1;
private static final float ONE_PIX = 1;
/**
* HVGA屏density=160
* QVGA屏density=120
* WVGA屏density=240
* WQVGA屏density=120
* http://developer.android.com/guide/practices/screens_support.html
* */
public static void dp2px(Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
int px = (int) ( ONE_DIP * scale + 0.5f);
Toast.makeText(context, "1 dip = "+px+ " px", Toast.LENGTH_LONG).show();
}
//if the device use navigation bar, it can not get the correct resolution
public static void getScreenResolution(Activity activity) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
String string = "Device Resolution is:" + displayMetrics.widthPixels + "x" + displayMetrics.heightPixels;
Toast.makeText(activity, string, Toast.LENGTH_LONG).show();
}
public int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
public int getNavigationBarHeight(Context context) {
int navigationBarHeight = 0;
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navigationBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return navigationBarHeight;
}
public static void getSensors(Context context) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
//Show the Sensor information
for (Sensor s : allSensors) {
switch (s.getType()) {
case Sensor.TYPE_ACCELEROMETER:
Log.i("Seven", "accelerometer");
break;
case Sensor.TYPE_GRAVITY:
Log.i("Seven", "gravity API 9");
break;
case Sensor.TYPE_GYROSCOPE:
Log.i("Seven", "gyroscope");
break;
case Sensor.TYPE_LIGHT:
Log.i("Seven", "light_sensor");
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
Log.i("Seven", "LINEAR_ACCELERATION API9");
break;
case Sensor.TYPE_MAGNETIC_FIELD:
Log.i("Seven", "magnetic field");
break;
case Sensor.TYPE_ORIENTATION:
Log.i("Seven", "orientation");
break;
case Sensor.TYPE_PRESSURE:
Log.i("Seven", "pressure");
break;
case Sensor.TYPE_PROXIMITY:
Log.i("Seven", "proximity");
break;
case Sensor.TYPE_ROTATION_VECTOR:
Log.i("Seven", "ROTATION");
break;
case Sensor.TYPE_TEMPERATURE:
Log.i("Seven", "temperature");
break;
default:
Log.i("Seven", "Unknown");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment