按比例裁剪图片,1:1、3:4、4:3为例,比例可以任意设置

1、依赖引入:

compile 'com.edmodo:cropper:1.0.1'

2、布局引入:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.example.ceshi.allworkdemo.MainActivity">
    <com.edmodo.cropper.CropImageView
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/CropImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/lay"
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:id="@+id/lay"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:id="@+id/tet_11"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#333333"
            android:textSize="15sp"
            android:text="1:1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/tet_43"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#333333"
            android:textSize="15sp"
            android:text="4:3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/tet_34"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#333333"
            android:textSize="15sp"
            android:text="3:4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/tet_ok"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#333333"
            android:textSize="15sp"
            android:text="裁剪"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
    </LinearLayout>

</RelativeLayout>

3、主要代码:

 mCropImageView.setImageBitmap(getBitmap(R.drawable.bg));//为了兼容小图片,必须在代码中加载图片
        mCropImageView.setFixedAspectRatio(true);//设置允许按比例截图,如果不设置就是默认的任意大小截图
        mCropImageView.setAspectRatio(1, 1);//设置比例为一比一
//        mCropImageView.setGuidelines(CropImageView.ON);//设置显示网格的时机,默认为on_touch
/**
 * @param resId
 * @return 如果图片太小,那么就拉伸
 */
public Bitmap getBitmap(int resId) {
    WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
    int width = wm.getDefaultDisplay().getWidth();
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(), resId);
    float scaleWidth = 1,scaleHeight = 1;
    if(bitmap.getWidth() < width) {
        scaleWidth = width / bitmap.getWidth();
        scaleHeight = scaleWidth;
    }
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight()
            , matrix, true);
    return bitmap;
}
Bitmap bitmap = mCropImageView.getCroppedImage();//得到裁剪好的图片
ImageView croppedImageView = (ImageView) findViewById(R.id.imageView);
croppedImageView.setImageBitmap(bitmap);//设置到imageview中

4、效果展示:

图片2

图片1图片3

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值