‘Window’

来源:互联网 发布:mac可以装bilibili吗 编辑:程序博客网 时间:2024/06/02 21:45


LayoutInflater在Activity控制单元裁剪XML(View集合)装入Window。

WindowManagerGlobal.getInstance().startTrimMemory(TRIM_MEMORY_COMPLETE); 让应用的一系列GPU缓存被清理。

Window:获取当前窗体,并添加V: getWindowManager().addView( overlay,  new WindowManager.LayoutParams(  LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT,   WindowManager.LayoutParams.TYPE_APPLICATION,  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT));

LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width, h);
params.leftMargin = x;  //默认0,如超出时可设为-1*width
params.topMargin = y;   Button b = new Button(this);

params.addRule(RelativeLayout.BELOW, b.getId());

parent.addView(b, params);

字体不随系统改变@Override public Resources getResources() {
        Resources res = super.getResources();
        Configuration configuration = res.getConfiguration();
        if (configuration.fontScale != 1) {
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();
            res.updateConfiguration(newConfig, res.getDisplayMetrics());}
        return res;    } //allowCaps=false防英文自动大写 setText(String.format("名字%s,年龄%d",name,age));  <string name="info">名字转义字符%1$s(%1表第一个位置的变量, $s表示字符串类型),年龄是%2$d的用户</string>

Service弹(系统级)Dialog,在 Manifest 里面注册系统权限,显示 dialog 时加 flag.

Activity中调用attach创建一PhoneWindow,setContentView(R.layout.xxx) -> getWindow().setContentView(),调用PhoneWindow中的setContentView()创建DecorView,将R.layout.xxx通过布局填充器填充,调用ViewGroup。removeAllView(),再添加新的:addView()。

获取V的绝对位置:@Override

protected void onDraw(Canvas canvas) {
    float[] values = new float[9];

    int[] location1 = new int[2];
    Matrix matrix = canvas.getMatrix();
    matrix.getValues(values);

    location1[0] = (int) values[2];//2者控制位移
    location1[1] = (int) values[5];
    Log.i(TAG, "location1 = " + Arrays.toString(location1));

    int[] location2 = new int[2];
    this.getLocationOnScreen(location2);
    Log.i(TAG, "location2 = " + Arrays.toString(location2));
}



'V':改变visibility -->   构造View   -->      onFinishInflate  -->   onAttachedToWindow  -->  onMeasure(可滚动Vp里面放最大高度可滚动VP)  -->  onSizeChanged(初始化或更改v大小时)  -->  onLayout  -->   onDraw  -->  onDetackedFromWindow      View.setLayerType(layerType(Software Layer/Harderware Layer ), paint);软绘层将 View 存储成 bitmap;硬绘层则将之 存储成Texture,占用 GPU 中的存储,在动画后需要重新设置成LAYER TYPE NONE

VP:获取自身对应的lp:generateLayoutParams();childV.getLayoutParams()//对应父view的layoutparams MeasureSpec.getSize(widthMeasureSpec);MeasureSpec.getMode(widthMeasureSpec); getChildCount() getChildAt() measureChild(ren)   childV.getMesuredWidth() onLayout:childV.getVisibility() childV.layout()  dispatchDraw(Canvas)


CV (Canvas操作matrix主要是前乘,translate是坐标系的移动,缩放比例为负数时会根据缩放中心轴翻转:cv.scale(1,-1)翻转y坐标轴,cv.skew (float sx, float sy)sx:将画布在x方向上倾斜,sx为倾斜角度的tan值)。saveLayerAlpha(10, 10, 300, 300, 0x88, Canvas.ALL_SAVE_FLAG) 存在离屏缓存中,canvas.restoreToCount(sc)。画布相当于画板,图层相当于画纸。先操作(如矩阵canvas.setMatrix(matrix.postTranslate()))画布,再绘制图形。 canvas.clipPath(path);  


Paint:  setXfermode((new PorterDuffXfermode(PorterDuff.Mode.SRC_IN))后再Paint.setXfermode(null):用其需具alpha属性,难以兼容硬件加速,所以setLayerType(LAYER_TYPE_SOFTWARE, null);        setShader(bitmapShader(图像重复TileMode)/ linearGradient(颜色渐变float x0, float y0, float x1, float y1, int color0, int color1,TileMode tile:填充区域超过起始点和终结点的距离时,平铺模式:CLAMP 重复终点边缘颜色,REPEAT,MIRROR倒序重复 ) 。RadialGradient : 环形渲染,SweepGradient : 扫描渐变渲染/梯度渲染,ComposeShader : 组合渲染) ,其父类Shader主要是返回绘制时颜色的横向跨度,不作用于Bitmap)   。

(RGB通道) 颜色 = 绘制的颜色 + (1 - 透明度) × Canvas上原有颜色   RectF(left,top,right,bottom相对于屏幕的起始点)
色彩变换:(颜色)矩阵( paint.setColorFilter(colorMatrixColorFilter(colorMatrix3.postConcat(colorMatrix(4X5).setSaturation()/setRotate()/setScale()))) ):饱和度,色调,亮度;
像素点为单位:bitmap.s/getPixels(r[]gb)。     Transformation.setAlpha()

图像形态:Xfermode/BitmapShader/矩阵(3X3)/像素块drawBitmapMesh(方块变形2个交点1维数组)


updating

14+ 硬件加速(控制invalidate与分层)默认开启,但并不支持所有的2D画图的操作(如Canvas.draw...()),加速级别:<application android:hardwareAccelerated="false"></application>

<application   android:hardwareAccelerated="true"> <activity android:hardwareAccelerated="false"/> </application>
Window:getWindow().setFlags(  WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);V:myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 可以关闭View级别的,但在View级别关不了。验证是否支持硬件加速:View.isHardwareAccelerated();     Canvas.isHardwareAccelerated()建议在draw代码块使用:因为如果一个View被attach到一个硬件加速的Window上,即使没有硬件加速的Canvas,它也可被绘制。


SurfaceView :private final Object mSurfaceLock = new Object();  private DrawThread mThread;
@Override public void surfaceCreated(SurfaceHolder holder) {  
    mThread = new DrawThread(holder);
    mThread.setRun(true);  
    mThread.start();
}
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {  //可获取SurfaceView的宽高等信息}
@Override  public void surfaceDestroyed(SurfaceHolder holder) {
    synchronized (mSurfaceLock) { //因为绘制子线程中还有对 Canvas 的引用
        mThread.setRun(false); }
}
private class DrawThread extends Thread {
    private SurfaceHolder mHolder;
    private boolean mIsRun = false;
    public DrawThread(SurfaceHolder holder) {   mHolder = holder;    }
    @Override    public void run() {
        while(true) {
            synchronized (mSurfaceLock) {
                if (!mIsRun) {   return;    }
                Canvas canvas = mHolder.lockCanvas();//
                if (canvas != null) { doDraw(canvas);      mHolder.unlockCanvasAndPost(canvas);//

            }}

            Thread.sleep(PERIOD);
        }
    }
public void setRun(boolean isRun) { this.mIsRun = isRun;    }}


拦截系统级悬浮窗:


0 0