【问题标题】:How to zoom in and Zoom out bitmap in android canvas from multiple bitmap?如何从多个位图放大和缩小 android 画布中的位图?
【发布时间】:2016-09-01 11:47:53
【问题描述】:

我在 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;
    }
}

}

提前感谢您的帮助...

【问题讨论】:

    标签: java android canvas bitmap


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多