Android获取view高度,及通知栏高度,状态栏高度(色值 透明度 导航颜色渐变)

> Android获取view高度的三种方式:
-- getMeasuredHeight()与getHeight的区别:
1.实际上在当屏幕可以包裹内容的时候,他们的值相等,
2.只有当view超出屏幕后,才能看出他们的区别:getMeasuredHeight()是实际View的大小,与屏幕无关,而getHeight的大小此时则是屏幕的大小。
3.当超出屏幕后,getMeasuredHeight()等于getHeight()加上屏幕之外没有显示的大小
具体方法: 我们知道在oncreate中View.getWidth和View.getHeight无法获得一个view的高度和宽度,这是因为View组件 布局要在onResume回调后完成。
 - 下面说3种方式获取view高度
1. getViewTreeObserver
 - 使用 getViewTreeObserver().addOnGlobalLayoutListener()来获得宽度或者高度。
OnGlobalLayoutListener 是ViewTreeObserver的内部类,当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到,这是一个注册监听视图树的观察者(observer),在视图树的全局事件改变时得到通知。ViewTreeObserver不能直接实例化,而是通过getViewTreeObserver()获得。
- 除了OnGlobalLayoutListener ,ViewTreeObserver还有如下内部类:
  interfaceViewTreeObserver.OnGlobalFocusChangeListener
当在一个视图树中的焦点状态发生改变时,所要调用的回调函数的接口类
  interfaceViewTreeObserver.OnGlobalLayoutListener
当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,所要调用的回调函数的接口类
  interfaceViewTreeObserver.OnPreDrawListener
当一个视图树将要绘制时,所要调用的回调函数的接口类
  interfaceViewTreeObserver.OnScrollChangedListener
当一个视图树中的一些组件发生滚动时,所要调用的回调函数的接口类
  interfaceViewTreeObserver.OnTouchModeChangeListener
当一个视图树的触摸模式发生改变时,所要调用的回调函数的接口类
- 其中,我们可以利用OnGlobalLayoutListener来获得一个视图的真实高度。
private int mHeaderViewHeight; 
private View mHeaderView; 
..... 
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener( 
 new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() {                                                    
   mHeaderViewHeight = mHeaderView.getHeight(); 
   mHeaderView.getViewTreeObserver() 
     .removeGlobalOnLayoutListener(this); 
  } 
}); 
但是需要注意的是OnGlobalLayoutListener可能会被多次触发,因此在得到了高度之后,要将OnGlobalLayoutListener注销掉。

2. 还可以在VIew的post方法中获取, View post事件中获取
 public class TestHeight extends Activity { 
 TextView tv; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_activity_b); 
   tv = (TextView) findViewById(R.id.textView); 
  tv.post(new Runnable() { 
   @Override 
   public void run() { 
    int height= tv.getHeight(); 
   } 
  }); 
 } 


3. int w=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
    int h=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
    textView.measure( w,  h); 
int intwidth = textView.getMeasuredWidth();  

int intheight = textView.getMeasuredHeight(); 

> 色值 透明度 导航颜色渐变

-- 完全透明的色值
实用Android 颜色透明度换算- https://www.jianshu.com/p/1584e9809090
android 设置色值透明度- https://blog.csdn.net/shanshan_1117/article/details/79136990
-- 获取色值 int color = getResources().getColor(R.color.mycolor);
设置Button或ImageButton的背景为透明或者半透明 
  半透明< Button android:background="#e0000000" ... /> 
  透明< Button android:background="#00000000" ... /> 

  颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。例如,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000 。
-- 设置图片透明度
View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id 
v.getBackground().setAlpha(100);//0~255透明度值 
-- 顶部导航栏颜色渐变,标题栏颜色渐变效果
Android之scrollview滑动使导航栏渐变背景色- https://blog.csdn.net/android_cll/article/details/60467637

