如何正确裁剪图像以适合imageview

时间:2016-10-15 22:01:35

标签: android image crop picasso

如何裁剪我使用毕加索的图像,以正确适合布局父宽度和固定高度,例如:在横向模式和纵向模式下拍摄的照片均为500张。它们可以按比例缩小或缩小,但不会对质量产生太大影响。有点像Instagram如何在像正方形的滚动视图中适合他们的图片。我需要所有图片以适应布局宽度和一定高度。

提前致谢。

这是我的代码:

if(item.getTitle().equals("Add Picture")){
   verifyStoragePermissions(MainActivity.this);
   Intent loadPicture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
   startActivityForResult(loadPicture, RESULT_LOAD_IMAGE);
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();

            ImageView imageView = (ImageView) findViewById(R.id.loadPictureImageView);

            mlinearLayout = (LinearLayout) findViewById(R.id.imageScrollLayout);
            int width = mlinearLayout.getWidth();

            //Picasso.with(this).load(selectedImage).resize(width, 500).centerCrop().into(imageView);
            //Picasso.with(this).load(selectedImage).resize(width, 500).centerInside().into(imageView);
            Picasso.with(this).load(selectedImage).fit().into(imageView);

        }
    }

我的xml布局和imageview:

<LinearLayout
            android:id="@+id/imageScrollLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="vertical"
            android:layout_below="@+id/profile_layout">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:id="@+id/loadPictureImageView"
                android:elevation="10dp" />

        </LinearLayout>

1 个答案:

答案 0 :(得分:0)

scaleType上使用ImageView attr到xml,或使用Picasso内置的图像处理方法来确定比例和大小。可以找到一个很好的教程here

相关问题