裁剪图像并将其设置为Imageview的背景Android

时间:2015-08-19 17:37:25

标签: android image

我是Android开发中的新宠。 在这个stackoverflow社区的一位开发人员的帮助下,我解决了图像格局问题,并留下了另外两个问题来完成我的项目。

  1. 在将图像设置为imageview的背景之前裁剪图像。
  2. 即使保留ImageView.ScaleType.FIT_XY,如何阻止imageview缩小。我的意思是我从图库中选择的图像应该占据整个屏幕(布局)而不缩小。 我已经浏览了本网站中的一些问题和答案,但没有发现它们恰好符合我的代码。
  3. 1。Crop an image from gallery in android

    2。android:select image from gallery then crop that and show in an imageview

    以下是我片段的源代码:

    public class FragmentTab1 extends Fragment {
        RelativeLayout homes;
        private static int RESULT_LOAD_IMAGE = 1;
        ImageView bitmapView;
        BitmapFactory.Options options;
        String filePath;
        Button rot;
        private int mDegree = 0;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragmenttab1, container, false);
            homes = (RelativeLayout) view.findViewById(R.id.hmt);
            homes.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);
                }
            });
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
            final String filePath = prefs.getString("profilePic", "");
            if (!filePath.equals("")) {
                bitmapView = (ImageView) view.findViewById(R.id.keka);
                bitmapView.setImageBitmap(ExifUtils.rotateBitmap(filePath, decodeSampledBitmap(new File(filePath), 400, 400)));
                bitmapView.setScaleType(ImageView.ScaleType.FIT_XY);
    
            }
    
            return view;
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
                Uri picUri = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getActivity().getContentResolver().query(picUri,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                filePath = cursor.getString(columnIndex);
                cursor.close();
                bitmapView = (ImageView) getActivity().findViewById(R.id.keka);
                bitmapView.setImageBitmap(ExifUtils.rotateBitmap(filePath, decodeSampledBitmap(new File(filePath), 400, 400)));
                bitmapView.setScaleType(ImageView.ScaleType.FIT_XY);
                SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(getActivity());
                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;
        }`enter code here`
    }`enter code here`
    

1 个答案:

答案 0 :(得分:0)

我建议先在图像编辑器中裁剪图像,然后在XML布局文件中将图像设置为ImageView的背景,如下例所示:

   <ImageView>   
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/yourCroppedImage"/>

您不需要添加任何其他比例属性或属性。

另一种解决方案是将裁剪后的图像设置为整个布局的背景:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                tools:context=".MainActivity"
                android:background="@drawable/yourCroppedImage">