tag:blogger.com,1999:blog-62278656545575967762024-03-05T08:25:07.615-08:00Android Geek CoderAnonymoushttp://www.blogger.com/profile/07427573107090143546noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-6227865654557596776.post-30399836247590402062016-07-07T00:29:00.004-07:002016-07-07T01:38:25.539-07:00Android Freehand image crop<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: left;">
</div>
Hi Coders,<br />
I am going to explore a sample application in android ,which gives you<br />
an idea how to crop it according our selection (marking) image like Photoshop lasso tool crop...<br />
Hope this will helps you......<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><div style="text-align: left;">
<div style="text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdbDOdqzx7iuxcXHCod7z0nIcT8avnc_14OatVGND4Xlg5Hi49oaHkoBjcfOdKOCdGEks6TYpdP-Yu10n2gMPrbDAqhj9safwuBAwpnEfZv5GTAzbjSfXn9EvGOTLnuowqSpWpHM1JtWgg/s1600/screens.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdbDOdqzx7iuxcXHCod7z0nIcT8avnc_14OatVGND4Xlg5Hi49oaHkoBjcfOdKOCdGEks6TYpdP-Yu10n2gMPrbDAqhj9safwuBAwpnEfZv5GTAzbjSfXn9EvGOTLnuowqSpWpHM1JtWgg/s400/screens.jpg" width="400" /></a></div>
</div>
</td></tr>
<tr><td class="tr-caption" style="text-align: center;"><br />
<a name='more'></a><br />
<div style="text-align: left;">
Create a Project with Android Studio</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Create <b style="color: red;">CropView.java </b>and paste it below code</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: navy; font-weight: bold;">package </span>com.app.mydemos;
<span style="color: navy; font-weight: bold;">import </span>android.app.AlertDialog;
<span style="color: navy; font-weight: bold;">import </span>android.content.Context;
<span style="color: navy; font-weight: bold;">import </span>android.content.DialogInterface;
<span style="color: navy; font-weight: bold;">import </span>android.content.Intent;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Bitmap;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.BitmapFactory;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Canvas;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Color;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.DashPathEffect;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Paint;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Path;
<span style="color: navy; font-weight: bold;">import </span>android.util.AttributeSet;
<span style="color: navy; font-weight: bold;">import </span>android.util.Log;
<span style="color: navy; font-weight: bold;">import </span>android.view.MotionEvent;
<span style="color: navy; font-weight: bold;">import </span>android.view.View;
<span style="color: navy; font-weight: bold;">import </span>java.util.ArrayList;
<span style="color: navy; font-weight: bold;">import </span>java.util.List;
<span style="color: navy; font-weight: bold;">
</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: navy; font-weight: bold;">public class </span>CropView <span style="color: navy; font-weight: bold;">extends </span>View <span style="color: navy; font-weight: bold;">implements </span>View.OnTouchListener {
<span style="color: navy; font-weight: bold;">private </span>Paint <span style="color: #660e7a; font-weight: bold;">paint</span>;
<span style="color: navy; font-weight: bold;">public static </span>List<Point> <span style="color: #660e7a; font-style: italic;">points</span>;
<span style="color: navy; font-weight: bold;">int </span><span style="color: #660e7a; font-weight: bold;">DIST </span>= <span style="color: blue;">2</span>;
<span style="color: navy; font-weight: bold;">boolean </span><span style="color: #660e7a; font-weight: bold;">flgPathDraw </span>= <span style="color: navy; font-weight: bold;">true</span>;
Point <span style="color: #660e7a; font-weight: bold;">mfirstpoint </span>= <span style="color: navy; font-weight: bold;">null</span>;
<span style="color: navy; font-weight: bold;">boolean </span><span style="color: #660e7a; font-weight: bold;">bfirstpoint </span>= <span style="color: navy; font-weight: bold;">false</span>;
Point <span style="color: #660e7a; font-weight: bold;">mlastpoint </span>= <span style="color: navy; font-weight: bold;">null</span>;
Bitmap <span style="color: #660e7a; font-weight: bold;">bitmap </span>= BitmapFactory.<span style="font-style: italic;">decodeResource</span>(getResources(),
R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">pk</span>);
Context <span style="color: #660e7a; font-weight: bold;">mContext</span>;
<span style="color: navy; font-weight: bold;">public </span>CropView(Context c) {
<span style="color: navy; font-weight: bold;">super</span>(c);
<span style="color: #660e7a; font-weight: bold;">mContext </span>= c;
setFocusable(<span style="color: navy; font-weight: bold;">true</span>);
setFocusableInTouchMode(<span style="color: navy; font-weight: bold;">true</span>);
<span style="color: #660e7a; font-weight: bold;">paint </span>= <span style="color: navy; font-weight: bold;">new </span>Paint(Paint.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ANTI_ALIAS_FLAG</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setStyle(Paint.Style.<span style="color: #660e7a; font-style: italic; font-weight: bold;">STROKE</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setPathEffect(<span style="color: navy; font-weight: bold;">new </span>DashPathEffect(<span style="color: navy; font-weight: bold;">new float</span>[] { <span style="color: blue;">10</span>, <span style="color: blue;">20 </span>}, <span style="color: blue;">0</span>));
<span style="color: #660e7a; font-weight: bold;">paint</span>.setStrokeWidth(<span style="color: blue;">5</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setColor(Color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">WHITE</span>);
<span style="color: navy; font-weight: bold;">this</span>.setOnTouchListener(<span style="color: navy; font-weight: bold;">this</span>);
<span style="color: #660e7a; font-style: italic;">points </span>= <span style="color: navy; font-weight: bold;">new </span>ArrayList<Point>();
<span style="color: #660e7a; font-weight: bold;">bfirstpoint </span>= <span style="color: navy; font-weight: bold;">false</span>;
}
<span style="color: navy; font-weight: bold;">public </span>CropView(Context context, AttributeSet attrs) {
<span style="color: navy; font-weight: bold;">super</span>(context, attrs);
<span style="color: #660e7a; font-weight: bold;">mContext </span>= context;
setFocusable(<span style="color: navy; font-weight: bold;">true</span>);
setFocusableInTouchMode(<span style="color: navy; font-weight: bold;">true</span>);
<span style="color: #660e7a; font-weight: bold;">paint </span>= <span style="color: navy; font-weight: bold;">new </span>Paint(Paint.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ANTI_ALIAS_FLAG</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setStyle(Paint.Style.<span style="color: #660e7a; font-style: italic; font-weight: bold;">STROKE</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setStrokeWidth(<span style="color: blue;">2</span>);
<span style="color: #660e7a; font-weight: bold;">paint</span>.setColor(Color.<span style="color: #660e7a; font-style: italic; font-weight: bold;">WHITE</span>);
<span style="color: navy; font-weight: bold;">this</span>.setOnTouchListener(<span style="color: navy; font-weight: bold;">this</span>);
<span style="color: #660e7a; font-style: italic;">points </span>= <span style="color: navy; font-weight: bold;">new </span>ArrayList<Point>();
<span style="color: #660e7a; font-weight: bold;">bfirstpoint </span>= <span style="color: navy; font-weight: bold;">false</span>;
}
<span style="color: navy; font-weight: bold;">public void </span>onDraw(Canvas canvas) {
canvas.drawBitmap(<span style="color: #660e7a; font-weight: bold;">bitmap</span>, <span style="color: blue;">0</span>, <span style="color: blue;">0</span>, <span style="color: navy; font-weight: bold;">null</span>);
Path path = <span style="color: navy; font-weight: bold;">new </span>Path();
<span style="color: navy; font-weight: bold;">boolean </span>first = <span style="color: navy; font-weight: bold;">true</span>;
<span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">0</span>; i < <span style="color: #660e7a; font-style: italic;">points</span>.size(); i += <span style="color: blue;">2</span>) {
Point point = <span style="color: #660e7a; font-style: italic;">points</span>.get(i);
<span style="color: navy; font-weight: bold;">if </span>(first) {
first = <span style="color: navy; font-weight: bold;">false</span>;
path.moveTo(point.<span style="color: #660e7a; font-weight: bold;">x</span>, point.<span style="color: #660e7a; font-weight: bold;">y</span>);
} <span style="color: navy; font-weight: bold;">else if </span>(i < <span style="color: #660e7a; font-style: italic;">points</span>.size() - <span style="color: blue;">1</span>) {
Point next = <span style="color: #660e7a; font-style: italic;">points</span>.get(i + <span style="color: blue;">1</span>);
path.quadTo(point.<span style="color: #660e7a; font-weight: bold;">x</span>, point.<span style="color: #660e7a; font-weight: bold;">y</span>, next.<span style="color: #660e7a; font-weight: bold;">x</span>, next.<span style="color: #660e7a; font-weight: bold;">y</span>);
} <span style="color: navy; font-weight: bold;">else </span>{
<span style="color: #660e7a; font-weight: bold;">mlastpoint </span>= <span style="color: #660e7a; font-style: italic;">points</span>.get(i);
path.lineTo(point.<span style="color: #660e7a; font-weight: bold;">x</span>, point.<span style="color: #660e7a; font-weight: bold;">y</span>);
}
}
canvas.drawPath(path, <span style="color: #660e7a; font-weight: bold;">paint</span>);
}
<span style="color: navy; font-weight: bold;">public boolean </span>onTouch(View view, MotionEvent event) {
Point point = <span style="color: navy; font-weight: bold;">new </span>Point();
point.<span style="color: #660e7a; font-weight: bold;">x </span>= (<span style="color: navy; font-weight: bold;">int</span>) event.getX();
point.<span style="color: #660e7a; font-weight: bold;">y </span>= (<span style="color: navy; font-weight: bold;">int</span>) event.getY();
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">flgPathDraw</span>) {
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">bfirstpoint</span>) {
<span style="color: navy; font-weight: bold;">if </span>(comparepoint(<span style="color: #660e7a; font-weight: bold;">mfirstpoint</span>, point)) {
<span style="color: #660e7a; font-style: italic;">points</span>.add(<span style="color: #660e7a; font-weight: bold;">mfirstpoint</span>);
<span style="color: #660e7a; font-weight: bold;">flgPathDraw </span>= <span style="color: navy; font-weight: bold;">false</span>;
showcropdialog();
} <span style="color: navy; font-weight: bold;">else </span>{
<span style="color: #660e7a; font-style: italic;">points</span>.add(point);
}
} <span style="color: navy; font-weight: bold;">else </span>{
<span style="color: #660e7a; font-style: italic;">points</span>.add(point);
}
<span style="color: navy; font-weight: bold;">if </span>(!(<span style="color: #660e7a; font-weight: bold;">bfirstpoint</span>)) {
<span style="color: #660e7a; font-weight: bold;">mfirstpoint </span>= point;
<span style="color: #660e7a; font-weight: bold;">bfirstpoint </span>= <span style="color: navy; font-weight: bold;">true</span>;
}
}
invalidate();
<span style="color: navy; font-weight: bold;">if </span>(event.getAction() == MotionEvent.<span style="color: #660e7a; font-style: italic; font-weight: bold;">ACTION_UP</span>) {
<span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span><span style="color: #660e7a; font-weight: bold;">mlastpoint </span>= point;
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">flgPathDraw</span>) {
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-style: italic;">points</span>.size() > <span style="color: blue;">12</span>) {
<span style="color: navy; font-weight: bold;">if </span>(!comparepoint(<span style="color: #660e7a; font-weight: bold;">mfirstpoint</span>, <span style="color: #660e7a; font-weight: bold;">mlastpoint</span>)) {
<span style="color: #660e7a; font-weight: bold;">flgPathDraw </span>= <span style="color: navy; font-weight: bold;">false</span>;
<span style="color: #660e7a; font-style: italic;">points</span>.add(<span style="color: #660e7a; font-weight: bold;">mfirstpoint</span>);
showcropdialog();
}
}
}
}
<span style="color: navy; font-weight: bold;">return true</span>;
}
<span style="color: navy; font-weight: bold;">private boolean </span>comparepoint(Point first, Point current) {
<span style="color: navy; font-weight: bold;">int </span>left_range_x = (<span style="color: navy; font-weight: bold;">int</span>) (current.<span style="color: #660e7a; font-weight: bold;">x </span>- <span style="color: blue;">3</span>);
<span style="color: navy; font-weight: bold;">int </span>left_range_y = (<span style="color: navy; font-weight: bold;">int</span>) (current.<span style="color: #660e7a; font-weight: bold;">y </span>- <span style="color: blue;">3</span>);
<span style="color: navy; font-weight: bold;">int </span>right_range_x = (<span style="color: navy; font-weight: bold;">int</span>) (current.<span style="color: #660e7a; font-weight: bold;">x </span>+ <span style="color: blue;">3</span>);
<span style="color: navy; font-weight: bold;">int </span>right_range_y = (<span style="color: navy; font-weight: bold;">int</span>) (current.<span style="color: #660e7a; font-weight: bold;">y </span>+ <span style="color: blue;">3</span>);
<span style="color: navy; font-weight: bold;">if </span>((left_range_x < first.<span style="color: #660e7a; font-weight: bold;">x </span>&& first.<span style="color: #660e7a; font-weight: bold;">x </span>< right_range_x)
&& (left_range_y < first.<span style="color: #660e7a; font-weight: bold;">y </span>&& first.<span style="color: #660e7a; font-weight: bold;">y </span>< right_range_y)) {
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-style: italic;">points</span>.size() < <span style="color: blue;">10</span>) {
<span style="color: navy; font-weight: bold;">return false</span>;
} <span style="color: navy; font-weight: bold;">else </span>{
<span style="color: navy; font-weight: bold;">return true</span>;
}
} <span style="color: navy; font-weight: bold;">else </span>{
<span style="color: navy; font-weight: bold;">return false</span>;
}
}
<span style="color: navy; font-weight: bold;">private void </span>showcropdialog() {
DialogInterface.OnClickListener dialogClickListener = <span style="color: navy; font-weight: bold;">new </span>DialogInterface.OnClickListener() {
<span style="color: olive;">@Override</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public void </span>onClick(DialogInterface dialog, <span style="color: navy; font-weight: bold;">int </span>which) {
Intent intent;
<span style="color: navy; font-weight: bold;">switch </span>(which) {
<span style="color: navy; font-weight: bold;">case </span>DialogInterface.<span style="color: #660e7a; font-style: italic; font-weight: bold;">BUTTON_POSITIVE</span>:<span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span>intent = <span style="color: navy; font-weight: bold;">new </span>Intent(<span style="color: #660e7a; font-weight: bold;">mContext</span>, ImageCropActivity.<span style="color: navy; font-weight: bold;">class</span>);
intent.putExtra(<span style="color: green; font-weight: bold;">"crop"</span>, <span style="color: navy; font-weight: bold;">true</span>);
<span style="color: #660e7a; font-weight: bold;">mContext</span>.startActivity(intent);
<span style="color: navy; font-weight: bold;">break</span>;
<span style="color: navy; font-weight: bold;">case </span>DialogInterface.<span style="color: #660e7a; font-style: italic; font-weight: bold;">BUTTON_NEGATIVE</span>:<span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span>intent = <span style="color: navy; font-weight: bold;">new </span>Intent(<span style="color: #660e7a; font-weight: bold;">mContext</span>, ImageCropActivity.<span style="color: navy; font-weight: bold;">class</span>);
intent.putExtra(<span style="color: green; font-weight: bold;">"crop"</span>, <span style="color: navy; font-weight: bold;">false</span>);
<span style="color: #660e7a; font-weight: bold;">mContext</span>.startActivity(intent);
<span style="color: #660e7a; font-weight: bold;">bfirstpoint </span>= <span style="color: navy; font-weight: bold;">false</span>;<span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;">
</span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">break</span>;
}
}
};
AlertDialog.Builder builder = <span style="color: navy; font-weight: bold;">new </span>AlertDialog.Builder(<span style="color: #660e7a; font-weight: bold;">mContext</span>);
builder.setMessage(<span style="color: green; font-weight: bold;">"Do you Want to save Crop or Non-crop image?"</span>)
.setPositiveButton(<span style="color: green; font-weight: bold;">"Crop"</span>, dialogClickListener)
.setNegativeButton(<span style="color: green; font-weight: bold;">"Non-crop"</span>, dialogClickListener).show()
.setCancelable(<span style="color: navy; font-weight: bold;">false</span>);
}
}</pre>
</div>
<br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Create <span style="color: red;"><b>image_crop_activity.xml</b> </span>file and paste it below code.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version=</span><span style="color: green; font-weight: bold;">"1.0" </span><span style="color: blue; font-weight: bold;">encoding=</span><span style="color: green; font-weight: bold;">"utf-8"</span><span style="font-style: italic;">?></span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><<span style="color: navy; font-weight: bold;">LinearLayout </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">=</span><span style="color: green; font-weight: bold;">"http://schemas.android.com/apk/res/android"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width=</span><span style="color: green; font-weight: bold;">"match_parent"</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height=</span><span style="color: green; font-weight: bold;">"match_parent"</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:gravity=</span><span style="color: green; font-weight: bold;">"center"</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:orientation=</span><span style="color: green; font-weight: bold;">"vertical"</span>>
<<span style="color: navy; font-weight: bold;">ImageView</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:id=</span><span style="color: green; font-weight: bold;">"@+id/iv"</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_width=</span><span style="color: green; font-weight: bold;">"wrap_content"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:layout_height=</span><span style="color: green; font-weight: bold;">"wrap_content" </span>/>
</<span style="color: navy; font-weight: bold;">LinearLayout</span>></pre>
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Create <b><span style="color: red;">ImageCropActivity.java</span></b> and past it below code.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: navy; font-weight: bold;">package </span>com.app.mydemos;
<span style="background-color: #e4e4ff; color: navy; font-weight: bold;">import</span><span style="color: navy; font-weight: bold;"> </span>android.app.Activity;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Bitmap;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.BitmapFactory;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Canvas;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Paint;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.Path;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.PorterDuff;
<span style="color: navy; font-weight: bold;">import </span>android.graphics.PorterDuffXfermode;
<span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;
<span style="color: navy; font-weight: bold;">import </span>android.util.DisplayMetrics;
<span style="color: navy; font-weight: bold;">import </span>android.widget.ImageView;
<span style="color: grey; font-style: italic;">
</span><span style="color: navy; font-weight: bold;">public class </span>ImageCropActivity <span style="color: navy; font-weight: bold;">extends </span><span style="background-color: #e4e4ff;">Activity</span> {
ImageView <span style="color: #660e7a; font-weight: bold;">compositeImageView</span>;
<span style="color: navy; font-weight: bold;">boolean </span><span style="color: #660e7a; font-weight: bold;">crop</span>;
<span style="color: olive;">@Override</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {
<span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.<span style="color: #660e7a; font-style: italic; font-weight: bold;">image_crop_activity</span>);
Bundle extras = getIntent().getExtras();
<span style="color: navy; font-weight: bold;">if </span>(extras != <span style="color: navy; font-weight: bold;">null</span>) {
<span style="color: #660e7a; font-weight: bold;">crop </span>= extras.getBoolean(<span style="color: green; font-weight: bold;">"crop"</span>);
}
<span style="color: navy; font-weight: bold;">int </span>widthOfscreen = <span style="color: blue;">0</span>;
<span style="color: navy; font-weight: bold;">int </span>heightOfScreen = <span style="color: blue;">0</span>;
DisplayMetrics dm = <span style="color: navy; font-weight: bold;">new </span>DisplayMetrics();
<span style="color: navy; font-weight: bold;">try </span>{
getWindowManager().getDefaultDisplay().getMetrics(dm);
} <span style="color: navy; font-weight: bold;">catch </span>(Exception ex) {
}
widthOfscreen = dm.<span style="color: #660e7a; font-weight: bold;">widthPixels</span>;
heightOfScreen = dm.<span style="color: #660e7a; font-weight: bold;">heightPixels</span>;
<span style="color: #660e7a; font-weight: bold;">compositeImageView </span>= (ImageView) findViewById(R.id.<span style="color: #660e7a; font-style: italic; font-weight: bold;">iv</span>);
Bitmap bitmap2 = BitmapFactory.<span style="font-style: italic;">decodeResource</span>(getResources(),
R.drawable.<span style="color: #660e7a; font-style: italic; font-weight: bold;">pk</span>);
Bitmap resultingImage = Bitmap.<span style="font-style: italic;">createBitmap</span>(widthOfscreen,
heightOfScreen, bitmap2.getConfig());
Canvas canvas = <span style="color: navy; font-weight: bold;">new </span>Canvas(resultingImage);
Paint paint = <span style="color: navy; font-weight: bold;">new </span>Paint();
paint.setAntiAlias(<span style="color: navy; font-weight: bold;">true</span>);
Path path = <span style="color: navy; font-weight: bold;">new </span>Path();
<span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">0</span>; i < CropView.<span style="color: #660e7a; font-style: italic;">points</span>.size(); i++) {
path.lineTo(CropView.<span style="color: #660e7a; font-style: italic;">points</span>.get(i).<span style="color: #660e7a; font-weight: bold;">x</span>, CropView.<span style="color: #660e7a; font-style: italic;">points</span>.get(i).<span style="color: #660e7a; font-weight: bold;">y</span>);
}
canvas.drawPath(path, paint);
<span style="color: navy; font-weight: bold;">if </span>(<span style="color: #660e7a; font-weight: bold;">crop</span>) {
paint.setXfermode(<span style="color: navy; font-weight: bold;">new </span>PorterDuffXfermode(PorterDuff.Mode.<span style="color: #660e7a; font-style: italic; font-weight: bold;">SRC_IN</span>));
} <span style="color: navy; font-weight: bold;">else </span>{
paint.setXfermode(<span style="color: navy; font-weight: bold;">new </span>PorterDuffXfermode(PorterDuff.Mode.<span style="color: #660e7a; font-style: italic; font-weight: bold;">SRC_OUT</span>));
}
canvas.drawBitmap(bitmap2, <span style="color: blue;">0</span>, <span style="color: blue;">0</span>, paint);
<span style="color: #660e7a; font-weight: bold;">compositeImageView</span>.setImageBitmap(resultingImage);
}
}</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"></pre>
</div>
<div style="text-align: left;">
Create <b><span style="color: red;">Point.java</span></b> class and past it below code.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: navy; font-weight: bold;">package </span>com.app.mydemos;
<span style="color: grey; font-style: italic;">
</span><span style="color: navy; font-weight: bold;">public class </span>Point {
<span style="color: navy; font-weight: bold;">public float </span><span style="color: #660e7a; font-weight: bold;">dy</span>;
<span style="color: navy; font-weight: bold;">public float </span><span style="color: #660e7a; font-weight: bold;">dx</span>;
<span style="color: navy; font-weight: bold;">float </span><span style="color: #660e7a; font-weight: bold;">x</span>, <span style="color: #660e7a; font-weight: bold;">y</span>;
<span style="color: olive;">@Override</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">public </span>String toString() {
<span style="color: navy; font-weight: bold;">return </span><span style="color: #660e7a; font-weight: bold;">x </span>+ <span style="color: green; font-weight: bold;">", " </span>+ <span style="color: #660e7a; font-weight: bold;">y</span>;
}
}</pre>
</div>
<br />
<div style="text-align: left;">
Create <b><span style="color: red;">MainActivty.java</span></b> class and paste it below code</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: navy; font-weight: bold;">package </span>com.app.mydemos;
<span style="color: navy; font-weight: bold;">import </span>android.app.Activity;
<span style="color: navy; font-weight: bold;">import </span>android.os.Bundle;
<span style="color: grey; font-style: italic;">
</span><span style="color: navy; font-weight: bold;">public class </span><span style="background-color: #e4e4ff;">MainActivity</span> <span style="color: navy; font-weight: bold;">extends </span>Activity {
<span style="color: olive;">@Override</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onCreate(Bundle savedInstanceState) {
<span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState);
}
<span style="color: olive;">@Override</span><span style="color: olive;"> </span><span style="color: navy; font-weight: bold;">protected void </span>onResume() {
<span style="color: navy; font-weight: bold;">super</span>.onResume();
setContentView(<span style="color: navy; font-weight: bold;">new </span>CropView(<span style="background-color: #e4e4ff;">MainActivity</span>.<span style="color: navy; font-weight: bold;">this</span>));
}
}</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"></pre>
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Open your <b><span style="color: red;">AndroidManifest.xml</span></b> file and paste it below code.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="font-style: italic;"><?</span><span style="color: blue; font-weight: bold;">xml version=</span><span style="color: green; font-weight: bold;">"1.0" </span><span style="color: blue; font-weight: bold;">encoding=</span><span style="color: green; font-weight: bold;">"utf-8"</span><span style="font-style: italic;">?></span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><<span style="color: navy; font-weight: bold;">manifest </span><span style="color: blue; font-weight: bold;">xmlns:</span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">=</span><span style="color: green; font-weight: bold;">"http://schemas.android.com/apk/res/android"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: blue; font-weight: bold;">package=</span><span style="color: green; font-weight: bold;">"com.app.mydemos" </span>>
<<span style="color: navy; font-weight: bold;">application</span><span style="color: navy; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:allowBackup=</span><span style="color: green; font-weight: bold;">"true"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:icon=</span><span style="color: green; font-weight: bold;">"@mipmap/ic_launcher"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:label=</span><span style="color: green; font-weight: bold;">"@string/app_name"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:theme=</span><span style="color: green; font-weight: bold;">"@style/AppTheme" </span>></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><<span style="color: navy; font-weight: bold;">activity</span><span style="color: navy; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;"> android</span><span style="color: blue; font-weight: bold;">:name=</span><span style="color: green; font-weight: bold;">".MainActivity"</span><span style="color: green; font-weight: bold;"> </span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"><span style="color: #660e7a; font-weight: bold;"> android</span><span style="color: blue; font-weight: bold;">:label=</span><span style="color: green; font-weight: bold;">"@string/app_name" </span>></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"> <<span style="color: navy; font-weight: bold;">intent-filter</span>>
<<span style="color: navy; font-weight: bold;">action </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name=</span><span style="color: green; font-weight: bold;">"android.intent.action.MAIN" </span>/>
<<span style="color: navy; font-weight: bold;">category </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name=</span><span style="color: green; font-weight: bold;">"android.intent.category.LAUNCHER" </span>/>
</<span style="color: navy; font-weight: bold;">intent-filter</span>></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"> </<span style="color: navy; font-weight: bold;">activity</span>></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"> <<span style="color: navy; font-weight: bold;">activity </span><span style="color: #660e7a; font-weight: bold;">android</span><span style="color: blue; font-weight: bold;">:name=</span><span style="color: green; font-weight: bold;">".ImageCropActivity"</span>/></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 10.5pt;"> </<span style="color: navy; font-weight: bold;">application</span>>
</<span style="color: navy; font-weight: bold;">manifest</span>></pre>
</div>
</td></tr>
</tbody></table>
</div>
Anonymoushttp://www.blogger.com/profile/07427573107090143546noreply@blogger.com4