-- 淘宝标题栏样式;仿天猫首页刷新效果,导航栏颜色渐变效果
在res/drawable里定义一个toolbar_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="0"
        android:startColor="#f89e07"
        android:centerColor="#ee5511"
        android:endColor="#e40b1b"
        android:type="linear" />
</shape>
android:angle 渐变的角度,必须是45的倍数 
android:startColor 开始颜色 
android:centerColor 中间颜色 
android:endColor 结束颜色 
android:type=”linear” 线性渐变
然后直接调用就可以了 android:background="@drawable/toolbar_bg" 

android——顶部导航栏滑动变换背景色- https://blog.csdn.net/xy8199/article/details/78801862
Android 仿天猫京东淘宝 首页的 title栏变色和下拉刷新动画效果- https://blog.csdn.net/qq_33553515/article/details/53156278

- View在Y轴缩放效果  Android
Android UI效果篇-(3)用属性动画实现收缩菜单- https://www.jianshu.com/p/835fc313d1e1
Android 实现页面顶部下拉、底部上拉 沿着Y轴缩放效果- https://blog.csdn.net/qq_33214392/article/details/52471588
Android开发 View的平移、缩放、旋转以及位置、坐标系- https://blog.csdn.net/QQ55214/article/details/52386155
-- 自定义actionBar、ScorllView实现顶部导航颜色渐变;ActionBar随ScorllView上下拖动而透明度渐变效果
Android沉浸式状态栏 + scrollView顶部伸缩 + actionBar渐变- https://github.com/androidstarjack/TranslucentScrollView
Android沉浸式状态栏 + actionBar渐变 + scrollView顶部伸缩- https://download.csdn.net/download/yanjunhui2011/9754865
android沉浸式状态栏+图片背景+标题栏渐变+背景伸缩- https://github.com/yanjunhui2014/TranslucentScrollView

Android实现ScrollView顶部布局上滑缩小,下滑恢复- https://blog.csdn.net/yanjunhui2011/article/details/78274608

Android实现ScrollView顶部布局上滑缩小,下滑恢复- http://download.csdn.net/download/yanjunhui2011/10028253

iOS渐变导航条- https://github.com/huberyhx/HXChangeNavigationBar

仿掌上英雄联盟首页效果(iOS)- https://github.com/yixuanjiang/jianshu

-- android 横向竖向的抽屉效果, 折叠效果;自定义Panel实现多方向抽屉(上下左右)效果;Android 标题下的内容折叠效果
Android仿掌上英雄联盟首页,实现折叠效果- https://www.jianshu.com/p/5dc19d15c096
(推荐)仿掌上英雄联盟首页加载效果(折叠效果,顶部渐变)- https://github.com/wapchief/imitationLOL

共同头部+ViewPager+ListView- https://github.com/cpoopc/ScrollableLayout

实现点击Menu展开Item的抽屉效果- https://github.com/songxiaoliang/LessItemFoldLayout

支持竖向滚动的ViewPager- https://github.com/chadguo/VerticalViewPager
VerticalViewPager-chad 利用 ViewPager 的 fakeDrag 特性,并通过内部 View 的 OnTouchListener 处理 / 模拟事件分发,可以很好地支持 ViewPager 与内部竖向滚动 View 的组合使用。 

Android——仿美团商品详情页折叠效果- https://blog.csdn.net/u012230055/article/details/78228148
 
其实要实现这种效果的方法还有很多,比如利用Design库中的CoordinatorLayout,和AppBarLayout结合来用,也能实现折叠效果。
使用知识点:MaterialDesign中的CoordinatoryLayout,ToolBarLayout,CollapsingToolBarLayout。
https://www.jianshu.com/p/5dc19d15c096

Android开发之DrawerLayout实现抽屉效果- https://www.jianshu.com/p/f31cb996c369
android 横向竖向的抽屉效果- https://download.csdn.net/download/ha000/9683228
android沉浸式状态栏、fitsSystemWindows、标题栏折叠- https://github.com/xiewenfeng/CollapseToolBar

