今年的博客写的好少,自己是越来越懒了,一转眼又到年底了,回首年初的计划,做到的却寥寥无几
大家使用app时肯定遇到过本篇主题的效果,当app出了某些新功能需要给用户以提示时,更新app后高亮显示需要提示用户更新的地方,具体怎么实现呢,其实也蛮简单的,客官,请往里走
参考自:https://blog.csdn.net/huang_xiao_yu/article/details/52922281
国际惯例:
首先熟悉两点概念:
- getLocationOnScreen() 获取当前控件相对于屏幕左上角位置
- getLocationOnWindow() 获取当前控件相对于当前窗口左上角位置
听着是不是感觉有点一样,二者在window占据全部screen时,返回值相等;不同的典型情况是在Dialog中时。当Dialog出现在屏幕中间时,View.getLocationOnScreen()取得的值要比 View.getLocationInWindow()取得的值要大
https://blog.csdn.net/tmj2014/article/details/53283804
第二点是window、decorview、contentview之间的关系
总结为
每个activity持有一个window对象(phonewindow为window唯一实现类)
每个phonewindow持有一个decorview(是FrameLayout的子类)
在加载activity时会根据theme选择对应的布局,加入decorview(一般会包含ActionBar,Title,和一个id为content的FrameLayout)
看下图会清晰点:(图片来源于网络)
参考自:
https://blog.csdn.net/lmj623565791/article/details/41894125/
https://blog.csdn.net/weixin_41101173/article/details/79685305
前戏哦不准备工作做完了,可以。。。
主要代码如下
//开启线程获取要高亮显示布局的位置(这里没有其它窗口,getLocationOnScreen getLocationOnWindow就没有区别了)
new Thread() {
@Override
public void run() {
super.run();
final int[] locationArr = new int[2];
//直到获取到位置为止,去加载高亮显示的view
while (locationArr[0] == 0 && locationArr[1] == 0) {
SystemClock.sleep(100);
tvNeedHighLight.getLocationOnScreen(locationArr);
}
//ui操作,要在主线程
runOnUiThread(new Runnable() {
@Override
public void run() {
final View highLightView = getLayoutInflater().inflate(R.layout.layout_high_light, null);
TextView tvHighLight2 = highLightView.findViewById(R.id.tv_need_high_light);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvHighLight2.getLayoutParams();
//状态栏不在contentview中,需要减去状态栏高度
layoutParams.setMargins(locationArr[0], locationArr[1] - getStatusBarHeight(), 0, 0);
//获取contentview,是一个framelayout
final FrameLayout root = getWindow().getDecorView().findViewById(android.R.id.content);
tvHighLight2.setVisibility(View.VISIBLE);
//将蒙版view加入到contentview 中
root.addView(highLightView);
highLightView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将蒙版view移除
root.removeView(highLightView);
}
});
}
});
}
}.start();
1、当前activity中是没有actionbar的,如果有的话,需要减去actionbar的高度
2、其实也不用把需要高亮显示的view在copy一遍到蒙版layout中,把需要高亮显示的view生成bitmap,在蒙版layout中添加一个imageview设置对应的参数和bitmap应该也是可以的
好了,就到这里吧。