Android Imageview FITXY图像缩小问题

时间:2015-10-17 04:46:57

标签: android imageview

<大家好,我想从我的手机图库中选择一张图片并将其显示为imageview的背景。我能够显示它,甚至我能够通过(FITXY属性)使图像适合整个屏幕of imageview)。

问题是图像实际上缩小了,所以我尝试了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;
 }

}

屏幕截图here

的链接

1 个答案:

答案 0 :(得分:0)

使用FITXY会使您的图像适合imageView,无论图像的分辨率或宽高比如何都会导致图像显示不正确...

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