高仿Android QQ菜单,左侧抽屉,底部导航- https://www.jianshu.com/p/a02cdba49349
DrawerLayout+FragmentTabHost实现,模仿Android QQ菜单,左侧抽屉,底部导航Demo- https://github.com/ShiBin1990/QQNavigationDemo
Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭- https://blog.csdn.net/lmj623565791/article/details/39257409
Android超高仿 QQ5.0 侧滑菜单项目完整实例代码 - https://download.csdn.net/download/lmj623565791/7911785
SlidingDrawerLayout上下滑动的菜单控件- https://github.com/xu0425/SlidingDrawerLayout/tree/master/SlidingDrawerLayout
 

> 通知栏高度,状态栏高度

-- 获取通知栏高度
方法1:
public static int getStatusBarHeight(Context context){
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 38;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        } 
        return statusBarHeight;
    }
方法2:(存疑)
private int getStatusBarHeight(){
   Rect rect = new Rect();
   getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
   return rect.top;
}
方法3:(存疑)
public static int getStatusBarHeight() {
        return Resources.getSystem().getDimensionPixelSize(
                Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
    }

-- 获取屏幕当前截图
/**
     * 获取当前屏幕截图,包含状态栏
     * @param activity
     * @return
     * by Hankkin at:2015-10-07 21:16:43
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;
 
    }
 
    /**
     * 获取当前屏幕截图,不包含状态栏
     * @param activity
     * @return
     * by Hankkin at:2015-10-07 21:16:43
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
 
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = null;
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return bp;
 
    }

/**
     * 获取导航栏高度
     * @param context
     * @return
     */
    public static int getDaoHangHeight(Context context) {
        int result = 0;
        int resourceId=0;
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid!=0){
             resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            CMLog.show("高度:"+resourceId);
            CMLog.show("高度:"+context.getResources().getDimensionPixelSize(resourceId) +"");
            return context.getResources().getDimensionPixelSize(resourceId);
        }else
            return 0;
    }

Android 修改通知栏一条信息的显示高度- https://blog.csdn.net/nihaoqiulinhe/article/details/50548917
Android完美获取状态栏高度、标题栏高度、编辑区域高度的获取- https://blog.csdn.net/a_running_wolf/article/details/50477965

-- 获取Android屏幕尺寸
if (android.os.Build.VERSION.SDK_INT >= 13) {
            display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            width = size.x;
            height = size.y;
        }else {
            display = getWindowManager().getDefaultDisplay();
            width = display.getWidth();
            height = display.getHeight();
        }

DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  width = metrics.widthPixels;
  height = metrics.heightPixels;

width = getResources().getDisplayMetrics().heightPixels;
 height = getResources().getDisplayMetrics().widthPixels;

-- 去除导航栏
在onCraete()方法中的setContentView();的之前调用下面这句代码:
 requestWindowFeature(Window.FEATURE_NO_TITLE);

-- 去除状态栏/通知栏
在onCraete()方法中的setContentView();的之前调用下面这句代码:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
,WindowManager.LayoutParams.FLAG_FULLSCREEN);

-- 获取 屏幕状态栏高度和标题栏高度 避免出现0的情况
@Override  
    public void onWindowFocusChanged(boolean hasFocus) {  
        super.onWindowFocusChanged(hasFocus);  
  
        Rect frame = new Rect();  
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
        int statusBarHeight = frame.top;  
        // 获取标题栏高度  
        Window window = getWindow();  
        int contentViewTop = getWindow()  
                .findViewById(Window.ID_ANDROID_CONTENT).getTop();  
        // statusBarHeight是上面所求的状态栏的高度  
        titleBarHeight = contentViewTop - statusBarHeight;  
  
        Log.i("test", "statusBarHeight=" + statusBarHeight + " contentViewTop="  
                + contentViewTop + " titleBarHeight=" + titleBarHeight);  
    }

猜你喜欢

转载自blog.csdn.net/ShareUs/article/details/81317016