如何从多个位图放大和缩小Android画布中的位图?

时间:2016-09-01 11:47:54

标签: java android canvas bitmap

我在Android画布上创建了bitmap,我希望放大和缩小我在Arraylist上存储图像。它正确缩放,但当bitmap所有bitmap放大和缩小时有多个bitmap。我想仅缩放我触摸的特定bitmap。在我的代码中,有功能可以拖动图像完全工作,但public class ZoomView extends View { images touchedCircle; private ScaleGestureDetector scaleDetector; private float scaleFactor = 1.f; private BitmapDrawable bitmap; private HashSet<images> img = new HashSet<images>(CIRCLES_LIMIT); private SparseArray<images> mCirclePointer = new SparseArray<images>(CIRCLES_LIMIT) public ZoomView(Context context) { this(context, null); } public ZoomView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Style.STROKE); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(5); mPaint.setStrokeCap(Paint.Cap.ROUND); bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.deleteforever); bitmap = (BitmapDrawable) getResources().getDrawable(R.drawable.mesej); scaleDetector = new ScaleGestureDetector(context, new ScaleListener()); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (images img1 : img) { canvas.save(); canvas.scale(scaleFactor, scaleFactor); canvas.drawBitmap(bitmap.getBitmap(), img1.centerX, img1.centerY, null); canvas.restore(); } } @Override public boolean onTouchEvent(final MotionEvent event) { boolean handled = false; scaleDetector.onTouchEvent(event); images touchedCircle; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: xTouch = event.getX(0); yTouch = event.getY(0); touchedCircle = obtainTouchedCircle(xTouch, yTouch); touchedCircle.centerX = xTouch; touchedCircle.centerY = yTouch; touchedtext = obtainTouchedText(xTouch2, yTouch2); mCirclePointer.put(event.getPointerId(0), touchedCircle); invalidate(); handled = true; break; case MotionEvent.ACTION_MOVE: touchedCircle = mCirclePointer.get(pointerId); if (!scaleDetector.isInProgress()){ if (null != touchedCircle) { touchedCircle.centerX = xTouch - bitmap.getBitmap().getWidth() / 2; touchedCircle.centerY = yTouch - bitmap.getBitmap().getHeight() / 2; invalidate(); } } invalidate(); handled = true; break; case MotionEvent.ACTION_UP: invalidate(); handled = true; break; case MotionEvent.ACTION_POINTER_UP: mCirclePointer.remove(pointerId); invalidate(); handled = true; break; case MotionEvent.ACTION_CANCEL: zooming=false; invalidate(); handled = true; break; default: break; } invalidate(); return super.onTouchEvent(event) || handled; } private static class images { int radius; float centerX; float centerY; images(float X, float Y) { this.centerX = X; this.centerY = Y; this.radius = radius; } private images obtainTouchedCircle(final float xTouch, final float yTouch) { images touchedCircle = getTouchedCircle(xTouch, yTouch); if (null == touchedCircle) { touchedCircle = new images(xTouch, yTouch); if (img.size() == CIRCLES_LIMIT) { img.clear(); } if (c.getImage() == 1) { img.add(touchedCircle); } } return touchedCircle; } private images getTouchedCircle(final float xTouch, final float yTouch) { images touched = null; for (images circle : img) { if ((circle.centerX < xTouch) && (circle.centerY < yTouch)) { float bitmap_width =bitmap.getBitmap().getWidth(); float bitmap_height = bitmap.getBitmap().getHeight(); float width = circle.centerX + bitmap_width; float height = circle.centerY + bitmap_height; if ((xTouch < circle.centerX + bitmap.getBitmap().getWidth()) && (yTouch < circle.centerY + bitmap.getBitmap().getHeight())) { touched = circle; break; } } } return touched; } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { scaleFactor *= detector.getScaleFactor(); scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 3.0f)); invalidate(); return true; } } 缩放它或工作。

这是代码

for model in simmodels:
   result = simulate(model)
   speed = result["speed"]
   distance = result["distance"]

}

感谢您提前帮助......

1 个答案:

答案 0 :(得分:1)

您为所有图片使用单一比例因子。您需要保留一系列比例因子,每个图像一个。然后在缩放时在onDraw中,为该图像选择正确的比例因子。