在Android中實現背景圖片自適應的方法有幾種,可以根據具體情況選擇使用以下其中一種或多種方法:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background_image"
android:scaleType="fitXY"/>
InputStream is = getResources().openRawResource(R.drawable.background_image);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
options.inSampleSize = calculateInSampleSize(options, imageViewWidth, imageViewHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
imageView.setImageBitmap(bitmap);
Glide.with(context)
.load(R.drawable.background_image)
.centerCrop()
.into(imageView);