代码:
//获取控件尺寸(控件尺寸只有在事件里面可以获取到) TextView mTV = (TextView) findViewById(R.id.iv_view); int width = mTV.getWidth(); int height = mTV.getHeight(); //获取图片尺寸 Drawable drawable = ContextCompat.getDrawable(this, R.drawable.about_logo); int width2 = drawable.getIntrinsicWidth(); int height2 = drawable.getIntrinsicHeight(); //获取屏幕尺寸 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width3 = dm.widthPixels; int height3 = dm.heightPixels;结果:
PS:单位都是像素
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 2楼u0100288696天前 10:00
- 学习了。
- 1楼zhuhai__yizhi6天前 21:36
- 博主,补充一下,仅仅获取图片大小的时候,使用你的那个方法消耗的内存会比较大,应使用如下方法比较好。nBitmapFactory.Options options = new BitmapFactory.Options(); noptions.inJustDecodeBounds = true; nBitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options);nLog.e("Test", "Bitmap Height == " + options.outHeight);
- Re: pengkv6天前 09:48
- 回复zhuhai__yizhin多谢补充,又多了一种方法,不过你这个是获取sd卡的图片,我上面写得是drawable文件里的图片