如何在放大android后画一个圆

我正在使用图层视图和使用layerDrawable绘制叠加图像。 我使用了两个原始位图和myBitmap。 缩放后,我无法在正确的位置绘制圆,它被绘制在不同的位置。

这是我正在使用的代码,

ImageView view = (ImageView) findViewById(R.id.imageView); view.setOnTouchListener(this); Options options = new BitmapFactory.Options(); options.inScaled = false; original = BitmapFactory.decodeResource(getResources(), R.drawable.mainscreen,options); original= getResizedBitmap(original, width, 200); myBitmap = func(original); Resources r = getResources(); layers = new Drawable[2]; layers[0] = new BitmapDrawable(original); layers[1] = new BitmapDrawable(myBitmap); LayerDrawable layerDrawable = new LayerDrawable(layers); view.setImageDrawable(layerDrawable); bitmap = Bitmap.createBitmap(width, 200, Config.ARGB_8888); pcanvas = new Canvas(); pcanvas.setBitmap(bitmap); pcanvas.drawBitmap(grayScale, 0, 0, null); public boolean onTouch(View v, MotionEvent rawEvent) { WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent); // ... ImageView view = (ImageView) v; // Dump touch event to log // dumpEvent(event); if (isZoomRequired == false) { x = (int) rawEvent.getX(); y = (int) rawEvent.getY(); r = SettingsActivity.brushsize; pcanvas.drawCircle(x, y, r, mPaint); layers[1] = new BitmapDrawable(bitmap); LayerDrawable layerDrawable = new LayerDrawable(layers); view.setImageDrawable(layerDrawable); } else { // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: savedMatrix.set(matrix); start.set(event.getX(), event.getY()); Log.d(TAG, "mode=DRAG"); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); Log.d(TAG, "oldDist=" + oldDist); if (oldDist > 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; Log.d(TAG, "mode=ZOOM"); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; Log.d(TAG, "mode=NONE"); break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { // ... matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { float newDist = spacing(event); Log.d(TAG, "newDist=" + newDist); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); } } break; } view.setImageMatrix(matrix); } return true; // indicate event was handled } 

请帮助我。 缩放后,我需要在正确的位置绘制圆。

任何帮助,将不胜感激。

Solutions Collecting From Web of "如何在放大android后画一个圆"

你可以添加图片显示你想要得到什么,而图片显示你得到什么?

有一件事要记住,某些操作对y轴使用不同的零点。 我一直在面对这个matrix运算,我必须计算一个校正因子,因为一个操作使用视图的顶部为y = 0,matrix操作使用视图的底部为y = 0 ,所以实际的matrix运算需要类似于:matrixY =(totalHeightY – Yposition)。

但是显示你的期望和你得到的将有助于诊断问题比运行和通过你的代码工作更快:)

我可以看到在使用postTranslate之前,还没有使用过matrix.preTranslate,以确保视图在正确的位置设置图像。

如果不知道自己想做什么,这很难正确回答。

我build议你创build另一个扩展View的类,并使用它在你的活动中在你的ImageView上绘制圆。 这也可以让你绘制尽可能多的圈子(如果你打算超过1),而不必在你的活动中创build一些function。

一旦你创build了扩展视图的类,那么在你的onDraw()方法中,你可以通过canvas.drawCircle()和canvas.translate()在屏幕上移动它。 您将不得不使用onTouchEvent并拦截触摸事件。

这里有一个很好的例子,apk包括: http : //adblogcat.com/custom-view-to-draw-rotate-erase-and-convert-images-to-black-and-white/

