protected override Bitmap Transform(Bitmap source) { int size = Math.Min(source.Width, source.Height); int width = (source.Width - size) / 2; int height = (source.Height - size) / 2; Bitmap bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp); if (width != 0 || height != 0) { // source isn't square, move viewport to centre Matrix 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); }
private void DrawRoundedBlurredBitmap(Canvas canvas, Bitmap blurredBitmap, int overlayColor) { if (blurredBitmap != null) { InternalLogger.Debug( $"BlurView@{GetHashCode()}", $"DrawRoundedBlurredBitmap( mCornerRadius: {mCornerRadius}, mOverlayColor: {mOverlayColor} )"); var mRectF = new RectF { Right = Width, Bottom = Height }; mPaint.Reset(); mPaint.AntiAlias = true; BitmapShader shader = new BitmapShader(blurredBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp); Matrix matrix = new Matrix(); matrix.PostScale(mRectF.Width() / blurredBitmap.Width, mRectF.Height() / blurredBitmap.Height); shader.SetLocalMatrix(matrix); mPaint.SetShader(shader); canvas.DrawRoundRect(mRectF, mCornerRadius, mCornerRadius, mPaint); mPaint.Reset(); mPaint.AntiAlias = true; mPaint.Color = new Color(overlayColor); canvas.DrawRoundRect(mRectF, mCornerRadius, mCornerRadius, mPaint); } }
protected override void OnDraw(Canvas canvas) { Bitmap rawBitmap = getBitmap(this.Drawable); if (rawBitmap != null) { int viewWidth = Width; int viewHeight = Height; int viewMinSize = Math.Min(viewWidth, viewHeight); float dstWidth = viewMinSize; float dstHeight = viewMinSize; if (mShader == null || !rawBitmap.Equals(mRawBitmap)) { mRawBitmap = rawBitmap; mShader = new BitmapShader(mRawBitmap, TileMode.Clamp, TileMode.Clamp); } if (mShader != null) { mMatrix.SetScale(dstWidth / rawBitmap.Width, dstHeight / rawBitmap.Height); mShader.SetLocalMatrix(mMatrix); } mPaintBitmap.SetShader(mShader); float radius = viewMinSize / 2.0f; canvas.DrawCircle(radius, radius, radius, mPaintBitmap); } else { base.OnDraw(canvas); } }
public static Bitmap ToCropped(Bitmap source, double xOffset, double yOffset, double Width, double Height) { var config = source.GetConfig(); if (config == null) { config = Bitmap.Config.Argb8888; // This will support transparency } Bitmap bitmap = Bitmap.CreateBitmap((int)(source.Width * Width), (int)(source.Height * Height), config); 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 (xOffset != 0 || yOffset != 0) { matrix.SetTranslate((float)(-xOffset * source.Width), (float)(-yOffset * source.Height)); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = false; RectF rectF = new RectF(0, 0, (float)(source.Width * Width), (float)(source.Height * Height)); canvas.DrawRect(rectF, paint); return(bitmap); } }
/** * 设置BitmapShader */ private void SetBitmapShader() { Drawable drawable = Drawable; if (null == drawable) { return; } Bitmap bitmap = DrawableToBitmap(drawable); // 将bitmap作为着色器来创建一个BitmapShader mBitmapShader = new BitmapShader(bitmap, TileMode.Clamp, TileMode.Clamp); float scale = 1.0f; if (mType == TYPE_CIRCLE) { // 拿到bitmap宽或高的小值 int bSize = Math.Min(bitmap.Width, bitmap.Height); scale = mWidth * 1.0f / bSize; } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) { // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值; scale = Math.Max(Width * 1.0f / bitmap.Width, Height * 1.0f / bitmap.Height); } // shader的变换矩阵,我们这里主要用于放大或者缩小 mMatrix.SetScale(scale, scale); // 设置变换矩阵 mBitmapShader.SetLocalMatrix(mMatrix); mPaint.SetShader(mBitmapShader); }
protected override void OnBoundsChange(Rect bounds) { base.OnBoundsChange(bounds); rect.Set(margin, margin, bounds.Width() - margin, bounds.Height() - margin); //µ÷Õûλͼ£¬ÉèÖþØÕó Matrix shaderMatrix = new Matrix(); shaderMatrix.SetRectToRect(bitmapRect, rect, Matrix.ScaleToFit.Fill); //ÉèÖþØÕó×ÅÉ«Æ÷ bitmapShader.SetLocalMatrix(shaderMatrix); }
public override void Draw(Canvas canvas) { if (_rebuildShader) { var bitmapShader = new BitmapShader(_bitmap, _tileModeX, _tileModeY); if (_tileModeX == Shader.TileMode.Clamp && _tileModeY == Shader.TileMode.Clamp) { bitmapShader.SetLocalMatrix(_shaderMatrix); } _bitmapPaint.SetShader(bitmapShader); _rebuildShader = false; } if (_oval) { if (_borderWidth > 0) { canvas.DrawOval(_drawableRect, _bitmapPaint); canvas.DrawOval(_borderRect, _borderPaint); } else { canvas.DrawOval(_drawableRect, _bitmapPaint); } } else { if (any(_cornersRounded)) { float radius = _cornerRadius; if (_borderWidth > 0) { canvas.DrawRoundRect(_drawableRect, radius, radius, _bitmapPaint); canvas.DrawRoundRect(_borderRect, radius, radius, _borderPaint); RedrawBitmapForSquareCorners(canvas); RedrawBorderForSquareCorners(canvas); } else { canvas.DrawRoundRect(_drawableRect, radius, radius, _bitmapPaint); RedrawBitmapForSquareCorners(canvas); } } else { canvas.DrawRect(_drawableRect, _bitmapPaint); if (_borderWidth > 0) { canvas.DrawRect(_borderRect, _borderPaint); } } } }
protected override void OnBoundsChange(Rect bounds) { base.OnBoundsChange(bounds); var baseBitmap = Bitmap; var shader = new BitmapShader(baseBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp); var matrix = new Matrix(); matrix.SetScale(((float)bounds.Width()) / baseBitmap.Width, ((float)bounds.Height()) / baseBitmap.Height, 0.0f, 0.0f); shader.SetLocalMatrix(matrix); avatar.SetShader(shader); }
public Paint GetDefaultOuterRimPaint() { // Use a linear gradient to create the 3D effect LinearGradient verticalGradient = new LinearGradient(mOuterRimRect.Left, mOuterRimRect.Top, mOuterRimRect.Left, mOuterRimRect.Bottom, Color.Rgb(255, 255, 255), Color.Rgb(84, 90, 100), Shader.TileMode.Repeat); // Use a Bitmap shader for the metallic style Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.light_alu); BitmapShader aluminiumTile = new BitmapShader(bitmap, Shader.TileMode.Repeat, Shader.TileMode.Repeat); Matrix matrix = new Matrix(); matrix.SetScale(1.0f / bitmap.Width, 1.0f / bitmap.Height); aluminiumTile.SetLocalMatrix(matrix); Paint paint = new Paint(PaintFlags.AntiAlias); paint.SetShader(new ComposeShader(verticalGradient, aluminiumTile, PorterDuff.Mode.Multiply)); paint.FilterBitmap = true; return(paint); }
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); } } 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); } } }
private void UpdateShaderMatrix() { float scale; float dx = 0; float dy = 0; mShaderMatrix.Set(null); if (mBitmapWidth * mDrawableRect.Height() > mDrawableRect.Width() * mBitmapHeight) { scale = mDrawableRect.Height() / (float)mBitmapHeight; dx = (mDrawableRect.Width() - mBitmapWidth * scale) * 0.5f; } else { scale = mDrawableRect.Width() / (float)mBitmapWidth; dy = (mDrawableRect.Height() - mBitmapHeight * scale) * 0.5f; } mShaderMatrix.SetScale(scale, scale); mShaderMatrix.PostTranslate((int)(dx + 0.5f) + mDrawableRect.Left, (int)(dy + 0.5f) + mDrawableRect.Top); mBitmapShader.SetLocalMatrix(mShaderMatrix); }
private void UpdateShaderMatrix() { float scale; float dx = 0; float dy = 0; _mShaderMatrix.Set(null); if (_mBitmapWidth * _mDrawableRect.Height() > _mDrawableRect.Width() * _mBitmapHeight) { scale = _mDrawableRect.Height() / _mBitmapHeight; dx = (_mDrawableRect.Width() - _mBitmapWidth * scale) * 0.5f; } else { scale = _mDrawableRect.Width() / _mBitmapWidth; dy = (_mDrawableRect.Height() - _mBitmapHeight * scale) * 0.5f; } _mShaderMatrix.SetScale(scale, scale); _mShaderMatrix.PostTranslate((int)(dx + 0.5f) + _mDrawableRect.Left, (int)(dy + 0.5f) + _mDrawableRect.Top); _mBitmapShader.SetLocalMatrix(_mShaderMatrix); }
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); bitmap.HasAlpha = true; 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); } }
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); bitmap.HasAlpha = true; 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 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); } var config = source.GetConfig(); if (config == null) { config = Bitmap.Config.Argb8888; // This will support transparency } Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, config); 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 Bitmap Transform(Bitmap source) { int minEdge = Math.Min(source.Width, source.Height); int dx = (source.Width - minEdge) / 2; int dy = (source.Height - minEdge) / 2; // Init shader Shader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp); Matrix matrix = new Matrix(); matrix.SetTranslate(-dx, -dy); // Move the target area to center of the source bitmap shader.SetLocalMatrix(matrix); // Init paint Paint paint = new Paint(PaintFlags.AntiAlias); paint.SetShader(shader); // Create and draw circle bitmap Bitmap output = Bitmap.CreateBitmap(minEdge, minEdge, source.GetConfig()); Canvas canvas = new Canvas(output); canvas.DrawOval(new RectF(0, 0, minEdge, minEdge), paint); // Recycle the source bitmap, because we already generate a new one source.Recycle(); return(output); //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; }