将图像裁剪为方形-Android

将图像裁剪为方形-Android,android,android-imageview,resize-crop,Android,Android Imageview,Resize Crop,如何从左侧和右侧剪切矩形图像(600 x 300)以适合方形ImageView?我不想调整图像的大小,我只想把它裁剪成300x300 [解决方案] 正如@blackbelt所说的 Bitmap cropImg=Bitmap.createBitmap(src、startX、startY、dstWidth、dstWidth) 非常适合裁剪图像。那么,如何自动裁剪不同大小的图像呢。我为此创建了以下简单代码: // From drawable Bitmap src= BitmapFactory.deco

如何从左侧和右侧剪切矩形图像(600 x 300)以适合方形ImageView?我不想调整图像的大小,我只想把它裁剪成300x300

[解决方案]

正如@blackbelt所说的

Bitmap cropImg=Bitmap.createBitmap(src、startX、startY、dstWidth、dstWidth)

非常适合裁剪图像。那么,如何自动裁剪不同大小的图像呢。我为此创建了以下简单代码:

// From drawable
Bitmap src= BitmapFactory.decodeResource(context.getResources(), R.drawable.image);

// From URL
Bitmap src = null;
try {
    String URL = "http://www.example.com/image.jpg";
    InputStream in = new java.net.URL(URL).openStream();
    src = BitmapFactory.decodeStream(in);
} catch (Exception e) {
    e.printStackTrace();
}

int width = src.getWidth();
int height = src.getHeight();
int crop = (width - height) / 2;
Bitmap cropImg = Bitmap.createBitmap(src, crop, 0, height, height);

ImageView.setImageBitmap(cropImg);
你可以用

Bitmap dst = Bitmap.createBitmap(src, startX, startY, dstWidth, dstHeight);
从文件中:

从源的指定子集返回不可变位图 位图。新位图可以是与源相同的对象,也可以是副本 已经制作好了。其初始化密度与 原始位图


您可以在上面的答案上找到文档

的扩展部分

因为在某些情况下,只需使用Bitmap.createBitmap(),就可以得到异常非预期结果,例如:

java.lang.IllegalArgumentException:x+宽度必须为 设置固定的图像视图高度、宽度,并将两个属性设置为“图像视图”

完成


现在xml具有如下属性

 custom:cropAspectRatioX="2"
    custom:cropAspectRatioY="1"
如果您想要方形裁剪,请将两者都设为1。现在它是矩形的

添加活动CropActivity

       package agropost.post.agro.com.agropost.Activity;

    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.DisplayMetrics;
    import android.widget.Button;

    import com.theartofdev.edmodo.cropper.CropImageView;

    import agropost.post.agro.com.agropost.R;
    import agropost.post.agro.com.agropost.Utility.Constants;
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;

    public class CropActivity extends AppCompatActivity {


        public static boolean isCrop = false;
        @BindView(R.id.img_crop)
        CropImageView imgCrop;
        @BindView(R.id.btn_done)
        Button btnDone;
        @BindView(R.id.btn_cancel)
        Button btnCancel;




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_crop);
            ButterKnife.bind(this);


            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            width = width - 80;
            imgCrop.getLayoutParams().height = width;
            imgCrop.getLayoutParams().width = width;

            imgCrop.setBackground(null);
            imgCrop.setScaleType(CropImageView.ScaleType.FIT_CENTER);



                imgCrop.setImageBitmap(Constants.mDashboardActivity.thumbnail_r);



        }

        @OnClick(R.id.btn_done)
        public void onViewClicked() {

            isCrop = true;
            Intent returnIntent = new Intent();



                Constants.mDashboardActivity.thumbnail_r = imgCrop.getCroppedImage();
                setResult(3, returnIntent);



            finish();
        }

        @OnClick(R.id.btn_cancel)
        public void onViewClickedCancel() {

            byte[] byteArray = getIntent().getByteArrayExtra("default");
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);


            Constants.mDashboardActivity.thumbnail_r = bmp;
            isCrop = true;
            Intent returnIntent = new Intent();
            setResult(3, returnIntent);

            finish();
        }


       @Override
        public void onBackPressed() {
            //  super.onBackPressed();


        }


    }
