【问题标题】:Crop Image and set it as Imageview's background Android裁剪图像并将其设置为 Imageview 的背景 Android
【发布时间】:2015-11-13 03:25:13
【问题描述】:

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

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

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`

【问题讨论】:

    标签: android image


    【解决方案1】:

    我建议先在图像编辑器中裁剪图像,然后在 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">
    

    【讨论】:

    • 嗨,乔希,感谢您的回答,但此逻辑是否会从图库中裁剪图像并自动设置为 imageview bacground。谢谢。
    • 您不应使用 Android Studio 来裁剪图像,而应提前在图像编辑器中裁剪图像。在 Android 中进行的任何类型的自动裁剪都会产生不可预知的结果,具体取决于您用于查看应用程序的设备。您可能还希望将单独的图像用于横向布局(用于面向横向的设备,例如平板电脑)。
    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多