Android Imageview FITXY 图片缩小问题

Android Imageview FITXY Image shrinking issue

`大家好,我想从我的 phone 图库中挑选一张图片并将其显示为背景 imageview.I 我可以展示它,甚至我也可以制作图像通过(图像视图的 FITXY 属性)适合整个屏幕。

问题是图像实际上缩小了,所以我尝试了 Fitcenter 和 adjustviewbounds 属性 等所有其他属性,但没有成功,使用此属性图像适合中心,我可以看到一些间隙(宽度和高度) .

谁能帮我解决这个问题。 这是我的完整代码和屏幕截图以供参考。

<FrameLayout
android:id="@+id/fm"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#70b29c"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
    android:id="@+id/set"
    android:contentDescription="@string/hello_world"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"/> 

这是我的 java 代码:

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sc = (LinearLayout) findViewById(R.id.home);
    Button wal = (Button) findViewById(R.id.setwall);
    wal.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

        @Override  
        public void onActivityResult(int requestCode, int resultCode, Intent  data) {  

             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2
             if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && null != data) {
                 Uri picUri = data.getData();
                 String[] filePathColumn = {MediaStore.Images.Media.DATA};
                 Cursor cursor = getContentResolver().query(picUri,
                         filePathColumn, null, null, null);
                 cursor.moveToFirst();
                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                 filePath = cursor.getString(columnIndex);
                 cursor.close();
                 bitmapView = (ImageView) findViewById(R.id.set);
                 bitmapView.setImageBitmap(ExifUtils.rotateBitmap(filePath, decodeSampledBitmap(new File(filePath), 400, 800)));
                 bitmapView.setScaleType(ImageView.ScaleType.FIT_XY);
                 SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                 SharedPreferences.Editor edit = shre.edit();
                 edit.putString("profilePic", filePath);
                 edit.commit();
             }
          }


 public Bitmap decodeSampledBitmap(File res, int reqWidth, int reqHeight) {
     if (res != null) {
         // First decode with inJustDecodeBounds=true to check dimensions
         final BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
         try {
             FileInputStream stream2 = new FileInputStream(res);

             BitmapFactory.decodeStream(stream2, null, options);

             stream2.close();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         // Calculate inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
         o2.inJustDecodeBounds = false;
         FileInputStream stream = null;
         try {
             stream = new FileInputStream(res);
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }
         Bitmap bitmap = BitmapFactory.decodeStream(stream, null, o2);
         try {
             stream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return bitmap;
     } else
         return null;
 }

 public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
     // Raw height and width of image
     final int height = options.outHeight;
     final int width = options.outWidth;
     int inSampleSize = 1;

     if (height > reqHeight || width > reqWidth) {

         final int halfHeight = height / 2;
         final int halfWidth = width / 2;

         // Calculate the largest inSampleSize value that is a power of 2 and keeps both
         // height and width larger than the requested height and width.
         while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
             inSampleSize *= 2;
         }
     }

     return inSampleSize;
 }

}

Link截图here

无论图像的分辨率或纵横比如何,使用 FITXY 都会使您的图像适合 imageView,这将导致图像显示不当...

如果您希望您的图像显示在整个屏幕上,最好使用 CENTER 或 CENTERCROP,这将裁剪所选图像,但将其设置为图像的纵横比并完全覆盖您的视图....