Android基于Window.ID_ANDROID_CONTENT给定id添加子View

时间:2024-03-15 12:34:42

Android基于Window.ID_ANDROID_CONTENT给定id添加子View


这一技术特点在一些视频播放器中比较有用。

例如代码:

package zhangphil.demo;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity {
    private static final int VIEW_ID = 0xf0a01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        int w = 500;
        int h = 500;
        int gap = 100;

        addView(w, h, getResources().getColor(android.R.color.holo_orange_light));
        addView(w - gap, h - gap, getResources().getColor(android.R.color.holo_green_light));
        addView(w - gap * 2, h - gap * 2, getResources().getColor(android.R.color.holo_red_light));
    }

    private void addView(int w, int h, int color) {
        ViewGroup vp = (ViewGroup) (scanForActivity(this))//.getWindow().getDecorView();
                .findViewById(Window.ID_ANDROID_CONTENT);

        View old = vp.findViewById(VIEW_ID);
        if (old != null) {
            vp.removeView(old);
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(w, h, Gravity.CENTER);
        FrameLayout newView = new FrameLayout(this);
        newView.setBackgroundColor(color);
        newView.setId(VIEW_ID);
        vp.addView(newView, params);
    }

    public static Activity scanForActivity(Context context) {
        if (context == null) return null;

        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return scanForActivity(((ContextWrapper) context).getBaseContext());
        }

        return null;
    }
}



运行结果:

Android基于Window.ID_ANDROID_CONTENT给定id添加子View



如果在addView里面把这段代码注释掉:

//        View old = vp.findViewById(VIEW_ID);
//        if (old != null) {
//            vp.removeView(old);
//        }


运行结果。

Android基于Window.ID_ANDROID_CONTENT给定id添加子View

这一特性如果巧妙加以利用可制作成特殊视频view的呈现。

Android基于Window.ID_ANDROID_CONTENT给定id添加子View


这一技术特点在一些视频播放器中比较有用。

例如代码:

package zhangphil.demo;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity {
    private static final int VIEW_ID = 0xf0a01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        int w = 500;
        int h = 500;
        int gap = 100;

        addView(w, h, getResources().getColor(android.R.color.holo_orange_light));
        addView(w - gap, h - gap, getResources().getColor(android.R.color.holo_green_light));
        addView(w - gap * 2, h - gap * 2, getResources().getColor(android.R.color.holo_red_light));
    }

    private void addView(int w, int h, int color) {
        ViewGroup vp = (ViewGroup) (scanForActivity(this))//.getWindow().getDecorView();
                .findViewById(Window.ID_ANDROID_CONTENT);

        View old = vp.findViewById(VIEW_ID);
        if (old != null) {
            vp.removeView(old);
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(w, h, Gravity.CENTER);
        FrameLayout newView = new FrameLayout(this);
        newView.setBackgroundColor(color);
        newView.setId(VIEW_ID);
        vp.addView(newView, params);
    }

    public static Activity scanForActivity(Context context) {
        if (context == null) return null;

        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return scanForActivity(((ContextWrapper) context).getBaseContext());
        }

        return null;
    }
}



运行结果:

Android基于Window.ID_ANDROID_CONTENT给定id添加子View



如果在addView里面把这段代码注释掉:

//        View old = vp.findViewById(VIEW_ID);
//        if (old != null) {
//            vp.removeView(old);
//        }


运行结果。

Android基于Window.ID_ANDROID_CONTENT给定id添加子View

这一特性如果巧妙加以利用可制作成特殊视频view的呈现。