活动的xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".Activity.CropActivity">


    <com.theartofdev.edmodo.cropper.CropImageView xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/img_crop"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/drawer_bg"
        android:scaleType="centerInside"
        custom:cropAspectRatioX="2"
        custom:cropAspectRatioY="1"
        custom:cropFixAspectRatio="true"
        custom:cropShape="rectangle" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_done"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="@dimen/margin_40"
            android:layout_marginRight="@dimen/margin_20"
            android:layout_marginTop="@dimen/margin_20"
            android:layout_weight="1"
            android:background="@drawable/btn_bg_green_rounded"
            android:text="Done"
            android:textColor="@color/colorWhite"

            android:textSize="@dimen/fontsize_normal" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="@dimen/margin_20"
            android:layout_marginRight="@dimen/margin_40"
            android:layout_marginTop="@dimen/margin_20"
            android:layout_weight="1"
            android:background="@drawable/btn_bg_green_rounded"
            android:text="Cancel"
            android:textColor="@color/colorWhite"
            android:textSize="@dimen/fontsize_normal"

            android:visibility="gone" />
    </LinearLayout>

</LinearLayout>

这正是我想要的,得到完美的圆形图像,而不是椭圆形。但是,如果图像的高度大于宽度,则不会在中心进行裁剪,因为createBitmap方法中y参数的值为0。下面是解决方法:添加这两行:
intcroph=(高度-宽度)/2;cropH=(cropH<0)?0:cropH使用cropH作为y值。
Bitmap cropImg=Bitmap.createBitmap(位图、cropW、cropH、newWidth、newHeight)很好,我明白你的意思。。。。基本上对宽度和高度进行相同的计算。
       package agropost.post.agro.com.agropost.Activity;

    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.DisplayMetrics;
    import android.widget.Button;

    import com.theartofdev.edmodo.cropper.CropImageView;

    import agropost.post.agro.com.agropost.R;
    import agropost.post.agro.com.agropost.Utility.Constants;
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;

    public class CropActivity extends AppCompatActivity {


        public static boolean isCrop = false;
        @BindView(R.id.img_crop)
        CropImageView imgCrop;
        @BindView(R.id.btn_done)
        Button btnDone;
        @BindView(R.id.btn_cancel)
        Button btnCancel;




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_crop);
            ButterKnife.bind(this);


            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            width = width - 80;
            imgCrop.getLayoutParams().height = width;
            imgCrop.getLayoutParams().width = width;

            imgCrop.setBackground(null);
            imgCrop.setScaleType(CropImageView.ScaleType.FIT_CENTER);



                imgCrop.setImageBitmap(Constants.mDashboardActivity.thumbnail_r);



        }

        @OnClick(R.id.btn_done)
        public void onViewClicked() {

            isCrop = true;
            Intent returnIntent = new Intent();



                Constants.mDashboardActivity.thumbnail_r = imgCrop.getCroppedImage();
                setResult(3, returnIntent);



            finish();
        }

        @OnClick(R.id.btn_cancel)
        public void onViewClickedCancel() {

            byte[] byteArray = getIntent().getByteArrayExtra("default");
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);


            Constants.mDashboardActivity.thumbnail_r = bmp;
            isCrop = true;
            Intent returnIntent = new Intent();
            setResult(3, returnIntent);

            finish();
        }


       @Override
        public void onBackPressed() {
            //  super.onBackPressed();


        }


    }
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".Activity.CropActivity">


    <com.theartofdev.edmodo.cropper.CropImageView xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/img_crop"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/drawer_bg"
        android:scaleType="centerInside"
        custom:cropAspectRatioX="2"
        custom:cropAspectRatioY="1"
        custom:cropFixAspectRatio="true"
        custom:cropShape="rectangle" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_done"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="@dimen/margin_40"
            android:layout_marginRight="@dimen/margin_20"
            android:layout_marginTop="@dimen/margin_20"
            android:layout_weight="1"
            android:background="@drawable/btn_bg_green_rounded"
            android:text="Done"
            android:textColor="@color/colorWhite"

            android:textSize="@dimen/fontsize_normal" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="@dimen/margin_20"
            android:layout_marginRight="@dimen/margin_40"
            android:layout_marginTop="@dimen/margin_20"
            android:layout_weight="1"
            android:background="@drawable/btn_bg_green_rounded"
            android:text="Cancel"
            android:textColor="@color/colorWhite"
            android:textSize="@dimen/fontsize_normal"

            android:visibility="gone" />
    </LinearLayout>

</LinearLayout>
      implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'