Crop image to square - Android

后端 未结 4 1920
盖世英雄少女心
盖世英雄少女心 2021-02-05 08:29

How can I cut rectangular image (600 x 300) from left and right to fit in square ImageView ? I don\'t want to resize image, I just want to crop it, to be 300 x 300.

4条回答
  •  感情败类
    2021-02-05 08:37

    Now xml is having properties like

     custom:cropAspectRatioX="2"
        custom:cropAspectRatioY="1"
    

    Make both 1 if u want square cropping. now it is of rectangle

    Add activity 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 of activity..............

        
    
    
    
        
    
        
    
            

    add dependecy

          implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
    

提交回复
热议问题