GhostView

转载请注明原地址:http://blog.csdn.net/weixin_38020796/article/details/64922899

GhostView可以在不改变View的parent的情况下,在自己的Overlay里绘制另一个View。被绘制的View不会在它的Parent里绘制,因为View的visibility被设为INVISIBLE。GhostView使用被绘制的View的 render node去绘制。当GhostView为VISIBLE时,它所绘制的View为INVISIBLE;当GhostView为INVISIBLE时,它所绘制的View为VISIBLE。

public class GhostView extends View

构造方法:和被绘制的View相互持有,将被绘制的View的transition visibility设为INVISIBLE,触发View的Parent刷新。

绘制方法:使用View的updateDisplayListIfDirty获取RenderNode进行绘制

创建方法:addGhost(View , ViewGroup , Matrix) 与 addGhost(View , ViewGroup)

Example:

MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) this.findViewById(R.id.textview);
        ColorDrawable colorDrawable = new ColorDrawable(Color.BLUE);
        colorDrawable.setAlpha(100);
        colorDrawable.setBounds(0, 0, 200, 200);
        textView.getOverlay().add(colorDrawable);

        final FrameLayout frameLayout = (FrameLayout) this.findViewById(R.id.frameLayout);
        frameLayout.setRight(frameLayout.getLeft() + 500);
        frameLayout.setBottom(frameLayout.getTop() + 200);
        MainActivity.this.addGhost(textView, frameLayout);

        this.findViewById(R.id.textview2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("Change To another");
            }
        });
    }

    private void addGhost(View view, ViewGroup viewGroup) {
        try {
            Class ghostViewClass = Class.forName("android.view.GhostView");
            Method addGhostMethod = ghostViewClass.getMethod("addGhost", View.class,
                    ViewGroup.class, Matrix.class);
            View ghostView = (View) addGhostMethod.invoke(null, view, viewGroup, null);
            ghostView.setBackgroundColor(Color.YELLOW);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

效果:

这里写图片描述

点击CHANGE按钮,改变Button的Text,可以看到GhostView也随之更新:

这里写图片描述

将GhostView设为INVISIBLE,原来的View正常绘制:

这里写图片描述

转载请注明原地址:http://blog.csdn.net/weixin_38020796/article/details/64922899

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值