public override void Draw (Canvas canvas)
		{
			var bounds = Bounds;

			if (alpha != 255) {
				paint.Alpha = 255;
				if (SecondBitmap != null) {
					if (shader1 == null)
						shader1 = new BitmapShader (FirstBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
					shader1.SetLocalMatrix (matrix);
					paint.SetShader (shader1);
					canvas.DrawRect (bounds, paint);
				} else
					canvas.DrawColor (defaultColor.ToAndroidColor());
			}
			if (alpha != 0) {
				paint.Alpha = alpha;
				if (FirstBitmap != null) {
					if (shader2 == null)
						shader2 = new BitmapShader (SecondBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
					shader2.SetLocalMatrix (matrix);
					paint.SetShader (shader2);
					canvas.DrawRect (bounds, paint);
				} else
					canvas.DrawColor (defaultColor.ToAndroidColor());
			}
		}
		protected override Bitmap Transform(IBitmapPool bitmapPool, Bitmap source, int outWidth, int outHeight)
		{
			int size = Math.Min(source.Width, source.Height);

			int width = (source.Width - size) / 2;
			int height = (source.Height - size) / 2;

			Bitmap squaredBitmap = Bitmap.CreateBitmap(source, width, height, size, size);
			if (squaredBitmap != source)
			{
				source.Recycle();
			}

			Bitmap bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb8888);

			Canvas canvas = new Canvas(bitmap);
			Paint paint = new Paint();
			BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.Clamp,
					BitmapShader.TileMode.Clamp);
			paint.SetShader(shader);
			paint.AntiAlias = true;

			float r = size / 2f;
			canvas.DrawCircle(r, r, r, paint);

			squaredBitmap.Recycle();

			return BitmapResource.Obtain(bitmap, bitmapPool).Get();
		}
		public static Bitmap ToRounded(Bitmap source, float rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor)
		{
			double sourceWidth = source.Width;
			double sourceHeight = source.Height;

			double desiredWidth = sourceWidth;
			double desiredHeight = sourceHeight;

			double desiredRatio = cropWidthRatio / cropHeightRatio;
			double currentRatio = sourceWidth / sourceHeight;

			if (currentRatio > desiredRatio)
				desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
			else if (currentRatio < desiredRatio)
				desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);

			float cropX = (float)((sourceWidth - desiredWidth) / 2d);
			float cropY = (float)((sourceHeight - desiredHeight) / 2d);

			if (rad == 0)
				rad = (float)(Math.Min(desiredWidth, desiredHeight) / 2d);
			else
				rad = (float)(rad * (desiredWidth + desiredHeight) / 2d / 500d);

			Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888);

			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				if (cropX != 0 || cropY != 0)
				{
					matrix.SetTranslate(-cropX, -cropY);
					shader.SetLocalMatrix(matrix);
				}

				paint.SetShader(shader);
				paint.AntiAlias = true;

				RectF rectF = new RectF(0f, 0f, (float)desiredWidth, (float)desiredHeight);
				canvas.DrawRoundRect(rectF, rad, rad, paint);

				if (borderSize > 0d) 
				{
					borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 500d);
					paint.Color = borderHexColor.ToColor(); ;
					paint.SetStyle(Paint.Style.Stroke);
					paint.StrokeWidth = (float)borderSize;
					paint.SetShader(null);

					RectF borderRectF = new RectF((float)(0d + borderSize/2d), (float)(0d + borderSize/2d), 
						(float)(desiredWidth - borderSize/2d), (float)(desiredHeight - borderSize/2d));

					canvas.DrawRoundRect(borderRectF, rad, rad, paint);
				}

				return bitmap;				
			}
		}
        public static Bitmap ToRounded(Bitmap source, float rad)
        {
            int size = Math.Min(source.Width, source.Height);

            int dx = (source.Width - size) / 2;
            int dy = (source.Height - size) / 2;

            Bitmap bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb8888);

            using (Canvas canvas = new Canvas(bitmap))
            using (Paint paint = new Paint())
            using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
            using (Matrix matrix = new Matrix())
            {
                if (dx != 0 || dy != 0)
                {
                    // source isn't square, move viewport to centre
                    matrix.SetTranslate(-dx, -dy);
                    shader.SetLocalMatrix(matrix);
                }
                paint.SetShader(shader);
                paint.AntiAlias = true;

                RectF rectF = new RectF(0, 0, size, size);
                canvas.DrawRoundRect(rectF, rad, rad, paint);

                return bitmap;
            }
        }
        public Bitmap Transform(Bitmap source)
        {
            int size = Math.Min(source.Width, source.Height);

            int width = (source.Width - size) / 2;
            int height = (source.Height - size) / 2;

            var bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb4444);

            var canvas = new Canvas(bitmap);
            var paint = new Paint();
            var shader =
                new BitmapShader(source, BitmapShader.TileMode.Clamp, BitmapShader.TileMode.Clamp);
            if (width != 0 || height != 0)
            {
                // source isn't square, move viewport to center
                var matrix = new Matrix();
                matrix.SetTranslate(-width, -height);
                shader.SetLocalMatrix(matrix);
            }
            paint.SetShader(shader);
            paint.AntiAlias = true;

            float r = size / 2f;
            canvas.DrawCircle(r, r, r, paint);

            source.Recycle();

            return bitmap;
        }
		public static Bitmap ToCropped(Bitmap source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio)
		{
			double sourceWidth = source.Width;
			double sourceHeight = source.Height;

			double desiredWidth = sourceWidth;
			double desiredHeight = sourceHeight;

			double desiredRatio = cropWidthRatio / cropHeightRatio;
			double currentRatio = sourceWidth / sourceHeight;

			if (currentRatio > desiredRatio)
				desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
			else if (currentRatio < desiredRatio)
				desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);

			xOffset = xOffset * desiredWidth;
			yOffset = yOffset * desiredHeight;

			desiredWidth =  desiredWidth / zoomFactor;
			desiredHeight = desiredHeight / zoomFactor;

			float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset);
			float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset);

			if (cropX < 0)
				cropX = 0;

			if (cropY < 0)
				cropY = 0;

			if (cropX + desiredWidth > sourceWidth)
				cropX = (float)(sourceWidth - desiredWidth);

			if (cropY + desiredHeight > sourceHeight)
				cropY = (float)(sourceHeight - desiredHeight);

			Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, source.GetConfig());

			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				if (cropX != 0 || cropY != 0)
				{
					matrix.SetTranslate(-cropX, -cropY);
					shader.SetLocalMatrix(matrix);
				}

				paint.SetShader(shader);
				paint.AntiAlias = false;

				RectF rectF = new RectF(0, 0, (int)desiredWidth, (int)desiredHeight);
				canvas.DrawRect(rectF, paint);

				return bitmap;				
			}
		}
 public CircleDrawable(Bitmap bmp)
 {
     this.bmp = bmp;
     this.bmpShader = new BitmapShader(bmp, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
     this.paint = new Paint() { AntiAlias = true };
     this.paint.SetShader(bmpShader);
     this.oval = new RectF();
 }
        public void Refresh()
        {
            LinkedList<DView> views = myEventHandler.GetAllView();
            Canvas c = null;
            try
            {
                if (surfaceHolder.Surface.IsValid)
                {
                    c = surfaceHolder.LockCanvas();

                    lock (surfaceHolder)
                    {
                        Paint paint = new Paint();
                        Paint paintShader = new Paint();
                        paint.Color = Color.Black;
                        c.DrawRect(0, 0
                            , this.Width, this.Height, paint);
                        foreach (var view in views)
                        {
                            if(view.ViewType==DViewType.Circle)
                            {
                                Color color=new Color(view.Pattern.Color);
                                color.A=255;
                                paint.Color = color;
                                c.DrawCircle(view.X, view.Y, view.Radius, paint);
                            }else
                            {
                                if (view.Pattern.PatternType == DPatternType.Image)
                                {
                                    BitmapShader shader = new BitmapShader(view.Pattern.Image, Shader.TileMode.Repeat, Shader.TileMode.Repeat);
                                    paintShader.SetStyle(Paint.Style.Fill);
                                    paintShader.SetShader(shader);
                                    c.DrawRect(view.X - view.Radius, view.Y - view.Radius
                                        , view.X + view.Radius, view.Y + view.Radius, paintShader);

                                }else
                                {
                                    Color color = new Color(view.Pattern.Color);
                                    color.A = 255;
                                    paint.Color = color;
                                    c.DrawRect(view.X - view.Radius, view.Y - view.Radius
                                        , view.X + view.Radius, view.Y + view.Radius, paint);
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Log.Error("MySurfaceView",ex.Message);
            }
            finally
            {
                if (c != null)
                    surfaceHolder.UnlockCanvasAndPost(c);
            }
        }
		public static Bitmap ToRotated(Bitmap source, double degrees, bool ccw, bool resize)
		{
			if (degrees == 0 || degrees % 360 == 0)
				return source;

			if (ccw)
				degrees = 360d - degrees;

			Bitmap bitmap = Bitmap.CreateBitmap(source.Width, source.Height, Bitmap.Config.Argb8888);
			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				paint.AntiAlias = true;
				paint.Dither = true;
				paint.FilterBitmap = true;

				float targetRotation = (float)degrees;
				float rotationPivotX = (float)source.Width / 2.0f;
				float rotationPivotY = (float)source.Height / 2.0f;
				float targetWidth = source.Width;
				float targetHeight = source.Height;

				if (resize && (degrees % 180 != 0))
				{
					double cosR = Math.Cos(DegreesToRadians(targetRotation));
					double sinR = Math.Sin(DegreesToRadians(targetRotation));

					// Recalculate dimensions after rotation around pivot point
					double x1T = rotationPivotX * (1.0 - cosR) + (rotationPivotY * sinR);
					double y1T = rotationPivotY * (1.0 - cosR) - (rotationPivotX * sinR);
					double x2T = x1T + (targetWidth * cosR);
					double y2T = y1T + (targetWidth * sinR);
					double x3T = x1T + (targetWidth * cosR) - (targetHeight * sinR);
					double y3T = y1T + (targetWidth * sinR) + (targetHeight * cosR);
					double x4T = x1T - (targetHeight * sinR);
					double y4T = y1T + (targetHeight * cosR);

					double maxX = Math.Max(x4T, Math.Max(x3T, Math.Max(x1T, x2T)));
					double minX = Math.Min(x4T, Math.Min(x3T, Math.Min(x1T, x2T)));
					double maxY = Math.Max(y4T, Math.Max(y3T, Math.Max(y1T, y2T)));
					double minY = Math.Min(y4T, Math.Min(y3T, Math.Min(y1T, y2T)));
					targetWidth = (int) Math.Floor(maxX - minX);
					targetHeight  = (int) Math.Floor(maxY - minY);

					float sx = (float)source.Width / targetWidth;
					float sy = (float)source.Height / targetHeight;

					matrix.SetScale(sx, sy, rotationPivotX, rotationPivotY);
				}

				matrix.PostRotate(targetRotation, rotationPivotX, rotationPivotY);
				canvas.DrawBitmap(source, matrix, paint);

				return bitmap;				
			}
		}
Exemple #10
0
        public VignetteDrawable(Bitmap bitmap, float cornerRadius = 5, int margin = 3, bool withEffect = true)
        {
            useGradientOverlay = withEffect;
            mCornerRadius = cornerRadius;

            bitmapShader = new BitmapShader (bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

            paint = new Paint () { AntiAlias = true };
            paint.SetShader (bitmapShader);

            margin = margin;
        }
		private void Init()
		{
			Drawable drawable = this.Drawable;
			bitmap = Bitmap.CreateScaledBitmap (((BitmapDrawable)drawable).Bitmap, this.Width, this.Height, false);

			BitmapShader shader;
			shader = new BitmapShader (bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

			// init paint
			paint = new Paint ();
			paint.AntiAlias = true;
			paint.SetShader (shader);
		}
        public RoundedImage(Bitmap bitmap)
        {
            mBitmap = bitmap;
            mRectF = new RectF();
            mPaint = new Paint();
            mPaint.AntiAlias = true;
            mPaint.Dither = true;
            BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
            mPaint.SetShader(shader);

            // NOTE: we assume bitmap is properly scaled to current density
            mBitmapWidth = mBitmap.Width;
            mBitmapHeight = mBitmap.Height;
        }
        public RoundedDrawable(Bitmap bitmap) : base()
        {
            mBitmap = bitmap;
            mRectF = new RectF();
            mPaint = new Paint();
            mPaint.AntiAlias = true;
            mPaint.Dither = true;

            BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
            mPaint.SetShader(shader);

            // Try to get it circular, even if the given bitmap isn't square
            mBitmapWidth = Math.Min( mBitmap.Width, mBitmap.Height);
            mBitmapHeight = Math.Min(mBitmap.Width, mBitmap.Height);
        }
		public static Bitmap ToRotatedBitmap(this Bitmap sourceBitmap, int rotationDegrees)
		{
			if (rotationDegrees == 0)
				return sourceBitmap;

			int width = sourceBitmap.Width;
			int height = sourceBitmap.Height;

			if (rotationDegrees == 90 || rotationDegrees == 270)
			{
				width = sourceBitmap.Height;
				height = sourceBitmap.Width;
			}

			Bitmap bitmap = Bitmap.CreateBitmap(width, height, sourceBitmap.GetConfig());
			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(sourceBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				// paint.AntiAlias = true;
				// paint.Dither = true;
				// paint.FilterBitmap = true;

				canvas.Save(Android.Graphics.SaveFlags.Matrix);

				if (rotationDegrees == 90)
					canvas.Rotate(rotationDegrees, width / 2, width / 2);
				else if (rotationDegrees == 270)
					canvas.Rotate(rotationDegrees, height / 2, height / 2);
				else
					canvas.Rotate(rotationDegrees, width / 2, height / 2);

				canvas.DrawBitmap(sourceBitmap, matrix, paint);
				canvas.Restore();
			}

			if (sourceBitmap != null && sourceBitmap.Handle != IntPtr.Zero && !sourceBitmap.IsRecycled)
			{
				sourceBitmap.Recycle();
				sourceBitmap.Dispose();
			}

			return bitmap;
		}
        public RoundCornerDrawable(Bitmap bitmap, float cornerRadius = 5, int margin = 3)
        {
            mCornerRadius = cornerRadius;

            bitmapShader = new BitmapShader (bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

            paint = new Paint () {
                AntiAlias = true,
            };
            paint.SetShader (bitmapShader);
            strokePaint = new Paint () {
                Color = Color.Rgb (200, 200, 200),
                StrokeWidth = 2,
            };
            strokePaint.SetStyle (Paint.Style.Stroke);

            this.margin = margin;
        }
        public static Bitmap ToRounded(Bitmap source, float rad, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth = source.Width;
            double sourceHeight = source.Height;

            double desiredWidth = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            else if (currentRatio < desiredRatio)
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);

            float cropX = (float)((sourceWidth - desiredWidth) / 2);
            float cropY = (float)((sourceHeight - desiredHeight) / 2);

            if (rad == 0)
                rad = (float)(Math.Min(desiredWidth, desiredHeight) / 2);

            Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888);

            using (Canvas canvas = new Canvas(bitmap))
            using (Paint paint = new Paint())
            using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
            using (Matrix matrix = new Matrix())
            {
                if (cropX != 0 || cropY != 0)
                {
                    matrix.SetTranslate(-cropX, -cropY);
                    shader.SetLocalMatrix(matrix);
                }

                paint.SetShader(shader);
                paint.AntiAlias = true;

                RectF rectF = new RectF(0, 0, (int)desiredWidth, (int)desiredHeight);
                canvas.DrawRoundRect(rectF, rad, rad, paint);

                return bitmap;
            }
        }
		public VignetteDrawable (Bitmap bitmap, float cornerRadius = 5, int margin = 3, bool withEffect = true)
		{
			useGradientOverlay = withEffect;
			mCornerRadius = cornerRadius;

			bitmapShader = new BitmapShader (bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

			paint = new Paint () {
				AntiAlias = true,
			};
			paint.SetShader (bitmapShader);
			strokePaint = new Paint () {
				Color = Color.Rgb (200, 200, 200),
				StrokeWidth = 2,
			};
			strokePaint.SetStyle (Paint.Style.FillAndStroke);

			this.margin = margin;
		}
        public RoundedDrawable(Bitmap bitmap)
        {
            _mBitmapWidth = bitmap.Width;
            _mBitmapHeight = bitmap.Height;
            _mBitmapRect.Set(0, 0, _mBitmapWidth, _mBitmapHeight);

            _mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
            _mBitmapShader.SetLocalMatrix(_mShaderMatrix);

            _mBitmapPaint = new Paint();
            _mBitmapPaint.SetStyle(Paint.Style.Fill);
            _mBitmapPaint.AntiAlias = (true);
            _mBitmapPaint.SetShader(_mBitmapShader);

            _mBorderPaint = new Paint();
            _mBorderPaint.SetStyle(Paint.Style.Stroke);
            _mBorderPaint.AntiAlias = (true);
            _mBorderPaint.Color = Color.Black;
            _mBorderPaint.StrokeWidth = (_mBorderWidth);
        }
		public static Bitmap ToCropped(Bitmap source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio)
		{
			double sourceWidth = source.Width;
			double sourceHeight = source.Height;

			double desiredWidth = sourceWidth;
			double desiredHeight = sourceHeight;

			double desiredRatio = cropWidthRatio / cropHeightRatio;
			double currentRatio = sourceWidth / sourceHeight;

			if (currentRatio > desiredRatio)
				desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
			else if (currentRatio < desiredRatio)
				desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);

			desiredWidth = zoomFactor * desiredWidth;
			desiredHeight = zoomFactor * desiredHeight;

			float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset);
			float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset);

			Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888);

			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				matrix.SetTranslate(-cropX, -cropY);
				shader.SetLocalMatrix(matrix);

				paint.SetShader(shader);
				paint.AntiAlias = true;

				return bitmap;				
			}
		}
Exemple #20
0
        protected override void OnDraw(Canvas canvas)
        {
            int mBorderWidth;
            int mCanvasSize;
            Bitmap mImage;
            Paint mPaint;
            Paint mPaintBorder;

            mPaint = new Paint();
            mPaint.AntiAlias = true;
            mPaintBorder = new Paint();
            mPaintBorder.Color = BorderColor;
            mPaint.AntiAlias = true;
            mImage = DrawableToBitmap(Drawable);
            mBorderWidth = 2;

            if (mImage != null)
            {
                mCanvasSize = canvas.Width;

                if (canvas.Height < mCanvasSize)
                {
                    mCanvasSize = canvas.Height;
                }

                Bitmap scaledBitmap = Bitmap.CreateScaledBitmap(mImage, mCanvasSize, mCanvasSize, true); //true --> apply the given scaleType.
                BitmapShader shader = new BitmapShader(scaledBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
                mPaint.SetShader(shader);

                int circleCenter = (mCanvasSize - (mBorderWidth * 2)) / 2;

                if (ImageBorderVisibility)
                    canvas.DrawCircle(circleCenter + mBorderWidth, circleCenter + mBorderWidth, ((mCanvasSize - (mBorderWidth * 2)) / 2) + mBorderWidth - 4.0f, mPaintBorder);

                canvas.DrawCircle(circleCenter + mBorderWidth, circleCenter + mBorderWidth, ((mCanvasSize - (mBorderWidth * 2)) / 2) - 4.0f, mPaint);
            }
        }
        public Bitmap Transform(Bitmap source)
        {
            Bitmap result = Bitmap.CreateBitmap(source.Width, source.Height, source.GetConfig());
            Bitmap noise;
            try
            {
                noise = picasso.Load(Resource.Drawable.noise).Get();
            }
            catch (Exception)
            {
                throw new Exception("Failed to apply transformation! Missing resource.");
            }

            BitmapShader shader = new BitmapShader(noise, Shader.TileMode.Repeat, Shader.TileMode.Repeat);

            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.SetSaturation(0);
            ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);

            Paint paint = new Paint(PaintFlags.AntiAlias);
            paint.SetColorFilter(filter);

            Canvas canvas = new Canvas(result);
            canvas.DrawBitmap(source, 0, 0, paint);

            paint.SetColorFilter(null);
            paint.SetShader(shader);
            paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Multiply));

            canvas.DrawRect(0, 0, canvas.Width, canvas.Height, paint);

            source.Recycle();
            noise.Recycle();

            return result;
        }
 /**
  * Reinitializes the shader texture used to fill in
  * the Circle upon drawing.
  */
 public void RefreshBitmapShader()
 {
     _shader = new BitmapShader(Bitmap.CreateScaledBitmap(_image, _canvasSize, _canvasSize, false), Shader.TileMode.Clamp, Shader.TileMode.Clamp);
 }
		protected override void OnDraw(Canvas canvas)
		{
			// load the bitmap
			image = drawableToBitmap(Drawable);

			// init shader
			if (image != null)
			{

				canvasSize = canvas.Width;
				if (canvas.Height < canvasSize)
				{
					canvasSize = canvas.Height;
				}

				BitmapShader shader = new BitmapShader(Bitmap.CreateScaledBitmap(image, canvasSize, canvasSize, false), Shader.TileMode.Clamp, Shader.TileMode.Clamp);
				paint.SetShader(shader);

				int circleCenter = (canvasSize - (borderWidth * 2)) / 2;
				canvas.DrawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) + borderWidth - 4.0f, paintBorder);
				canvas.DrawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
			}
		}
		private void setup() {
			if (!mReady) {
				mSetupPending = true;
				return;
			}

			if (mBitmap == null) {
				return;
			}

			mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

			mBitmapPaint.AntiAlias = true;
			mBitmapPaint.SetShader(mBitmapShader);

			mBorderPaint.SetStyle (Paint.Style.Stroke);
			mBorderPaint.AntiAlias = true;
			//bug ,报错
			var packageName = Context.Resources.GetString(Resource.String.app_name);
			mBorderPaint.Color = Context.Resources.GetColor(mBorderColor); 


			mBorderPaint.StrokeWidth = mBorderWidth;

			mFillPaint.SetStyle(Paint.Style.Fill);
			mFillPaint.AntiAlias=true;
		    mFillPaint.Color =  Context.Resources.GetColor(mFillColor);

			mBitmapHeight = mBitmap.Height;
			mBitmapWidth = mBitmap.Width;

			mBorderRect.Set(0, 0, Width, Height);
			mBorderRadius = Java.Lang.Math.Min((mBorderRect.Height()- mBorderWidth) / 2, (mBorderRect.Width() - mBorderWidth) / 2);
			mDrawableRect.Set(mBorderRect);

			if (!mBorderOverlay) {
				mDrawableRect.Inset(mBorderWidth, mBorderWidth);
			}
		
			mDrawableRadius = Java.Lang.Math.Min(mDrawableRect.Height() / 2, mDrawableRect.Width() / 2);

			updateShaderMatrix();
			Invalidate ();
		}
        private void Setup()
        {
            if (!_mReady)
            {
                _mSetupPending = true;
                return;
            }

            if (Width == 0 && Height == 0)
            {
                return;
            }

            if (_mBitmap == null)
            {
                Invalidate();
                return;
            }

            _mBitmapShader = new BitmapShader(_mBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

            _mBitmapPaint.AntiAlias = true;
            _mBitmapPaint.SetShader(_mBitmapShader);

            _mBorderPaint.SetStyle(Paint.Style.Stroke);
            _mBorderPaint.AntiAlias = true;
            _mBorderPaint.Color = new Color(_mBorderColor);
            _mBorderPaint.StrokeWidth = _mBorderWidth;

            _mFillPaint.SetStyle(Paint.Style.Fill);
            _mFillPaint.AntiAlias = true;
            _mFillPaint.Color = new Color(_mFillColor);

            _mBitmapHeight = _mBitmap.Height;
            _mBitmapWidth = _mBitmap.Width;

            _mBorderRect.Set(0, 0, Width, Height);
            _mBorderRadius = Math.Min((_mBorderRect.Height() - _mBorderWidth)/2.0f,
                (_mBorderRect.Width() - _mBorderWidth)/2.0f);

            _mDrawableRect.Set(_mBorderRect);
            if (!_mBorderOverlay)
            {
                _mDrawableRect.Inset(_mBorderWidth, _mBorderWidth);
            }
            _mDrawableRadius = Math.Min(_mDrawableRect.Height()/2.0f, _mDrawableRect.Width()/2.0f);

            UpdateShaderMatrix();
            Invalidate();
        }
		public static Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, 
			CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio)
		{
			double sourceWidth = source.Width;
			double sourceHeight = source.Height;

			double desiredWidth = sourceWidth;
			double desiredHeight = sourceHeight;

			double desiredRatio = cropWidthRatio / cropHeightRatio;
			double currentRatio = sourceWidth / sourceHeight;

			if (currentRatio > desiredRatio)
			{
				desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
			}
			else if (currentRatio < desiredRatio)
			{
				desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
			}

			topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
			topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
			bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
			bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;

			float cropX = (float)((sourceWidth - desiredWidth) / 2);
			float cropY = (float)((sourceHeight - desiredHeight) / 2);

			Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888);

			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			using (var path = new Path())
			{
				if (cropX != 0 || cropY != 0)
				{
					matrix.SetTranslate(-cropX, -cropY);
					shader.SetLocalMatrix(matrix);
				}

				paint.SetShader(shader);
				paint.AntiAlias = true;

				// TopLeft
				if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) 
				{
					path.MoveTo(0, (float)topLeftCornerSize);
					path.LineTo((float)topLeftCornerSize, 0);
				}
				else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) 
				{
					path.MoveTo(0, (float)topLeftCornerSize);
					path.QuadTo(0, 0, (float)topLeftCornerSize, 0);
				}
				else
				{
					path.MoveTo(0, 0);
				}

				// TopRight
				if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) 
				{
					path.LineTo((float)(desiredWidth - topRightCornerSize), 0);
					path.LineTo((float)desiredWidth, (float)topRightCornerSize);
				}
				else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded))
				{
					path.LineTo((float)(desiredWidth - topRightCornerSize), 0);
					path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize);
				}
				else
				{
					path.LineTo((float)desiredWidth ,0);
				}

				// BottomRight
				if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) 
				{
					path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize));
					path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight);
				}
				else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded))
				{
					path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize));
					path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight);
				}
				else
				{
					path.LineTo((float)desiredWidth, (float)desiredHeight);
				}

				// BottomLeft
				if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) 
				{
					path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight);
					path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize));
				}
				else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) 
				{
					path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight);
					path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize));
				}
				else
				{
					path.LineTo(0, (float)desiredHeight);
				}

				path.Close();
				canvas.DrawPath(path, paint);

				return bitmap;				
			}
		}
Exemple #27
0
        private void setup()
        {
            if (!mReady)
            {
                mSetupPending = true;
                return;
            }

            if (mBitmap == null)
            {
                return;
            }

            mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

            mBitmapPaint.AntiAlias = true;
            mBitmapPaint.SetShader(mBitmapShader);

            mBorderPaint.SetStyle(Paint.Style.Stroke);
            mBorderPaint.AntiAlias = true;
            mBorderPaint.Color = mBorderColor;
            mBorderPaint.StrokeWidth = _borderWidth;

            mBitmapHeight = mBitmap.Height;
            mBitmapWidth = mBitmap.Width;

            mBorderRect.Set(0, 0, this.Width, this.Height);

            mBorderRadius = Java.Lang.Math.Min((mBorderRect.Height() - _borderWidth) / 2, (mBorderRect.Width() - _borderWidth) / 2);

            mDrawableRect.Set(_borderWidth, _borderWidth, mBorderRect.Width() - _borderWidth, mBorderRect.Height() - _borderWidth);
            mDrawableRadius = Java.Lang.Math.Min(mDrawableRect.Height() / 2, mDrawableRect.Width() / 2);

            updateShaderMatrix();
            Invalidate();
        }