2016-09-01 10 views
2

アンドロイドキャンバスに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; 
    } 
} 

}事前にヘルプ

おかげで...

答えて

1

です。それぞれの画像に対して1つのスケールファクタの配列を保持する必要があります。次に、スケールしたときのonDrawで、その画像の正しい倍率を選択します。

+0

いくつかのコードを教えてもらえますか? –

関連する問題