类android.view.ViewTreeObserver.OnGlobalFocusChangeListener源码实例Demo

下面列出了怎么用android.view.ViewTreeObserver.OnGlobalFocusChangeListener的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: AndroidTVWidget   文件: MainActivity.java
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OPENLOG.initTag("hailongqiu", true); // 开启log输出.
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        // WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test_main);
        SmoothHorizontalScrollView hscroll_view = (SmoothHorizontalScrollView) findViewById(R.id.hscroll_view);
        hscroll_view.setFadingEdge((int)getDimension(R.dimen.w_100)); // 滚动窗口也需要适配.
        //
        test_top_iv = findViewById(R.id.test_top_iv);
        /* MainUpView 设置. */
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        // mainUpView1 = new MainUpView(this); // 手动添加(test)
        // mainUpView1.attach2Window(this); // 手动添加(test)
        mOpenEffectBridge = (OpenEffectBridge) mainUpView1.getEffectBridge();
        // 4.2 绘制有问题,所以不使用绘制边框.
        // 也不支持倒影效果,绘制有问题.
        // 请大家不要按照我这样写.
        // 如果你不想放大小人超出边框(demo,张靓颖的小人),可以不使用OpenEffectBridge.
        // 我只是测试----DEMO.(建议大家使用 NoDrawBridge)
        if (Utils.getSDKVersion() == 17) { // 测试 android 4.2版本.
            switchNoDrawBridgeVersion();
        } else { // 其它版本(android 4.3以上).
            mainUpView1.setUpRectResource(R.drawable.test_rectangle); // 设置移动边框的图片.
            mainUpView1.setShadowResource(R.drawable.item_shadow); // 设置移动边框的阴影.
        }
        // mainUpView1.setUpRectResource(R.drawable.item_highlight); //
        // 设置移动边框的图片.(test)
        // mainUpView1.setDrawUpRectPadding(new Rect(0, 0, 0, -26)); //
        // 设置移动边框的距离.
        // mainUpView1.setDrawShadowPadding(0); // 阴影图片设置距离.
        // mOpenEffectBridge.setTranDurAnimTime(500); // 动画时间.

        FrameMainLayout main_lay11 = (FrameMainLayout) findViewById(R.id.main_lay);
        main_lay11.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(final View oldFocus, final View newFocus) {
                if (newFocus != null)
                    newFocus.bringToFront(); // 防止放大的view被压在下面. (建议使用MainLayout)
                float scale = 1.2f;
                mainUpView1.setFocusView(newFocus, mOldFocus, scale);
                mOldFocus = newFocus; // 4.3以下需要自己保存.
                // 测试是否让边框绘制在下面,还是上面. (建议不要使用此函数)
                if (newFocus != null) {
                    testTopDemo(newFocus, scale);
                }
            }
        });
        // test demo.
        gridview_lay = findViewById(R.id.gridview_lay);
        gridview_lay.setOnClickListener(this);
        findViewById(R.id.listview_lay).setOnClickListener(this);
        findViewById(R.id.keyboard_lay).setOnClickListener(this);
        findViewById(R.id.viewpager_lay).setOnClickListener(this);
        findViewById(R.id.effect_rlay).setOnClickListener(this);
        findViewById(R.id.menu_rlayt).setOnClickListener(this);
        findViewById(R.id.recyclerview_rlayt).setOnClickListener(this);
        /**
         * 尽量不要使用鼠标. !!!! 如果使用鼠标,自己要处理好焦点问题.(警告)
         */
//		main_lay11.setOnHoverListener(new OnHoverListener() {
//			@Override
//			public boolean onHover(View v, MotionEvent event) {
//				mainUpView1.setVisibility(View.INVISIBLE);
//				return true;
//			}
//		});
        //
        for (int i = 0; i < main_lay11.getChildCount(); i++) {
            main_lay11.getChildAt(i).setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
//						v.performClick();
                        v.requestFocus();
                    }
                    return false;
                }
            });
        }
    }
 
