我已经在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;
}
}
}
您对所有图像使用单个比例因子。您需要保存一个比例因子数组,每个图像对应一个比例因子。然后在你的onDraw中,当你缩放时,为该图像选择正确的比例因子。