图像可以来自图库,也可以来自相机……
对此有什么解决方案吗?
解决方法 >在图库中加载图库或相机中的图像(使用画布绘制图像)public class SomeVIEw extends VIEw implements OntouchListener { private Paint paint; public static List<Point> points; int disT = 2; boolean flgPathDraw = true; Point mfirstpoint = null; boolean bfirstpoint = false; Point mlastpoint = null; Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.gallery_12); Context mContext; public SomeVIEw(Context c) { super(c); mContext = c; setFocusable(true); setFocusableIntouchMode(true); paint = new Paint(Paint.ANTI_AliAS_FLAG); paint.setStyle(Paint.Style.stroke); paint.setPathEffect(new DashPathEffect(new @R_301_5987@[] { 10,20 },0)); paint.setstrokeWIDth(5); paint.setcolor(color.WHITE); this.setontouchListener(this); points = new ArrayList<Point>(); bfirstpoint = false; } public SomeVIEw(Context context,AttributeSet attrs) { super(context,attrs); mContext = context; setFocusable(true); setFocusableIntouchMode(true); paint = new Paint(Paint.ANTI_AliAS_FLAG); paint.setStyle(Paint.Style.stroke); paint.setstrokeWIDth(2); paint.setcolor(color.WHITE); this.setontouchListener(this); points = new ArrayList<Point>(); bfirstpoint = false; } public voID onDraw(Canvas canvas) { canvas.drawBitmap(bitmap,null); Path path = new Path(); boolean first = true; for (int i = 0; i < points.size(); i += 2) { Point point = points.get(i); if (first) { first = false; path.moveto(point.x,point.y); } else if (i < points.size() - 1) { Point next = points.get(i + 1); path.quadTo(point.x,point.y,next.x,next.y); } else { mlastpoint = points.get(i); path.lineto(point.x,point.y); } } canvas.drawPath(path,paint); } public boolean ontouch(VIEw vIEw,MotionEvent event) { // if(event.getAction() != MotionEvent.ACTION_DOWN) // return super.ontouchEvent(event); Point point = new Point(); point.x = (int) event.getX(); point.y = (int) event.getY(); if (flgPathDraw) { if (bfirstpoint) { if (comparepoint(mfirstpoint,point)) { // points.add(point); points.add(mfirstpoint); flgPathDraw = false; showcropdialog(); } else { points.add(point); } } else { points.add(point); } if (!(bfirstpoint)) { mfirstpoint = point; bfirstpoint = true; } } invalIDate(); Log.e("Hi ==>","Size: " + point.x + " " + point.y); if (event.getAction() == MotionEvent.ACTION_UP) { Log.d("Action uP*******~~~~~~~>>>>","called"); mlastpoint = point; if (flgPathDraw) { if (points.size() > 12) { if (!comparepoint(mfirstpoint,mlastpoint)) { flgPathDraw = false; points.add(mfirstpoint); showcropdialog(); } } } } return true; } private boolean comparepoint(Point first,Point current) { int left_range_x = (int) (current.x - 3); int left_range_y = (int) (current.y - 3); int right_range_x = (int) (current.x + 3); int right_range_y = (int) (current.y + 3); if ((left_range_x < first.x && first.x < right_range_x) && (left_range_y < first.y && first.y < right_range_y)) { if (points.size() < 10) { return false; } else { return true; } } else { return false; } } public voID fillinPartofPath() { Point point = new Point(); point.x = points.get(0).x; point.y = points.get(0).y; points.add(point); invalIDate(); } public voID resetVIEw() { points.clear(); paint.setcolor(color.WHITE); paint.setStyle(Style.stroke); flgPathDraw = true; invalIDate(); } private voID showcropdialog() { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { Intent intent; switch (which) { case DialogInterface.button_POSITIVE: // Yes button clicked // bfirstpoint = false; intent = new Intent(mContext,CropActivity.class); intent.putExtra("crop",true); mContext.startActivity(intent); break; case DialogInterface.button_NEGATIVE: // No button clicked intent = new Intent(mContext,false); mContext.startActivity(intent); bfirstpoint = false; // resetVIEw(); break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage("Do you Want to save Crop or Non-crop image?") .setPositivebutton("Crop",dialogClickListener) .setNegativebutton("Non-crop",dialogClickListener).show() .setCancelable(false); } }class Point { public @R_301_5987@ dy; public @R_301_5987@ dx; @R_301_5987@ x,y;@OverrIDepublic String toString() { return x + "," + y; }}
> MainActivity调用SomevIEw来绘制/加载图像
public class MainActivity_ extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);}@OverrIDeprotected voID onResume() { super.onResume(); setContentVIEw(new SomeVIEw(MainActivity_.this));}}
> CropActivity:在位图上加载裁剪图像.
public class CropActivity extends Activity { ImageVIEw compositeImageVIEw; boolean crop; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.cropvIEw); Bundle extras = getIntent().getExtras(); if (extras != null) { crop = extras.getBoolean("crop"); } int wIDthOfscreen = 0; int heightOfScreen = 0; displayMetrics dm = new displayMetrics(); try { getwindowManager().getDefaultdisplay().getMetrics(dm); } catch (Exception ex) { } wIDthOfscreen = dm.wIDthPixels; heightOfScreen = dm.heightPixels; compositeImageVIEw = (ImageVIEw) findVIEwByID(R.ID.our_imagevIEw); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.gallery_12); Bitmap resultingImage = Bitmap.createBitmap(wIDthOfscreen,heightOfScreen,bitmap2.getConfig()); Canvas canvas = new Canvas(resultingImage); Paint paint = new Paint(); paint.setAntiAlias(true); Path path = new Path(); for (int i = 0; i < SomeVIEw.points.size(); i++) { path.lineto(SomeVIEw.points.get(i).x,SomeVIEw.points.get(i).y); } canvas.drawPath(path,paint); if (crop) { paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); } else { paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); } canvas.drawBitmap(bitmap2,paint); compositeImageVIEw.setimageBitmap(resultingImage); }}总结
以上是内存溢出为你收集整理的Android:免费裁剪图像全部内容,希望文章能够帮你解决Android:免费裁剪图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)