android 图片剪切组件,uCrop

软件简介

uCrop 是Yalantis 组织开源的图片裁剪库, 支持缩放, 旋转图片, 支持各种比例的裁剪框,

非常强大的一个图片裁剪库。

c8772c542a07104de88c0504a809ebed.gif

使用

1. 添加包.

compile 'com.yalantis:ucrop:1.3.+'

2. 注册 UCropActivity 在 AndroidManifest.xml中

android:name="com.yalantis.ucrop.UCropActivity"

android:screenOrientation="portrait"

android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

3. 代码设置

UCrop.of(sourceUri, destinationUri)

.withAspectRatio(16, 9)

.withMaxResultSize(maxWidth, maxHeight)

.start(context);

4. 实现继承的方法

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {

final Uri resultUri = UCrop.getOutput(data);

} else if (resultCode == UCrop.RESULT_ERROR) {

final Throwable cropError = UCrop.getError(data);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以通过自定义View的方式来实现图片剪切圆角的效果。以下是一个简单的示例代码: 1. 自定义一个RoundImageView类,继承自ImageView: ``` public class RoundImageView extends ImageView { private Paint mPaint; private RectF mRectF; private float mRadius; public RoundImageView(Context context) { this(context, null); } public RoundImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mRectF = new RectF(); mPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { super.onDraw(canvas); return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap roundBitmap = getRoundBitmap(bitmap, getWidth(), getHeight(), mRadius); mPaint.reset(); canvas.drawBitmap(roundBitmap, 0, 0, mPaint); } private Bitmap getRoundBitmap(Bitmap bitmap, int width, int height, float radius) { Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); mRectF.set(0, 0, width, height); canvas.drawRoundRect(mRectF, radius, radius, mPaint); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, null, mRectF, mPaint); return output; } public void setRadius(float radius) { this.mRadius = radius; invalidate(); } } ``` 2. 在xml布局文件中引用这个RoundImageView: ``` <com.example.RoundImageView android:id="@+id/iv_round" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:src="@drawable/test" app:radius="20dp"/> ``` 3. 在Activity中找到这个RoundImageView,并调用setRadius()方法设置圆角半径: ``` RoundImageView roundImageView = findViewById(R.id.iv_round); roundImageView.setRadius(20); ``` 这样就可以实现在Android中对图片进行圆角剪切的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值