如何从c#中的ImageView获取图像大小?

时间:2013-10-28 10:33:28

标签: c# android imageview size

我在c#中为我的Android应用程序制作了可缩放的ImageView。我可以捏缩放图像并移动它,但我可以将它移出屏幕。我想阻止这个。我需要显示的位图大小来做到这一点。 如何从c#中的ImagView显示的位图中获取位图大小? 这是可缩放的ImageView的代码:

    class NewZoomableImageView :ImageView
{
    private static int INVALID_POINTER_ID = -1;

  private float mPosX;
  private float mPosY;

  private float mLastTouchX;
  private float mLastTouchY;

  private float mLastGestureX;
  private float mLastGestureY;
  private int mActivePointerId = INVALID_POINTER_ID;

  private ScaleGestureDetector mScaleDetector;
  private static float mScaleFactor = 1.0f;

    public NewZoomableImageView(Context context, Bitmap bitmap):base(context)
  {
    mScaleDetector = new ScaleGestureDetector(Context,new ScaleListener());
  }

  public override bool OnTouchEvent (MotionEvent e)
  {
    mScaleDetector.OnTouchEvent (e);

    //int action = e.Action;
    switch (e.Action & MotionEventActions.Mask) {
      case MotionEventActions.Down:
      if (!mScaleDetector.IsInProgress) {
        float x = e.GetX ();
        float y = e.GetY ();

        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = e.GetPointerId (0);
      }
      break;
      case MotionEventActions.Pointer1Down:
      if (mScaleDetector.IsInProgress) {
        float gx = mScaleDetector.FocusX;
        float gy = mScaleDetector.FocusY;

        mLastGestureX = gx;
        mLastGestureY = gy;
      }
      break;
      case MotionEventActions.Move:
      if (!mScaleDetector.IsInProgress) {
        int pointerIdx = e.FindPointerIndex (mActivePointerId);
        float x = e.GetX (pointerIdx);
        float y = e.GetY (pointerIdx);

        float dx = x - mLastTouchX;
        float dy = y - mLastTouchY;

          float oldXPos = mPosX;
          float oldYPos = mPosY;

        mPosX += dx;
        mPosY += dy;

        Invalidate ();

        mLastTouchX = x;
        mLastTouchY = y;

      } else {
        float gx = mScaleDetector.FocusX;
        float gy = mScaleDetector.FocusY;

        float gdx = gx - mLastGestureX;
        float gdy = gy - mLastGestureY;

        mPosX += gdx;
        mPosY += gdy;

        Invalidate ();

        mLastGestureX = gx;
        mLastGestureY = gy;
      }
      break;
      case MotionEventActions.Up:
      mActivePointerId = INVALID_POINTER_ID;
      break;
      case MotionEventActions.Cancel:
      mActivePointerId = INVALID_POINTER_ID;
      break;
      case MotionEventActions.PointerUp:

      int pointerIdx2 = (int)(e.Action & MotionEventActions.PointerIndexMask) >> (int)MotionEventActions.PointerIndexShift;
      int pointerId = e.GetPointerId (pointerIdx2);

      if (pointerId == mActivePointerId) {
        int NewPointerIndex = pointerIdx2 == 0 ? 1 : 0;
          mLastTouchX = e.GetX (NewPointerIndex);
          mLastTouchY = e.GetY (NewPointerIndex);
        mActivePointerId = e.GetPointerId (NewPointerIndex);
      }
      else{
        int TempPointerIdx = e.FindPointerIndex(mActivePointerId);
        mLastTouchX = e.GetX(TempPointerIdx);
        mLastTouchY = e.GetY(TempPointerIdx);
      }
      break;
    }

    return true;
  }

  protected override void OnDraw (Canvas canvas)
  {
    canvas.Save ();

    canvas.Translate (mPosX, mPosY);
    if (mScaleDetector.IsInProgress) {
      canvas.Scale (mScaleFactor, mScaleFactor, mScaleDetector.FocusX, mScaleDetector.FocusY);
    } else {
      canvas.Scale (mScaleFactor, mScaleFactor,mLastGestureX,mLastGestureY);
    }
    base.OnDraw (canvas);
    canvas.Restore();
  }


  private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
  {
    public override bool OnScale (ScaleGestureDetector detector)
    {
      mScaleFactor *= detector.ScaleFactor;

      mScaleFactor = Math.Max(0.1f, Math.Min(mScaleFactor, 10.0f));

      return true;
    }

  }
  }

1 个答案:

答案 0 :(得分:1)

1)获取BitmapDrawable,例如:

BitmapDrawable bd = (BitmapDrawable)imageView.Background;

2)使用“位图”属性

int width = bd.Bitmap.Width;
int height = bd.Bitmap.Height;
int image = bd.image.len(width);