Android获得标题栏,状态栏,控件,屏幕高度

注意,数据的获取应该在onWindowFocusChanged函数中进行,防止数据获取错误。
首先声明整个手机屏幕的获取:

activity.getWindowManager().getDefaultDisplay();

声明整个应用(除了状态栏之外的区域的获取):

    Rect outRect = new Rect();  
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  

得到除了状态栏和标题栏之外的View绘制的区域:

    Rect outRect = new Rect();  
    activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  

在得到了绘制的区域之后我们来得到所要计算的高度:
一、状态栏的高度:

    Rect frame = new Rect();  
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
    int statusBarHeight = frame.top;  

另外常用的两种方式:
首先是反射的方式:

    int statusBarHeight=0;
  try {
  Class clazz=Class.forName("com.android.internal.R$dimen");
  Object object=clazz.newInstance();
  Field field=clazz.getField("status_bar_height");
  //反射出该对象中status_bar_height字段所对应的在R文件的id值
  //该id值由系统工具自动生成,文档描述如下:
  //The desired resource identifier, as generated by the aapt tool.
  int id = Integer.parseInt(field.get(object).toString());
  System.out.println("id="+id);
  //依据id值获取到状态栏的高度,单位为像素
  statusBarHeight = context.getResources().getDimensionPixelSize(id);
  System.out.println("statusBarHeight="+statusBarHeight+"pixel");
  } catch (Exception e) {
  
  }

其次:

 int result=0;
        int resourceId=getResources().getIdentifier("status_bar_height","dimen","android");
        if(resourceId>0){
            result=getResources().getDimensionPixelSize(resourceId);
        }
        return result;

二、得到标题栏的高度:

    int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
    //statusBarHeight是上面所求的状态栏的高度  
    int titleBarHeight = contentTop - statusBarHeight  

三、两种方式得到屏幕的高度:

    WindowManager windowManager = getWindowManager();  
    Display display = windowManager.getDefaultDisplay();  
    screenWidth = display.getWidth();  
    screenHeight = display.getHeight();  
DisplayMetrics dm = new DisplayMetrics();   
this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity  
screenWidth =dm.widthPixels;  
screenHeight =dm.heightPixels

补充使得屏幕横屏的代码:

setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE);

四:获取控件的宽高,一般来说,我们在onCreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高

 //------------------------------------------------方法一  
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
        imageView.measure(w, h);  
        int height =imageView.getMeasuredHeight();  
        int width =imageView.getMeasuredWidth();  
        textView.append("\n"+height+","+width);  

    此方法会加载onMeasure三次  



        //-----------------------------------------------方法二  
        ViewTreeObserver vto = imageView.getViewTreeObserver();  
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
            public boolean onPreDraw() {  
                int height = imageView.getMeasuredHeight();  
                int width = imageView.getMeasuredWidth();  
                textView.append("\n"+height+","+width);  
                return true;  
            }  
        });  

    此方法会加载onMeasure二次,但是回调函数会回调很多次  

     //-----------------------------------------------方法三     
        ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
        vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @Override    
            public void onGlobalLayout() {  
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
                textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
            }    
        });    
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!针对您的问题,您可以使用 Android 中的 Toolbar 控件来实现滑动渐变标题栏。 具体实现步骤如下: 1. 在布局文件中添加 Toolbar 控件,并设置其高度为 wrap_content。 2. 在代码中通过 findViewById 获取 Toolbar 对象,并调用 setSupportActionBar 方法将其设置为当前 Activity 的 ActionBar。 3. 在 Activity 的 onCreate 方法中,通过 getSupportActionBar().setDisplayHomeAsUpEnabled(true) 设置标题栏左侧显示返回按钮。 4. 在滑动时,通过监听 RecyclerView 的滚动事件,动态改变标题栏的背景颜色和透明度,实现滑动渐变效果。 5. 在 onScrollStateChanged 方法中,根据当前滚动状态判断是否需要执行动画效果。 下面是一个简单的示例代码: ```java // 获取 Toolbar 对象 Toolbar toolbar = findViewById(R.id.toolbar); // 将 Toolbar 设置为当前 Activity 的 ActionBar setSupportActionBar(toolbar); // 显示返回按钮 getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 监听 RecyclerView 的滚动事件 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE) { // 滑动停止时执行动画效果 animateToolbarColor(0xFF0000FF, 0x00000000); } } @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 获取当前 RecyclerView 的滚动位置 int scrollY = recyclerView.computeVerticalScrollOffset(); // 计算标题栏透明度 int alpha = Math.min(255, scrollY * 2); // 执行动画效果 animateToolbarColor(Color.argb(alpha, 0xFF, 0x00, 0x00), Color.argb(0, 0x00, 0x00, 0x00)); } }); // 改变标题栏背景颜色和透明度的动画效果 private void animateToolbarColor(int fromColor, int toColor) { ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor); colorAnimation.setDuration(250); colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { toolbar.setBackgroundColor((int) animator.getAnimatedValue()); } }); colorAnimation.start(); } ``` 希望这个示例能够对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值