就像下面的OnTouchListener一样,你可以放大你的图片或者位图

 imageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: savedMatrix.set(matrix1); start.set(event.getX(), event.getY()); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); if (oldDist > 10f) { start.set(event.getX(), event.getY()); savedMatrix.set(matrix1); midPoint(mid, event); // mode = POINT2; mode = ZOOM; } break; case MotionEvent.ACTION_UP: mode = NONE; distanceOffset = minOffset; case MotionEvent.ACTION_POINTER_UP: mode = NONE; distanceOffset = minOffset; break; case MotionEvent.ACTION_MOVE: if (mode == POINT2) { newDist = spacing(event); if (newDist - oldDist > 5f || newDist - oldDist < -5f) { mode = ZOOM; } else { start.set(event.getX(), event.getY()); mode = DRAG; } } else if (mode == DRAG) { matrix1.set(savedMatrix); matrix1.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { newDist = spacing(event); if (newDist > 10f) { matrix1.set(savedMatrix); float scale = newDist / oldDist; matrix1.postScale(scale, scale, mid.x, mid.y); finalscale = scale; } } break; } view.setImageMatrix(matrix1); // matrixTurning(matrix1, view); return true; // indicate event was handled } }); } 

现在开始在图像的缩放部分绘制圆,在这里我写简单的paint.You代码绘制圆在下面的listener.HOP这对你有用。

 mPaintView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { PaintView view = (PaintView) v; view.setScaleType(ImageView.ScaleType.MATRIX); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: if (falg) { savedMatrix.set(matrix); start.set(event.getX(), event.getY()); mode = DRAG; } else { view.onTouchEvent(event); } break; case MotionEvent.ACTION_POINTER_DOWN: if (falg) { oldDist = spacing(event); if (oldDist > 10f) { start.set(event.getX(), event.getY()); savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; } } break; case MotionEvent.ACTION_UP: if (falg) { mode = NONE; distanceOffset = minOffset; } case MotionEvent.ACTION_POINTER_UP: if (falg) { mode = NONE; distanceOffset = minOffset; } break; case MotionEvent.ACTION_MOVE: if (falg) { if (mode == POINT2) { newDist = spacing(event); if (newDist - oldDist > 5f || newDist - oldDist < -5f) { mode = ZOOM; } else { start.set(event.getX(), event.getY()); mode = DRAG; } } else if (mode == DRAG) { matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { newDist = spacing(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); finalscale = scale; } } } else { view.onTouchEvent(event); } break; } limitZoom(matrix); view.setImageMatrix(matrix); matrixTurning(matrix, view); RectF r = new RectF(); matrix.mapRect(r); scaledImageOffsetX = r.left; scaledImageOffsetY = r.top; return true; } }); } private void limitZoom(Matrix m) { float[] values = new float[9]; m.getValues(values); float scaleX = values[Matrix.MSCALE_X]; float scaleY = values[Matrix.MSCALE_Y]; if(scaleX > MAX_ZOOM) { scaleX = MAX_ZOOM; } else if(scaleX < MIN_ZOOM) { scaleX = MIN_ZOOM; } if(scaleY > MAX_ZOOM) { scaleY = MAX_ZOOM; } else if(scaleY < MIN_ZOOM) { scaleY = MIN_ZOOM; } values[Matrix.MSCALE_X] = scaleX; values[Matrix.MSCALE_Y] = scaleY; m.setValues(values); } public boolean getFlag(boolean b) { return falg = b; } 

PaintView.class

 class PaintView extends ImageView { private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; // onDraw private Paint mPaint; // onTouch private float mX, mY; private static final float TOUCH_TOLERANCE = 4; public PaintView(Context context) { this(context, null); } public PaintView(Context context, AttributeSet attrs) { super(context, attrs); mBitmap = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { // canvas.drawColor(0xFFAAAAAA); super.onDraw(canvas); mCanvas = canvas; // canvas = mCanvas; canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); // canvas.drawBitmap(mBitmap, PaintScreen.matrix, mBitmapPaint); canvas.drawPath(mPath, mPaint); } public void clear() { mPaint.reset(); // invalidate(); } public void setMPaint(Paint paint) { mPaint = paint; } private void touchStart(float x, float y) { // mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; } private void touchMove(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } } private void touchUp() { mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw mPath.reset(); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); Log.d("PaintView", "ev ->" + event.getAction()); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchStart(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: touchMove(x, y); invalidate(); break; case MotionEvent.ACTION_UP: touchUp(); invalidate(); break; } return true; } public void cMatrix(Matrix matrix) { mCanvas.setMatrix(matrix); } 

}