package safeluck.drive.evaluation.util;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.res.Resources;
|
import android.graphics.Bitmap;
|
import android.telephony.TelephonyManager;
|
import android.util.DisplayMetrics;
|
import android.util.Log;
|
import android.view.Surface;
|
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.EncodeHintType;
|
import com.google.zxing.MultiFormatWriter;
|
import com.google.zxing.WriterException;
|
import com.google.zxing.common.BitMatrix;
|
|
import java.util.Hashtable;
|
|
import static android.graphics.Color.BLACK;
|
|
|
public final class DimenUtil {
|
|
|
private static final String TAG = "DimenUtil";
|
|
public static int getScreenWidth(Context context ) {
|
final Resources resources = context.getResources();
|
final DisplayMetrics dm = resources.getDisplayMetrics();
|
return dm.widthPixels;
|
}
|
|
public static int getScreenHeight(Context context) {
|
final Resources resources = context.getResources();
|
final DisplayMetrics dm = resources.getDisplayMetrics();
|
return dm.heightPixels;
|
}
|
|
public static int getDisplayRotation(Activity activity) {
|
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
switch (rotation) {
|
case Surface.ROTATION_0:
|
return 0;
|
case Surface.ROTATION_90:
|
return 90;
|
case Surface.ROTATION_180:
|
return 180;
|
case Surface.ROTATION_270:
|
return 270;
|
}
|
return 0;
|
}
|
|
public static Bitmap createQRCode(String str, int widthAndHeight)
|
throws WriterException {
|
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
|
BitMatrix matrix = new MultiFormatWriter().encode(str,
|
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
|
|
int width = matrix.getWidth();
|
int height = matrix.getHeight();
|
int[] pixels = new int[width * height];
|
//画黑点
|
for (int y = 0; y < height; y++) {
|
for (int x = 0; x < width; x++) {
|
if (matrix.get(x, y)) {
|
pixels[y * width + x] = BLACK; //0xff000000
|
}
|
}
|
}
|
Bitmap bitmap = Bitmap.createBitmap(width, height,
|
Bitmap.Config.ARGB_8888);
|
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
|
return bitmap;
|
}
|
|
public static int px2dp(Context context, float pxValue) {
|
float scale = context.getResources().getDisplayMetrics().density;
|
Log.i(TAG,"density="+scale);
|
return (int) (pxValue / scale + 0.5f);// + 0.5f是为了让结果四舍五入
|
}
|
|
public static int px2sp(Context context, float pxValue) {
|
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
|
Log.i(TAG,"scaledDensity 1.2625="+fontScale);
|
return (int) (pxValue / fontScale + 0.5f);
|
}
|
|
}
|