共计 3795 个字符,预计需要花费 10 分钟才能阅读完成。
环境
win10 + android studio
文件
IRotationWatcher.aidl
package android.view;
interface IRotationWatcher {void onRotationChanged(int rotation);
}
IWindowManager.aidl
// IWindowManager.aidl
package android.view;
import android.graphics.Point;
import android.view.IRotationWatcher;
/***
* Define this same aidl file here to make it compiling when calling system hidden APIs.
*
* According to the 'delegation model' for loading classes on Java plattform, the actual
* implementation of IWindowManager from the Framework will be applied.
*/
interface IWindowManager {void getInitialDisplaySize(int displayId, out Point size);
void getBaseDisplaySize(int displayId, out Point size);
void getRealDisplaySize(out Point paramPoint);
/**
* Remove a rotation watcher set using watchRotation.
* @hide
*/
void removeRotationWatcher(IRotationWatcher watcher);
}
配置
在我的项目内创立 aidl 文件目录
留神我的项目包名必须与零碎包名统一否则报错
文件配置好后须要
build->make project
这样在我的项目文件内就能够应用了
DisplayUtil.java 工具类
package com.xxx.xxx;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Display;
import android.view.IRotationWatcher;
import android.view.IWindowManager;
import java.lang.reflect.Method;
/** Created by Sean on 5/27/17. */
/* package */ final class DisplayUtil {
private IWindowManager iWindowManager;
@SuppressLint("PrivateApi")
public DisplayUtil() {
Class<?> serviceManagerClass = null;
try {serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getDeclaredMethod("getService", String.class);
// WindowManager
Object ws = getService.invoke(null, Context.WINDOW_SERVICE);
iWindowManager = IWindowManager.Stub.asInterface((IBinder) ws);
} catch (Exception e) {e.printStackTrace();
}
}
/**
* * Retrieves the device actual display size.
*
* @return {@link Point}
*/
Point getCurrentDisplaySize() {
try {Point localPoint = new Point();
// Resolve the screen resolution for devices with OS version 4.3+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {iWindowManager.getInitialDisplaySize(0, localPoint);
} else {// void getDisplaySize(out Point size)
Point out = new Point();
iWindowManager
.getClass()
.getMethod("getDisplaySize", Point.class)
.invoke(iWindowManager, out);
if (out.x > 0 && out.y > 0) {localPoint = out;}
}
System.out.println(">>> Dimension:" + localPoint);
return localPoint;
} catch (Exception e) {e.printStackTrace();
}
return null;
}
/**
* Retrieve the current orientation of the primary screen.
*
* @return Constant as per {@link android.view.Surface.Rotation}.
* @see Display#DEFAULT_DISPLAY
*/
int getScreenRotation() {
int rotation = 0;
try {Class<?> cls = iWindowManager.getClass();
try {rotation = (Integer) cls.getMethod("getRotation").invoke(iWindowManager);
} catch (NoSuchMethodException e) {
rotation =
(Integer) cls.getMethod("getDefaultDisplayRotation").invoke(iWindowManager);
}
} catch (Exception e) {e.printStackTrace();
}
System.out.println(">>> Screen rotation:" + rotation);
return rotation;
}
void setRotateListener(RotateListener listener) {
try {Class<?> clazz = iWindowManager.getClass();
IRotationWatcher watcher =
new IRotationWatcher.Stub() {
@Override
public void onRotationChanged(int rotation) throws RemoteException {if (listener != null) {listener.onRotate(rotation);
}
}
};
try {clazz.getMethod("watchRotation", IRotationWatcher.class, int.class)
.invoke(iWindowManager, watcher, Display.DEFAULT_DISPLAY); // 26+
} catch (NoSuchMethodException ex) {clazz.getMethod("watchRotation", IRotationWatcher.class)
.invoke(iWindowManager, watcher);
}
} catch (Exception e) {e.printStackTrace();
}
}
Bitmap rotateBitmap(Bitmap bitmap, float degree) {Matrix matrix = new Matrix();
matrix.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
interface RotateListener {void onRotate(int rotate);
}
}
应用
DisplayUtil du = new DisplayUtil();
Point size = du.getCurrentDisplaySize();
// 屏幕分辩率
System.out.println(size.x);
System.out.println(size.y);
起源:https://github.com/rayworks/D…
正文完