簡體   English   中英

android:minSdkVersion =“ 8”時如何獲取圖像的高度和寬度

[英]How to get the height and width of an image when android:minSdkVersion=“8”

通常,我可以使用以下代碼來獲取圖像的寬度,但它需要API級別16。如何在android:minSdkVersion =“ 8”時獲取圖像的高度和寬度

Cursor cur = mycontext.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Images.Media._ID + "=?", new String[] { id }, "");
string width=cur.getString(cur.getColumnIndex(MediaStore.Images.Media.HEIGHT));

傳遞選項以將邊界解碼到工廠:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;

要么

您可以通過使用getWidth()getHeight()來獲取ImageView的高度和寬度,而這將無法為您提供圖像的確切寬度和高度,要獲取圖像寬度的高度,首先需要獲取drawable作為背景,然后進行轉換吸引到 BitmapDrawable`以獲得圖像作為Bitmap,您可以像這樣獲得寬度和高度

Bitmap b = ((BitmapDrawble)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();

還是喜歡這樣

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();

上面的代碼將為您提供當前Imageview大小的位圖,例如設備的屏幕截圖

僅適用於ImageView尺寸

imageView.getWidth();
imageView.getHeight();

如果您有可繪制的圖像並且想要該尺寸,則可以像這樣獲得

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM