在这个我选择区域.我使用的代码就是这个.
private float x,y;private boolean zooming = false;private Paint mPaint;private Matrix mmatrix;private Shader mShader;private Bitmap mBitmap;private List<Point> mpoints;private List<MyPoints> mpointList;private Path mpath;private Canvas mcanvas;private Bitmap mresult_bitmap,resultingImage,finalbitmap;private Context mcontext;private boolean bfirstpoint = false;private Point mfirstpoint = null;private Point mlastpoint = null;public CircularZoomVIEw(Context context) { super(context); mcontext = context; mpath = new Path(); mpoints = new ArrayList<Point>(); setBackgroundResource(R.drawable.testing); mPaint = new Paint(); mresult_bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.testing); mPaint = new Paint(Paint.ANTI_AliAS_FLAG); mPaint.setStyle(Paint.Style.stroke); mPaint.setPathEffect(new DashPathEffect(new float[] { 10,20 },0)); mPaint.setstrokeWIDth(5); mPaint.setcolor(color.RED);}@OverrIDeprotected voID onDraw(Canvas canvas) { super.onDraw(canvas); if (zooming && resultingImage!=null) { zooming = false; ShowImage(resultingImage); canvas.drawBitmap(resultingImage,mmatrix,null); } boolean first = true; for (int i = 0; i < mpoints.size(); i += 2) { Point point = mpoints.get(i); if (first) { first = false; mpath.moveto(point.x,point.y); } else if (i < mpoints.size() - 1) { Point next = mpoints.get(i + 1); mpath.quadTo(point.x,point.y,next.x,next.y); } else { mlastpoint = mpoints.get(i); mpath.lineto(point.x,point.y); } } canvas.drawPath(mpath,mPaint);}@OverrIDepublic boolean ontouchEvent(MotionEvent event) { int action = event.getAction(); x = event.getX(); y = event.getY(); Point point = new Point(); point.x = (int) event.getX(); point.y = (int) event.getY(); if (bfirstpoint) { if (comparepoint(mfirstpoint,point)) { mpoints.add(mfirstpoint); addCircleFromPath(mpath); } else { mpoints.add(point); } } else { mpoints.add(point); } if (!(bfirstpoint)) { mfirstpoint = point; bfirstpoint = true; } invalIDate(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: zooming = false; this.invalIDate(); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: zooming = true; mlastpoint = point; if (mpoints.size() > 12) { if (!comparepoint(mfirstpoint,mlastpoint)) { mpoints.add(mfirstpoint); addCircleFromPath(mpath); } } this.invalIDate(); break; default: break; } return true;}public Bitmap getCroppedBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWIDth(),bitmap.getHeight(),Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0,bitmap.getWIDth(),bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0,0); paint.setcolor(color); canvas.drawCircle(bitmap.getWIDth() / 2,bitmap.getHeight() / 2,bitmap.getWIDth() / 2,paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,paint); return output;}voID ShowImage(Bitmap mbitmap) { display display = ((MainActivity) mcontext).getwindowManager().getDefaultdisplay(); int screenWIDth = display.getWIDth(); float imageWIDth = (float)mbitmap.getWIDth(); float imageHeight = (float)mbitmap.getHeight(); float newHeight = imageHeight / (imageWIDth / screenWIDth); float newWIDth = screenWIDth; float scaleWIDth = screenWIDth / imageWIDth; float scaleHeight = newHeight / imageHeight; SetimageMatrix(mbitmap,scaleWIDth,scaleHeight); }voID SetimageMatrix(Bitmap image,float scaleWIDth,float scaleHeight) { mmatrix = new Matrix(); mmatrix.setTranslate(40,40); mmatrix.postscale(scaleWIDth/2,scaleHeight/2); /*image.setimageMatrix(mmatrix); image.setScaleType(ScaleType.MATRIX); image.invalIDate();*/ }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 (mpoints.size() < 10) { return false; } else { return true; } } else { return false; }}private voID addCircleFromPath(Path path){ RectF bounds = new RectF(); path.computeBounds(bounds,true); int wIDth = (int) (bounds.right-bounds.left); int height = (int) (bounds.bottom-bounds.top); if(wIDth<20 && height<20){ path.reset(); return; } int radius ; if(wIDth>=height) radius = Math.round(((wIDth/2))); else radius = Math.round((int) ((height/2))); /*CircleTagObject circle = new CircleTagObject((int)bounds.left+wIDth/2,(int)bounds.top+height/2,radius,crossBitmap,tagBitmap,circleArray.size(),ImageEditorVIEw.this); circleArray.add(circle); tagBallID = circleArray.size() - 1; dragEnable = true;*/ resultingImage = getCroppedBitmap(Bitmap.createBitmap(mresult_bitmap,200,200)); mcanvas = new Canvas(resultingImage); path.reset(); resetVIEw(); invalIDate();}public voID resetVIEw() { mpoints.clear(); mPaint.setStyle(Paint.Style.stroke); mPaint.setPathEffect(new DashPathEffect(new float[] { 10,0)); mPaint.setstrokeWIDth(5); mPaint.setcolor(color.RED); invalIDate();}
如果我像上面那样创建硬编码位图,它显示的是好的但不是所选部分的裁剪位图.就像这个图像一样.
但是当添加所选区域的精确坐标时如: –
resultingImage = getCroppedBitmap(Bitmap.createBitmap(mresult_bitmap,(int)bounds.left,(int)bounds.top,wIDth,height));
然后发生异常: –
07-12 10:58:56.700: E/MessageQueue-JNI(12310): java.lang.IllegalArgumentException: y + height must be <= bitmap.height()07-12 10:58:56.700: E/MessageQueue-JNI(12310): at androID.graphics.Bitmap.createBitmap(Bitmap.java:565)07-12 10:58:56.700: E/MessageQueue-JNI(12310): at androID.graphics.Bitmap.createBitmap(Bitmap.java:530)07-12 10:58:56.700: E/MessageQueue-JNI(12310): at com.intel.vIEw.CircularZoomVIEw.addCircleFromPath(CircularZoomVIEw.java:237)
我知道为什么会发生此异常,但无法找到解决方案如何裁剪所选部分的裁剪图像.提前谢谢.
解决方法 我知道你的解决方案为时已晚,但这可能对其他人有帮助 Use of this code 帮助你摆脱这个问题. 总结以上是内存溢出为你收集整理的android – 如何在画布上选择区域的裁剪位图?全部内容,希望文章能够帮你解决android – 如何在画布上选择区域的裁剪位图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)