From 5115e022f4895e720d1662fabe00cd8b578b16ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20G=C5=82uszczyn=CC=81ski?= Date: Wed, 13 Jan 2016 10:02:21 +0100 Subject: [PATCH] Add circle image crop from image view --- .../DragScaleCircleView.java | 72 ++++++++++++++++++- .../MainActivity.java | 17 +++++ .../src/main/res/layout/activity_main.xml | 46 +++++++++--- 3 files changed, 123 insertions(+), 12 deletions(-) diff --git a/dragscalecircleview/src/main/java/com/rori/zenvo/dragscalecircleview/DragScaleCircleView.java b/dragscalecircleview/src/main/java/com/rori/zenvo/dragscalecircleview/DragScaleCircleView.java index 7086085..d46850d 100644 --- a/dragscalecircleview/src/main/java/com/rori/zenvo/dragscalecircleview/DragScaleCircleView.java +++ b/dragscalecircleview/src/main/java/com/rori/zenvo/dragscalecircleview/DragScaleCircleView.java @@ -20,7 +20,11 @@ import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.graphics.Rect; import android.graphics.RectF; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -240,6 +244,70 @@ public void setmBorderColor(int mBorderColor) { this.mBorderColor = mBorderColor; } + /** + * Get cropped circle bitmap of selected part of imagge + * @return + */ + public Bitmap getCroppedCircleBitmap() { + + Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap(); + + final Paint paint = new Paint(); + final RectF croppedRect = getCroppedRect(); + + croppedRect.top -= mBitmapRect.top; + croppedRect.left -= mBitmapRect.left; + croppedRect.bottom -= mBitmapRect.top; + croppedRect.right -= mBitmapRect.left; + + float scale = getScale(); + setRectScale(croppedRect, scale); + + final Rect rectSrc = new Rect(); + rectSrc.set((int) croppedRect.left, (int) croppedRect.top, (int) croppedRect.right, (int) croppedRect.bottom); + + int dstImageSize = rectSrc.width(); + final Rect rectDst = new Rect(0, 0, dstImageSize, dstImageSize); + float dstRect = (float) dstImageSize / 2f; + Bitmap mOutputBitmap = Bitmap.createBitmap(dstImageSize, dstImageSize, Bitmap.Config.ARGB_4444); + + Canvas mCanvas = new Canvas(mOutputBitmap); + mCanvas.drawARGB(0, 0, 0, 0); + mCanvas.drawCircle(dstRect, dstRect, dstRect, paint); + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + mCanvas.drawBitmap(bitmap, rectSrc, rectDst, paint); + + return mOutputBitmap; + } + + /** + * Get displayed image scale (image displayed on screen has different size than bitmap assigned to ImageView) + * @return + */ + private float getScale() { + Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap(); + return bitmap.getWidth() / mBitmapRect.width(); + } + + private void setRectScale(RectF rect, float scale) { + rect.top = rect.top * scale; + rect.left = rect.left * scale; + rect.bottom = rect.bottom * scale; + rect.right = rect.right * scale; + } + + /** + * Get rect for cropped part of image + * @return + */ + private RectF getCroppedRect() { + float x1 = mCenterPointX - mRadius; + float y1 = mCenterPointY - mRadius; + float x2 = mCenterPointX + mRadius; + float y2 = mCenterPointY + mRadius; + return new RectF(x1, y1, x2, y2); + } + @Override public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); @@ -275,7 +343,6 @@ protected void init(@NonNull Context context, @Nullable AttributeSet attrs) { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - super.onLayout(changed, left, top, right, bottom); mBitmapRect = getBitmapRect(); @@ -333,7 +400,6 @@ public boolean onTouch(View v, MotionEvent event) { } else if (action == MotionEvent.ACTION_MOVE) { setHandleMode(HANDLE_MOVE); drag((DragScaleCircleView) v, event, action); - } else if (action == MotionEvent.ACTION_UP) { setHandleMode(HANDLE_UP); drag((DragScaleCircleView) v, event, action); @@ -474,7 +540,7 @@ private void initCircleCropWindow(@NonNull RectF bitmapRect) { mDrawableHeight = bitmapRect.height() / mScaleY; mCenterPointX = mDrawableWidth / 2.0f; mCenterPointY = mDrawableHeight / 2.0f; - mRadius = (Math.min(mDrawableWidth, mDrawableHeight) - mOffset) / 2.0f; + mRadius = (Math.min(bitmapRect.width(), bitmapRect.height()) - mOffset) / 2.0f; } /** diff --git a/dragscalecircleviewExample/src/main/java/com/rori/zenvo/dragscalecircleviewexample/MainActivity.java b/dragscalecircleviewExample/src/main/java/com/rori/zenvo/dragscalecircleviewexample/MainActivity.java index 3dd65f9..242c70d 100644 --- a/dragscalecircleviewExample/src/main/java/com/rori/zenvo/dragscalecircleviewexample/MainActivity.java +++ b/dragscalecircleviewExample/src/main/java/com/rori/zenvo/dragscalecircleviewexample/MainActivity.java @@ -1,8 +1,12 @@ package com.rori.zenvo.dragscalecircleviewexample; +import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; import android.widget.CompoundButton; +import android.widget.ImageView; import android.widget.Switch; import com.rori.zenvo.dragscalecircleview.DragScaleCircleView; @@ -11,6 +15,8 @@ public class MainActivity extends AppCompatActivity { DragScaleCircleView mDragScaleCircleView; Switch guideLineSwitch; + Button getCroppedImage; + ImageView croppedImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -28,5 +34,16 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } } }); + croppedImage = (ImageView) findViewById(R.id.croppedImage); + getCroppedImage = (Button) findViewById(R.id.getCroppedImage); + getCroppedImage.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + Bitmap croppedBitmap = mDragScaleCircleView.getCroppedCircleBitmap(); + croppedImage.setImageBitmap(croppedBitmap); + + } + }); } } diff --git a/dragscalecircleviewExample/src/main/res/layout/activity_main.xml b/dragscalecircleviewExample/src/main/res/layout/activity_main.xml index 471229d..643f973 100644 --- a/dragscalecircleviewExample/src/main/res/layout/activity_main.xml +++ b/dragscalecircleviewExample/src/main/res/layout/activity_main.xml @@ -1,7 +1,10 @@ + xmlns:dragscalecircleview="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=".MainActivity"> + dragscalecircleview:hasGuideLine="false"/> + android:layout_height="match_parent" + android:layout_below="@id/dragScaleCircleView"> + + + android:layout_height="wrap_content" + android:padding="16dp"> + + + + + +