源代码2 项目: Android-tv-widget   文件: MainActivity.java
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OPENLOG.initTag("hailongqiu", true); // 开启log输出.
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        // WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test_main);
        SmoothHorizontalScrollView hscroll_view = (SmoothHorizontalScrollView) findViewById(R.id.hscroll_view);
        hscroll_view.setFadingEdge((int)getDimension(R.dimen.w_100)); // 滚动窗口也需要适配.
        //
        test_top_iv = findViewById(R.id.test_top_iv);
        /* MainUpView 设置. */
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        // mainUpView1 = new MainUpView(this); // 手动添加(test)
        // mainUpView1.attach2Window(this); // 手动添加(test)
        mOpenEffectBridge = (OpenEffectBridge) mainUpView1.getEffectBridge();
        // 4.2 绘制有问题,所以不使用绘制边框.
        // 也不支持倒影效果,绘制有问题.
        // 请大家不要按照我这样写.
        // 如果你不想放大小人超出边框(demo,张靓颖的小人),可以不使用OpenEffectBridge.
        // 我只是测试----DEMO.(建议大家使用 NoDrawBridge)
        if (Utils.getSDKVersion() == 17) { // 测试 android 4.2版本.
            switchNoDrawBridgeVersion();
        } else { // 其它版本(android 4.3以上).
            mainUpView1.setUpRectResource(R.drawable.test_rectangle); // 设置移动边框的图片.
            mainUpView1.setShadowResource(R.drawable.item_shadow); // 设置移动边框的阴影.
        }
        // mainUpView1.setUpRectResource(R.drawable.item_highlight); //
        // 设置移动边框的图片.(test)
        // mainUpView1.setDrawUpRectPadding(new Rect(0, 0, 0, -26)); //
        // 设置移动边框的距离.
        // mainUpView1.setDrawShadowPadding(0); // 阴影图片设置距离.
        // mOpenEffectBridge.setTranDurAnimTime(500); // 动画时间.

        FrameMainLayout main_lay11 = (FrameMainLayout) findViewById(R.id.main_lay);
        main_lay11.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(final View oldFocus, final View newFocus) {
                if (newFocus != null)
                    newFocus.bringToFront(); // 防止放大的view被压在下面. (建议使用MainLayout)
                float scale = 1.2f;
                mainUpView1.setFocusView(newFocus, mOldFocus, scale);
                mOldFocus = newFocus; // 4.3以下需要自己保存.
                // 测试是否让边框绘制在下面,还是上面. (建议不要使用此函数)
                if (newFocus != null) {
                    testTopDemo(newFocus, scale);
                }
            }
        });
        // test demo.
        gridview_lay = findViewById(R.id.gridview_lay);
        gridview_lay.setOnClickListener(this);
        findViewById(R.id.listview_lay).setOnClickListener(this);
        findViewById(R.id.keyboard_lay).setOnClickListener(this);
        findViewById(R.id.viewpager_lay).setOnClickListener(this);
        findViewById(R.id.effect_rlay).setOnClickListener(this);
        findViewById(R.id.menu_rlayt).setOnClickListener(this);
        findViewById(R.id.recyclerview_rlayt).setOnClickListener(this);
        /**
         * 尽量不要使用鼠标. !!!! 如果使用鼠标,自己要处理好焦点问题.(警告)
         */
//		main_lay11.setOnHoverListener(new OnHoverListener() {
//			@Override
//			public boolean onHover(View v, MotionEvent event) {
//				mainUpView1.setVisibility(View.INVISIBLE);
//				return true;
//			}
//		});
        //
        for (int i = 0; i < main_lay11.getChildCount(); i++) {
            main_lay11.getChildAt(i).setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
//						v.performClick();
                        v.requestFocus();
                    }
                    return false;
                }
            });
        }
    }
 
 类所在包
 同包方法