using System; namespace Shared.Phone.UserCenter { /// /// 做成一个头像控件,可以切换头像 /// public class ProfilePhotoControl:ButtonCommon { /// /// 做成一个头像控件,可以切换头像 /// /// 不允许编辑头像 public ProfilePhotoControl(bool isCanEditor = false) { int iconSize = Application.GetRealHeight(180); this.Width = iconSize; this.Height = iconSize; this.Radius = (uint)iconSize / 2; if (isCanEditor == true) { this.MouseUpEventHandler += (sender, e) => { //显示用户图标选择菜单 this.ShowUserIconSelectMenu(); }; } } /// /// 显示用户图标选择菜单 /// private void ShowUserIconSelectMenu() { //Dialog dialog = new Dialog(); //FrameLayout frameLayout = new FrameLayout(); //frameLayout.MouseUpEventHandler += (sender, e) => //{ // dialog.Close(); //}; //dialog.AddChidren(frameLayout); ////设定控件的高度 //int height = Application.GetRealHeight(100); ////设定控件左右与界面的间距 //int space = Application.GetRealHeight(30); ////设定控件的宽度 //int width = frameLayout.Width - space * 2; ////取消 //Button btnCancel = UserCenterControls.MakeViewButton(width, height); ////btnCancel.TextID = R.MyInternationalizationString.Cancel; //btnCancel.Gravity = Gravity.BottomCenter; //btnCancel.TextColor = UserCenterColor.Current.Red; //btnCancel.BorderWidth = 2; //btnCancel.BackgroundColor = UserCenterColor.Current.White; //btnCancel.BorderColor = UserCenterColor.Current.Gray; //btnCancel.Radius = 5; //btnCancel.MouseUpEventHandler += (sender, e) => //{ // dialog.Close(); //}; //frameLayout.AddChidren(btnCancel); ////相册 //Button btnAlbum = UserCenterControls.MakeViewButton(width, height); //btnAlbum.Text = "从相册中选择"; //btnAlbum.Gravity = Gravity.CenterHorizontal; //btnAlbum.TextColor = UserCenterColor.Current.SelectTextColor; //btnAlbum.BorderWidth = 2; //btnAlbum.BorderColor = UserCenterColor.Current.Gray; //btnAlbum.BackgroundColor = UserCenterColor.Current.White; //btnAlbum.Radius = 5; //btnAlbum.Y = btnCancel.Bottom - height * 2 - Application.GetRealHeight(10); //btnAlbum.MouseUpEventHandler += (sender, e) => //{ // Camera.SelectPicture(this.EditorPictrue, "MyIconPic.png"); // dialog.Close(); //}; //frameLayout.AddChidren(btnAlbum); ////拍照 //Button btnCamera = UserCenterControls.MakeViewButton(width, height); //btnCamera.Text = "拍照"; //btnCamera.Gravity = Gravity.CenterHorizontal; //btnCamera.TextColor = UserCenterColor.Current.SelectTextColor; //btnCamera.BorderWidth = 2; //btnCamera.BorderColor = UserCenterColor.Current.Gray; //btnCamera.BackgroundColor = UserCenterColor.Current.White; //btnCamera.Y = btnAlbum.Bottom - height * 2 + 2; //btnCamera.Radius = 5; //btnCamera.MouseUpEventHandler += (sender, e) => //{ // Camera.TakePicture((filename) => // { } // , "Picture"); // dialog.Close(); //}; //frameLayout.AddChidren(btnCamera); //dialog.Show(); } /// /// 图片编辑 /// /// File name. private void EditorPictrue(string fileName) { //Dialog dialog = new Dialog(); //FrameLayout frameLayout = new FrameLayout(); //frameLayout.MouseUpEventHandler += (sender, e) => //{ // if (e.X <= 50) // { // dialog.Close(); // } //}; //dialog.AddChidren(frameLayout); //ClipView clipView = new ClipView(); //clipView.Height = Application.GetRealHeight(600); //clipView.Gravity = Gravity.CenterVertical; //Bitmap bitmap = BitmapFactory.DecodeFile(fileName); //clipView.imageView.SetAdjustViewBounds(true); //clipView.imageView.SetScaleType(Android.Widget.ImageView.ScaleType.CenterCrop); //clipView.imageView.SetImageBitmap(bitmap); //frameLayout.AddChidren(clipView); //CircleView circleView = new CircleView(); //circleView.Height = Application.GetRealHeight(600); //circleView.Gravity = Gravity.CenterVertical; //frameLayout.AddChidren(circleView); //dialog.Show(); //ScaleGestureListener listener = new ScaleGestureListener(Application.Activity); //listener.init(clipView.imageView); } ///// ///// Circle view. ///// //public class CircleView : Shared.View //{ // public Android.Widget.ImageView imageView // { // get { return base.AndroidView as Android.Widget.ImageView; } // set { base.AndroidView = value; } // } // /// // /// Initializes a new instance of the class. // /// // public CircleView() // { // ImageViewEx view = new ImageViewEx(Application.Activity); // this.imageView = view; // } //} ///// ///// Android image view ex. ///// //public class ImageViewEx : Android.Widget.ImageView //{ // /// // /// Initializes a new instance of the class. // /// // /// Context. // public ImageViewEx(Android.Content.Context context) : base(context) // { // } // /// // /// Ons the draw. // /// // /// Canvas. // protected override void OnDraw(Canvas canvas) // { // base.OnDraw(canvas); // drawMask(canvas); // } // /// // /// 绘制蒙版 // /// // /// Canvas. // private void drawMask(Canvas canvas) // { // //画背景颜色 // Bitmap bitmap = Bitmap.CreateBitmap(this.Width, this.Height, Bitmap.Config.Argb8888); // Canvas c1 = new Canvas(bitmap); // c1.DrawARGB(150, 0, 0, 0); // Paint strokePaint = new Paint(); // strokePaint.AntiAlias = true; // strokePaint.Color = Android.Graphics.Color.White; // strokePaint.SetStyle(Paint.Style.Stroke); // c1.DrawCircle(this.Width / 2, this.Height / 2, this.Height / 2, strokePaint); // //画圆 // Bitmap circleBitmap = Bitmap.CreateBitmap(this.Width, this.Height, Bitmap.Config.Argb8888); // Canvas c2 = new Canvas(circleBitmap); // Paint circlePaint = new Paint(); // circlePaint.SetStyle(Paint.Style.Fill); // circlePaint.Color = Android.Graphics.Color.Red; // circlePaint.AntiAlias = true; // c2.DrawCircle(this.Width / 2, this.Height / 2, this.Height / 2, circlePaint); // //两个图层合成 // Paint paint = new Paint(); // paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.DstOut)); // c1.DrawBitmap(circleBitmap, 0, 0, paint); // paint.SetXfermode(null); // canvas.DrawBitmap(bitmap, 0, 0, null); // } //} ///// ///// Clip view. ///// //public class ClipView : Shared.View //{ // public Android.Widget.ImageView imageView // { // get { return base.AndroidView as Android.Widget.ImageView; } // set { base.AndroidView = value; } // } // /// // /// Initializes a new instance of the class. // /// // public ClipView() // { // ImageViewEx2 view = new ImageViewEx2(Application.Activity); // this.imageView = view; // } //} ///// ///// Image view ex2. ///// //public class ImageViewEx2 : Android.Widget.ImageView //{ // /// // /// Initializes a new instance of the class. // /// // /// Context. // public ImageViewEx2(Android.Content.Context context) : base(context) // { // } // /// // /// Ons the touch event. // /// // /// true, if touch event was oned, false otherwise. // /// E. // public override bool OnTouchEvent(MotionEvent e) // { // this.AdjustMotionEvent(e); // return base.OnTouchEvent(e); // } // PointF startPoint = new PointF(); // //当前状态 // private int mMode = Mode.NONE; // /// // /// 状态 // /// // private class Mode // { // //初始状态 // public const int NONE = 0; // //托动 // public const int DRAG = 1; // //缩放 // public const int ZOOM = 2; // } // private void AdjustMotionEvent(MotionEvent e) // { // if (e.Action == MotionEventActions.Down) // { // mMode = Mode.DRAG; // startPoint.Set(e.GetX(), e.GetY()); // } // else if (e.Action == MotionEventActions.Up) // { // mMode = Mode.NONE; // } // else if (e.Action == MotionEventActions.Move) // { // if (mMode == Mode.DRAG && e.PointerCount == 1) // { // float x = e.GetX(); // float y = e.GetY(); // float dx = e.GetX() - startPoint.X; // float dy = e.GetY() - startPoint.Y; // this.SetX(this.GetX() + dx); // this.SetY(this.GetY() + dy); // //刷新起点坐标 // startPoint.Set(x, y); // } // } // } //} ///// ///// ScaleGestureListener ///// //public class ScaleGestureListener :Android.Widget.FrameLayout, //ScaleGestureDetector.IOnScaleGestureListener //{ // //最大缩放比例 // private float MAX_SCALE = 4.0f; // //最小缩放比例 // private float MIN_SCALE = 1.0f; // //matrix array // private float[] MATRIX_ARR = new float[9]; // /// // /// 状态 // /// // private class Mode // { // // 初始状态 // public const int NONE = 0; // //托动 // public const int DRAG = 1; // //缩放 // public const int ZOOM = 2; // } // //当前状态 // private int mMode = Mode.NONE; // //缩放手势 // private Android.Views.ScaleGestureDetector mScaleDetector; // //矩阵 // private Matrix mMatrix = new Matrix(); // //托动时手指按下的点 // private PointF mPrevPointF = new PointF(); // //第一次 // private bool firstTime = true; // Android.Widget.ImageView imageView = null; // /// // /// ScaleGestureListener // /// // public ScaleGestureListener(Android.Content.Context context) : base(context) // { // } // /// // /// 初始化 // /// // public void init(Android.Widget.ImageView i_View) // { // imageView = i_View; // mScaleDetector = new ScaleGestureDetector(imageView.Context, this); // initPosAndScale(); // } // /// // /// 初始化缩放比例 // /// // private void initPosAndScale() // { // if (firstTime) // { // Android.Graphics.Drawables.Drawable drawable = imageView.Drawable; // int width = imageView.Width; // int height = imageView.Height; // //初始化 // int dw = drawable.IntrinsicWidth; // int dh = drawable.IntrinsicHeight; // float scaleX = 1.0f; // float scaleY = 1.0f; // //是否已经做过缩放处理 // bool isScaled = false; // if (width < getDiameter()) // { // scaleX = getDiameter() * 1.0f / width; // isScaled = true; // } // if (height < getDiameter()) // { // scaleY = getDiameter() * 1.0f / height; // isScaled = true; // } // float scale = Math.Max(scaleX, scaleY); // if (isScaled) // { // MIN_SCALE = scale; // } // else // { // MIN_SCALE = Math.Max((getDiameter() * 1.0f) / dw, getDiameter() * 1.0f / dh) + 0.01f; // } // mMatrix.PostScale(scale, scale, imageView.Width / 2, imageView.Height / 2); // mMatrix.PostTranslate((width - dw) / 2, (height - dh) / 2); // imageView.ImageMatrix = mMatrix; // firstTime = false; // } // } // /// // /// Ons the touch event. // /// // /// true, if touch event was oned, false otherwise. // /// E. // private bool OnTouchEvent2(MotionEvent e) // { // if (imageView.Drawable == null) // { // return false; // } // mScaleDetector.OnTouchEvent(e); // switch (e.Action) // { // case MotionEventActions.Down: // { // mMode = Mode.DRAG; // mPrevPointF.Set(e.GetX(), e.GetY()); // break; // } // case MotionEventActions.Up: // { // mMode = Mode.NONE; // break; // } // case MotionEventActions.Move: // { // if (mMode == Mode.DRAG && e.PointerCount == 1) // { // float x = e.GetX(); // float y = e.GetY(); // float dx = e.GetX() - mPrevPointF.X; // float dy = e.GetY() - mPrevPointF.Y; // RectF rectF = getMatrixRectF(); // // 如果宽度小于屏幕宽度,则禁止左右移动 // if (rectF.Width() <= getDiameter()) // { // dx = 0; // } // // 如果高度小雨屏幕高度,则禁止上下移动 // if (rectF.Height() <= getDiameter()) // { // dy = 0; // } // mMatrix.PostTranslate(dx, dy); // checkTrans(); // //边界判断 // imageView.ImageMatrix = mMatrix; // mPrevPointF.Set(x, y); // } // break; // } // } // return true; // } // /// // /// Ons the scale. // /// // /// true, if scale was oned, false otherwise. // /// Detector. // public bool OnScale(ScaleGestureDetector detector) // { // float scale = getScale(); // float scaleFactor = detector.ScaleFactor; // if ((scale >= MIN_SCALE && scaleFactor > 1.0f) || // (scale <= MAX_SCALE && scaleFactor < 1.0f)) // { // if (scale * scaleFactor <= MIN_SCALE) // { // scaleFactor = MIN_SCALE / scale; // } // else if (scale * scaleFactor >= MAX_SCALE) // { // scaleFactor = MAX_SCALE / scale; // } // mMatrix.PostScale(scaleFactor, scaleFactor, detector.FocusX, detector.FocusY); // checkTrans(); // imageView.ImageMatrix = mMatrix; // } // return true; // } // /// // /// Ons the scale begin. // /// // /// true, if scale begin was oned, false otherwise. // /// Detector. // public bool OnScaleBegin(ScaleGestureDetector detector) // { // mMode = Mode.ZOOM; // return true; // } // /// // /// Ons the scale end. // /// // /// Detector. // public void OnScaleEnd(ScaleGestureDetector detector) // { // mMode = Mode.NONE; // } // /// // /// 移动边界检查 // /// // private void checkTrans() // { // RectF rect = getMatrixRectF(); // float deltaX = 0; // float deltaY = 0; // int width = imageView.Width; // int height = imageView.Height; // int horizontalPadding = (width - getDiameter()) / 2; // int verticalPadding = (height - getDiameter()) / 2; // // 如果宽或高大于屏幕,则控制范围 ; 这里的0.001是因为精度丢失会产生问题 // if (rect.Width() + 0.01 >= getDiameter()) // { // if (rect.Left > horizontalPadding) // { // deltaX = -rect.Left + horizontalPadding; // } // if (rect.Right < width - horizontalPadding) // { // deltaX = width - horizontalPadding - rect.Right; // } // } // if (rect.Height() + 0.01 >= getDiameter()) // { // if (rect.Top > verticalPadding) // { // deltaY = -rect.Top + verticalPadding; // } // if (rect.Bottom < height - verticalPadding) // { // deltaY = height - verticalPadding - rect.Bottom; // } // } // mMatrix.PostTranslate(deltaX, deltaY); // } // /// // /// 得到直径 // /// // /// The diameter. // public int getDiameter() // { // return imageView.Height; // } // /// // /// 获得缩放值 // /// // /// The scale. // private float getScale() // { // return getMatrixValue(Matrix.MscaleX); // } // /// // /// Gets the matrix value. // /// // /// The matrix value. // /// Index. // private float getMatrixValue(int index) // { // mMatrix.GetValues(MATRIX_ARR); // return MATRIX_ARR[index]; // } // /// // /// 获得Matrix的RectF // /// // /// The matrix rect f. // private RectF getMatrixRectF() // { // Matrix matrix = mMatrix; // RectF rect = new RectF(); // if (null != imageView.Drawable) // { // rect.Set(0, 0, imageView.Drawable.IntrinsicWidth, imageView.Drawable.IntrinsicHeight); // matrix.MapRect(rect); // } // return rect; // } //} } }