如何使用pinch放大Xamarin Android?

如何使用pinch放大Xamarin Android?,android,xamarin,surfaceview,pinchzoom,Android,Xamarin,Surfaceview,Pinchzoom,如何使用“收缩”在SurfaceView中进行缩放?我正在用C#学习Xamarin/Android 这是我的密码: namespace test_surfaceView { class SurfaceViewActivity : SurfaceView, ISurfaceHolderCallback, SurfaceView.IOnTouchListener { //private ImageViewActivity imageViewActivity;

如何使用“收缩”在SurfaceView中进行缩放?我正在用C#学习Xamarin/Android

这是我的密码:

namespace test_surfaceView
{
    class SurfaceViewActivity : SurfaceView, ISurfaceHolderCallback, SurfaceView.IOnTouchListener
    {
        //private ImageViewActivity imageViewActivity;

        private ScaleGestureDetector mScaleDetector;
        private ScaleListener sListener;
        private static float mScaleFactor = 0.6F;
        private static float scalePointX;
        private static float scalePointY;

        private static int INVALID_POINTER_ID = -1;
        private int mActivePointerId = INVALID_POINTER_ID;
        private static float mPosX, mPosY, mLastTouchX, mLastTouchY;

        private Android.Graphics.Bitmap BitmapImage;
        private Android.Graphics.Bitmap NEwBitmap;

        private Canvas canvasS = new Canvas();

        ISurfaceHolder surfaceHolder;

        private Context mContext;
        private Paint paint;
        private byte[] byteArray;
        private BitmapFactory.Options bmOptions;


        public SurfaceViewActivity(Context context)
            : base(context)
        {
            // TODO: Complete member initialization
            Log.Debug("SurfaceMessage", "SurfaceViewActivity");
            Log.Debug("mScaleFactor", "" + mScaleFactor);
            Log.Debug("mScaleFactor", "" + mScaleFactor);
            Log.Debug("scalePointX", "" + scalePointX);
            Log.Debug("scalePointY", "" + scalePointY);

            //SurfaceView surfaceView = FindViewById<SurfaceView>(Resource.Id.ImageView);



            mContext = context;
            surfaceHolder = Holder;
            surfaceHolder.AddCallback(this);
        }

        public void SurfaceCreated(ISurfaceHolder holder)
        {
            Log.Debug("SurfaceMessage", "SurfaceCreated");
            //SetOnTouchListener(this);
            try
            {
                Thread dkdk = new Thread(new ThreadStart(run));
                dkdk.Start();
            }
            catch (System.Exception ex)
            {
                Log.Debug("MESSAGE", "THREAD START FAIL");
            }
        }

        private void run()
        {

            try
            {
                canvasS = surfaceHolder.LockCanvas(null);
                doDraw(canvasS);
            }
            catch (System.Exception ex)
            {

            }
            finally
            {
                if (canvasS != null)
                {
                    surfaceHolder.UnlockCanvasAndPost(canvasS);
                }
            }

        }

        private void doDraw(Canvas canvas)
        {

            canvas.Scale(mScaleFactor, mScaleFactor, scalePointX, scalePointY);
            canvas.Translate(mPosX, mPosY);
            Paint paint = new Paint();
            paint.AntiAlias = true;
            byteArray = Base64.Decode(ImagePath.BitmapPath, Base64.Default);
            bmOptions = new BitmapFactory.Options();
            bmOptions.InSampleSize = 1;
            bmOptions.InJustDecodeBounds = false;
            bmOptions.InPurgeable = true;

            BitmapImage = BitmapFactory.DecodeByteArray(byteArray, 0, byteArray.Length, bmOptions);
            //Bitmap dkdk = new Bitmap();
            NEwBitmap = Bitmap.CreateScaledBitmap(BitmapImage, 1300, 1900, true);

            canvas.DrawBitmap(NEwBitmap, 0, 0, paint);

            sListener = new ScaleListener();
            mScaleDetector = new ScaleGestureDetector(mContext, sListener);
            mScaleFactor = 500F / (float)Math.Min(Resources.DisplayMetrics.WidthPixels, Resources.DisplayMetrics.HeightPixels);

            this.SetOnTouchListener(this);
        }



