Skip to content

Commit

Permalink
Merge pull request #2 from jgluszczynski/master
Browse files Browse the repository at this point in the history
Add circle image crop from image view
  • Loading branch information
hpfs0 committed Jan 14, 2016
2 parents 548b989 + 5115e02 commit b46a6f4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +15,8 @@ public class MainActivity extends AppCompatActivity {

DragScaleCircleView mDragScaleCircleView;
Switch guideLineSwitch;
Button getCroppedImage;
ImageView croppedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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);

}
});
}
}
46 changes: 37 additions & 9 deletions dragscalecircleviewExample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:dragscalecircleview="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" tools:context=".MainActivity">
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">

<com.rori.zenvo.dragscalecircleview.DragScaleCircleView
android:id="@+id/dragScaleCircleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
dragscalecircleview:hasGuideLine="false"
android:src="@drawable/img1"
/>
dragscalecircleview:hasGuideLine="false"/>

<RelativeLayout
android:layout_below="@id/dragScaleCircleView"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:layout_below="@id/dragScaleCircleView">

<LinearLayout
android:id="@+id/option1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:padding="16dp">

<TextView
android:id="@+id/guideLineOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="5"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:text="Show Guideine"/>

<Switch
android:id="@+id/guideLineSwitch"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/getCroppedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get cropped image"/>

<ImageView
android:id="@+id/croppedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

</LinearLayout>

</RelativeLayout>

</RelativeLayout>

0 comments on commit b46a6f4

Please sign in to comment.