WindowManager,layoutInflater
参考:
WindowManager 的部分源码:
public interface WindowManager extends ViewManager {
//静态内部类
public static class LayoutParams extends ViewGroup.LayoutParams implements Parcelable {
//构造方法
public LayoutParams() {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
type = TYPE_APPLICATION;
format = PixelFormat.OPAQUE;
}
public LayoutParams(int _type) {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
type = _type;
format = PixelFormat.OPAQUE;
}
public LayoutParams(int _type, int _flags) {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
type = _type;
flags = _flags;
format = PixelFormat.OPAQUE;
}
public LayoutParams(int _type, int _flags, int _format) {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
type = _type;
flags = _flags;
format = _format;
}
public LayoutParams(int w, int h, int _type, int _flags, int _format) {
super(w, h);
type = _type;
flags = _flags;
format = _format;
}
public LayoutParams(int w, int h, int xpos, int ypos, int _type,
int _flags, int _format) {
super(w, h);
x = xpos;
y = ypos;
type = _type;
flags = _flags;
format = _format;
}
//.........................
//window主要的type分为三种:
//1. 应用窗口(从FIRST_APPLICATION_WINDOW到LAST_APPLICATION_WINDOW),是普通的顶层应用窗口,
//对这类window而言,它们的token必须设置给它们所属的activity的token
//2. 子窗口(从FIRST_SUB_WINDOW到LAST_SUB_WINDOW),与另一个顶层的window关联。
//这些window的token必须与它们依附的window的token一致。
//3. 系统窗口(从FIRST_SYSTEM_WINDOW到lAST_SYSTEM_WINDOW),是系统出于特别的目的使用的一类特殊的窗口。
//它们不应被应用随意使用,而且使用时需要特殊的权限。
public int type;
//所需的位图格式。可能是PixelFormat中的常量之一。默认值是不透明的。
public int format;
//各种行为选项/标志。默认值为无。
public int flags;
}
}
type:
在Android系统中,PopupWindow、Dialog、Activity、Toast等都有窗口的概念,但又各有不同,Android将窗口大致分为三类:应用窗口、子窗口、系统窗口。
其中,Activity与Dialog属于应用窗口、PopupWindow属于子窗口,必须依附到其他非子窗口才能存在,而Toast属于系统窗口,
Dialog可能比较特殊,从表现上来说偏向于子窗口,必须依附Activity才能存在,但是从性质上来说,仍然是应用窗口,有自己的WindowToken。
application:
sub:
system:
flag:
WindowManager.LayoutParams的各种flag含义
format:
PixelFormat 的源码:
public class PixelFormat {
public static final int UNKNOWN = 0;
/** System chooses a format that supports translucency (many alpha bits) */
public static final int TRANSLUCENT = -3; //半透明 ,[trænsˈlu:snt]
/**
* System chooses a format that supports transparency
* (at least 1 alpha bit)
*/
public static final int TRANSPARENT = -2; //透明 , [trænsˈpærənt]
/** System chooses an opaque format (no alpha bits required) */
public static final int OPAQUE = -1; //不透明, [əʊˈpeɪk]
public static final int RGBA_8888 = 1;
public static final int RGBX_8888 = 2;
public static final int RGB_888 = 3;
public static final int RGB_565 = 4;
public static final int RGBA_5551 = 6;
public static final int RGBA_4444 = 7;
public static final int A_8 = 8;
public static final int L_8 = 9;
public static final int LA_88 = 0xA;
public static final int RGB_332 = 0xB;
public static final int YCbCr_422_SP= 0x10;
public static final int YCbCr_420_SP= 0x11;
public static final int YCbCr_422_I = 0x14;
public static final int JPEG = 0x100;
public static void getPixelFormatInfo(int format, PixelFormat info) {
switch (format) {
case RGBA_8888:
case RGBX_8888:
info.bitsPerPixel = 32;
info.bytesPerPixel = 4;
break;
case RGB_888:
info.bitsPerPixel = 24;
info.bytesPerPixel = 3;
break;
case RGB_565:
case RGBA_5551:
case RGBA_4444:
case LA_88:
info.bitsPerPixel = 16;
info.bytesPerPixel = 2;
break;
case A_8:
case L_8:
case RGB_332:
info.bitsPerPixel = 8;
info.bytesPerPixel = 1;
break;
case YCbCr_422_SP:
case YCbCr_422_I:
info.bitsPerPixel = 16;
info.bytesPerPixel = 1;
break;
case YCbCr_420_SP:
info.bitsPerPixel = 12;
info.bytesPerPixel = 1;
break;
default:
throw new IllegalArgumentException("unknown pixel format " + format);
}
}
public static boolean formatHasAlpha(int format) {
switch (format) {
case PixelFormat.A_8:
case PixelFormat.LA_88:
case PixelFormat.RGBA_4444:
case PixelFormat.RGBA_5551:
case PixelFormat.RGBA_8888:
case PixelFormat.TRANSLUCENT:
case PixelFormat.TRANSPARENT:
return true;
}
return false;
}
public int bytesPerPixel; //每像素多少字节
public int bitsPerPixel; //每像素多少位
public static boolean isPublicFormat(int format) {
switch (format) {
case RGBA_8888:
case RGBX_8888:
case RGB_888:
case RGB_565:
return true;
}
return false;
}
}
ImageFormat 源码:
package android.graphics;
public class ImageFormat {
public static final int UNKNOWN = 0;
public static final int RGB_565 = 4;
public static final int YV12 = 0x32315659;
public static final int Y8 = 0x20203859;
public static final int Y16 = 0x20363159;
public static final int NV16 = 0x10;
public static final int NV21 = 0x11;
public static final int YUY2 = 0x14;
public static final int JPEG = 0x100;
public static final int YUV_420_888 = 0x23;
public static final int YUV_422_888 = 0x27;
public static final int YUV_444_888 = 0x28;
public static final int FLEX_RGB_888 = 0x29;
public static final int FLEX_RGBA_8888 = 0x2A;
public static final int RAW_SENSOR = 0x20;
public static final int RAW_PRIVATE = 0x24;
public static final int RAW10 = 0x25;
public static final int RAW12 = 0x26;
public static final int DEPTH16 = 0x44363159;
public static final int DEPTH_POINT_CLOUD = 0x101;
public static final int PRIVATE = 0x22;
public static int getBitsPerPixel(int format) { //每像素多少位
switch (format) {
case RGB_565:
return 16;
case NV16:
return 16;
case YUY2:
return 16;
case YV12:
return 12;
case Y8:
return 8;
case Y16:
case DEPTH16:
return 16;
case NV21:
return 12;
case YUV_420_888:
return 12;
case YUV_422_888:
return 16;
case YUV_444_888:
return 24;
case FLEX_RGB_888:
return 24;
case FLEX_RGBA_8888:
return 32;
case RAW_SENSOR:
return 16;
case RAW10:
return 10;
case RAW12:
return 12;
}
return -1;
}
public static boolean isPublicFormat(int format) {
switch (format) {
case RGB_565:
case NV16:
case YUY2:
case YV12:
case JPEG:
case NV21:
case YUV_420_888:
case YUV_422_888:
case YUV_444_888:
case FLEX_RGB_888:
case FLEX_RGBA_8888:
case RAW_SENSOR:
case RAW_PRIVATE:
case RAW10:
case RAW12:
case DEPTH16:
case DEPTH_POINT_CLOUD:
case PRIVATE:
return true;
}
return false;
}
}
Bitmap.Config 的源码:
public final class Bitmap implements Parcelable {
public enum Config {
ALPHA_8 (1), //1像素占1字节
RGB_565 (3), //1像素占2字节
ARGB_4444 (4), //1像素占2字节
ARGB_8888 (5); //1像素占4字节
final int nativeInt;
private static Config sConfigs[] = {
null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888
};
Config(int ni) {
this.nativeInt = ni;
}
static Config nativeToConfig(int ni) {
return sConfigs[ni];
}
}
}
获取
//获取WindowManager.LayoutParams
WindowManager.LayoutParams layoutParams1 = getWindow().getAttributes();//源码可查本质为无参数构造方法
WindowManager.LayoutParams layoutParams2 = new WindowManager.LayoutParams();
//获取windowManager
WindowManager windowManager1 = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
WindowManager windowManager2 = this.getWindowManager();
WindowManager windowManager3 = this.getWindow().getWindowManager();
//获取layoutInflater
LayoutInflater inflater1 = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//activity的方法
LayoutInflater inflater2 = LayoutInflater.from(this);//Fragment可用,LayoutInflater的方法,
LayoutInflater inflater3 = this.getLayoutInflater();//activity的方法
LayoutInflater inflater4 = this.getWindow().getLayoutInflater();