在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);