        public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int width, int height)
        {
            Log.Debug("SurfaceMessage", "SurfaceChanged");
            Log.Debug("SurfaceMessage", "width" + width);
            Log.Debug("SurfaceMessage", "height" + height);
        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            Log.Debug("SurfaceMessage", "SurfaceDestroyed");
            BitmapImage = null;
        }



        private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
        {
            public override bool OnScale(ScaleGestureDetector detector)
            {
                scalePointX = detector.FocusX;
                scalePointY = detector.FocusY;

                mScaleFactor *= detector.ScaleFactor;
                mScaleFactor = Math.Max(0.5f, Math.Min(mScaleFactor, 7.0f));

                return true;
            }
        }


        public bool OnTouch(View v, MotionEvent e)
        {
            mScaleDetector.OnTouchEvent(e);

            switch (e.Action & MotionEventActions.Mask)
            {
                case MotionEventActions.Down:
                    Log.Debug("MotionEventActions", "Down");
                    float x = (e.GetX() - scalePointX) / mScaleFactor;
                    float y = (e.GetY() - scalePointY) / mScaleFactor;

                    mLastTouchX = x;
                    mLastTouchY = y;

                    mActivePointerId = e.GetPointerId(0);
                    break;

                case MotionEventActions.Move:
                    Log.Debug("MotionEventActions", "Move");
                    int pointerIndex = e.FindPointerIndex(mActivePointerId);

                    float x2 = (e.GetX(pointerIndex) - scalePointX) / mScaleFactor;
                    float y2 = (e.GetY(pointerIndex) - scalePointY) / mScaleFactor;

                    float dx = (x2 - mLastTouchX);
                    float dy = (y2 - mLastTouchY);

                    if (!mScaleDetector.IsInProgress)
                    {
                        mPosX += dx;
                        mPosY += dy;

                        mLastTouchX = x2;
                        mLastTouchY = y2;
                    }
                    //this.Invalidate();
                    //PostInvalidate();

                    break;

                case MotionEventActions.Up:
                    Log.Debug("MotionEventActions", "Up");
                    mActivePointerId = INVALID_POINTER_ID;
                    break;

                case MotionEventActions.Cancel:
                    Log.Debug("MotionEventActions", "Cancel");
                    mActivePointerId = INVALID_POINTER_ID;
                    break;

                case MotionEventActions.PointerUp:
                    Log.Debug("MotionEventActions", "PointerUp");
                    int pointerIndex2 = (int)(e.Action & MotionEventActions.PointerIndexMask) >> (int)MotionEventActions.PointerIndexShift;
                    int pointerID = e.GetPointerId(pointerIndex2);

                    if (pointerID == mActivePointerId)
                    {
                        int newPointerIndex = pointerIndex2 == 0 ? 1 : 0;

                        mLastTouchX = (e.GetX(newPointerIndex) - scalePointX) / mScaleFactor;
                        mLastTouchY = (e.GetY(newPointerIndex) - scalePointY) / mScaleFactor;

                        mActivePointerId = e.GetPointerId(newPointerIndex);
                    }

                    break;
            }

            return true;
        }
    }
}
名称空间测试\u surfaceView
{
类SurfaceView活动:SurfaceView、ISurfaceHolderCallback、SurfaceView.IOnTouchListener
{
//私有ImageViewActivity ImageViewActivity;
专用scalegestruedetector mScaleDetector;
私人斯莱斯腾纳;
专用静态浮动mScaleFactor=0.6F;
私有静态浮点scalePointX;
私有静态浮动缩放点;
私有静态int无效\u指针\u ID=-1;
private int mActivePointerId=无效的\u指针\u ID;
专用静态浮点mPosX、mPosY、mLastTouchX、mLastTouchY;
私有Android.Graphics.Bitmap位图位图;
私有Android.Graphics.Bitmap NEwBitmap;
私有画布画布=新画布();
ISurfaceHolder表面支架;
私有上下文;
私人油漆;
专用字节[]字节数组;
私有位图工厂。选项bmOptions;
公共表面活动(上下文)
:基本(上下文)
{
//TODO:完成成员初始化
Log.Debug(“SurfaceMessage”、“SurfaceDeviceActivity”);
Log.Debug(“mScaleFactor”和“+mScaleFactor”);
Log.Debug(“mScaleFactor”和“+mScaleFactor”);
Log.Debug(“scalePointX”、“scalePointX+scalePointX”);
Log.Debug(“scalePointY”和“+scalePointY”);
//SurfaceView SurfaceView=findviewbyd(Resource.Id.ImageView);
mContext=上下文;
表面层=支架;
surfaceHolder.AddCallback(this);
}
已处理的公共空隙表面(ISurfaceHolder)
{
Log.Debug(“SurfaceMessage”、“SurfaceCreated”);
//SetOnTouchListener(这个);
尝试
{
线程dkdk=新线程(新线程开始(运行));
dk.Start();
}
catch(System.Exception-ex)
{
调试(“消息”,“线程启动失败”);
}
}
私家车
{
尝试
{
canvasS=surfaceHolder.LockCanvas(null);
多德劳(拉票);
}
catch(System.Exception-ex)
{
}
最后
{
if(canvas!=null)
{
surfaceHolder.UnlockCanvasAndPost(Canvas);
}
}
}
私有void doDraw(画布)
{
Scale(mScaleFactor、mScaleFactor、scalePointX、scalePointY);
canvas.Translate(mPosX,mPosY);
油漆=新油漆();
paint.AntiAlias=true;
byteArray=Base64.Decode(ImagePath.BitmapPath,Base64.Default);
bmOptions=新的BitmapFactory.Options();
bmOptions.InSampleSize=1;
bmOptions.InJustDecodeBounds=false;
bmOptions.inpurgable=true;
BitmapImage=BitmapFactory.DecodeByteArray(byteArray,0,byteArray.Length,bmOptions);
//位图DK=新位图();
NEwBitmap=Bitmap.CreateScaledBitmap(BitmapImage,1300,1900,true);
画布.DrawBitmap(NEwBitmap,0,0,paint);
sListener=新的ScaleListener();
mScaleDetector=新的scalegestruedetector(mContext,sListener);
mScaleFactor=500F/(float)Math.Min(Resources.DisplayMetrics.WidthPixels,Resources.DisplayMetrics.HeightPixels);
this.SetOnTouchListener(this);
}
public void surfacechange(ISurfaceHolder,Android.Graphics.Format格式,int-width,int-height)
{
Log.Debug(“SurfaceMessage”、“SurfaceChanged”);
Log.Debug(“SurfaceMessage”,“width”+width);
Log.Debug(“表面消息”、“高度”+高度);
}
公共空隙表面置换(ISurfaceHolder)
{
Log.Debug(“SurfaceMessage”、“SurfaceDestroyed”);
BitmapImage=null;
}
私有类ScaleListener:scalegstruedetector.SimpleOnScalegstrueListener
{
公共超控布尔放大(ScaleGetureDetector检测器)
{
scalePointX=detector.FocusX;
scalePointY=检测器.FocusY;
mScaleFactor*=检测器.ScaleFactor;
mScaleFactor=数学最大值(0.5f,数学最小值(mScaleFactor,7.0f));
返回true;
}
}
公共bool OnTouch(视图v,运动事件e)
{
MSCale探测器。通气孔(e);
开关(如Action和MotionEventActions.Mask)
{
案例MotionEventActions.Down:
调试(“MotionEventActions”,“Down”);
浮点x=(e.GetX()-scalePointX)/mScaleFactor;
浮点y=(e.GetY()-scalePointY)/mScaleFactor;
mLastTouchX=x;
mLastTouchY=y;
MacTivePointId=e.GetPointerId(0);
打破
case MotionEventActions。移动:
调试(“MotionEventActions”、“Move”);
int pointerIndex=e.FindPointerIndex(mactivePointId);
浮点x2=(e.GetX(pointerIndex)-scalePointX)/mScaleFactor;
浮点y2=(e.GetY(pointerIndex)-scalePointY)/mScaleFactor;
浮点dx=(x2-mLastTouchX);
浮动dy=(y2-最大值);
如果(!mScaleDetector.IsInProgress)
{
mPosX+=dx;
mPosY+=dy;
mLastTouchX=x2;
mLastTouchY=y2;
}
//这个。使无效();