public Bitmap Transform(Bitmap source)
        {
            var size = Math.Min(source.Width, source.Height);

            var x = (source.Width - size) / 2;
            var y = (source.Height - size) / 2;

            var squaredBitmap = Bitmap.CreateBitmap(source, x, y, size, size);

            if (squaredBitmap != source)
            {
                source.Recycle();
            }

            var bitmap = Bitmap.CreateBitmap(size, size, source.GetConfig());

            var canvas = new Canvas(bitmap);
            var paint  = new Paint();
            var shader = new BitmapShader(squaredBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

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

            var r = size / 2f;

            canvas.DrawCircle(r, r, r, paint);

            squaredBitmap.Recycle();
            return(bitmap ?? source);
        }
 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;// SetAntiAlias(true);
     mBitmapPaint.SetShader(mBitmapShader);
     mBorderPaint.SetStyle(Paint.Style.Stroke);
     mBorderPaint.AntiAlias   = true;           //.SetAntiAlias(true);
     mBorderPaint.Color       = mBorderColor;   //.SetColor(mBorderColor);
     mBorderPaint.StrokeWidth = mBorderWidth;   //  .SetStrokeWidth(mBorderWidth);
     mBitmapHeight            = mBitmap.Height; // .getHeight();
     mBitmapWidth             = mBitmap.Width;  //.getWidth();
     mBorderRect.Set(0, 0, Width, Height);      //  getWidth(), getHeight());
     mBorderRadius = Java.Lang.Math.Min((mBorderRect.Height() - mBorderWidth) / 2,
                                        (mBorderRect.Width() - mBorderWidth) / 2);
     mDrawableRect.Set(mBorderRect);
     if (!mBorderOverlay)
     {
         mDrawableRect.Inset(mBorderWidth, mBorderWidth);
     }
     mDrawableRadius = System.Math.Min(mDrawableRect.Height() / 2, mDrawableRect.Width() / 2);
     UpdateShaderMatrix();
     Invalidate();
 }
示例#3
0
        /**
         * 设置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);
        }
        public Stream GetRoundedFromSquareImage(Stream stream)
        {
            Stream result = null;

            using (Bitmap bitmap = BitmapFactory.DecodeStream(stream))
            {
                using (BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
                {
                    using (Paint paint = new Paint(PaintFlags.AntiAlias))
                    {
                        paint.SetShader(shader);
                        RectF rect = new RectF(0.0f, 0.0f, bitmap.Width, bitmap.Height);

                        using (Bitmap bitmapResult = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888))
                        {
                            using (Canvas canvas = new Canvas(bitmapResult))
                            {
                                canvas.DrawColor(Color.Transparent);
                                canvas.DrawRoundRect(rect, bitmap.Width / 2, bitmap.Height / 2, paint);

                                result = GetStreamFromBitmap(bitmapResult);
                            }
                            bitmapResult.Recycle();
                        }
                    }
                }
                bitmap.Recycle();
            }

            return(result);
        }
示例#5
0
        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);
        }
示例#6
0
        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 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);
        }
        public Bitmap Transform(Bitmap source)
        {
            int size = Math.Min(source.Width, source.Height);

            int x = (source.Width - size) / 2;
            int y = (source.Height - size) / 2;

            Bitmap squaredBitmap = Bitmap.CreateBitmap(source, x, y, size, size);

            if (squaredBitmap != source)
            {
                source.Recycle();
            }

            Bitmap bitmap = Bitmap.CreateBitmap(size, size, source.GetConfig());

            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(bitmap);
        }
示例#9
0
        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);
                        }
        }
示例#10
0
        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());
        }
示例#11
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 = mBorderWidth;

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

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

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

            UpdateShaderMatrix();
            Invalidate();
        }
示例#12
0
        public Vertices(Context context)
            : base(context)
        {
            Bitmap bm = GetResourceBitmap(R.drawable.beach);
            Shader s  = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

            mPaint.setShader(s);

            float w = bm.getWidth();
            float h = bm.getHeight();

            // construct our mesh
            setXY(mTexs, 0, w / 2, h / 2);
            setXY(mTexs, 1, 0, 0);
            setXY(mTexs, 2, w, 0);
            setXY(mTexs, 3, w, h);
            setXY(mTexs, 4, 0, h);

            setXY(mVerts, 0, w / 2, h / 2);
            setXY(mVerts, 1, 0, 0);
            setXY(mVerts, 2, w, 0);
            setXY(mVerts, 3, w, h);
            setXY(mVerts, 4, 0, h);

            mMatrix.setScale(0.8f, 0.8f);
            mMatrix.preTranslate(20, 20);
            mMatrix.invert(mInverse);
        }
        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);
            }
        }
        public Stream GetArcPartFromSquareImage(Stream ms, int partNumber, int nbParts)
        {
            Stream result = null;

            using (Bitmap bitmap = BitmapFactory.DecodeStream(ms))
            {
                int diameter = bitmap.Width; // (int)(AvatarSize * GetDensity());

                float arcAngle = 360 / nbParts;
                float arcStart = 90 + (partNumber - 1) * arcAngle;
                if (nbParts > 2)
                {
                    arcStart += (180 / nbParts);
                }

                float deltaX = (float)(diameter / 2 - ((diameter * Math.Cos(DegreeToRadian(arcStart + arcAngle / 2)) + diameter) / 2)) / 2;
                float deltaY = (float)(diameter / 2 - ((diameter * Math.Sin(DegreeToRadian(arcStart + arcAngle / 2)) + diameter) / 2)) / 2;

                float startX = (float)((diameter * Math.Cos(DegreeToRadian(arcStart)) + diameter) / 2);
                float startY = (float)((diameter * Math.Sin(DegreeToRadian(arcStart)) + diameter) / 2);

                //float endX = (float)((diameter * Math.Cos(DegreeToRadian(arcStart + arcAngle)) + diameter) / 2);
                //float endY = (float)((diameter * Math.Sin(DegreeToRadian(arcStart + arcAngle)) + diameter) / 2);

                using (Bitmap temp = Bitmap.CreateBitmap(diameter, diameter, Bitmap.Config.Argb8888))
                {
                    // First we get the bitmap part (we translate using deltaX/Y to have the center of the avatar)
                    using (BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
                    {
                        using (Paint paint = new Paint(PaintFlags.AntiAlias))
                        {
                            paint.SetShader(shader);

                            using (Android.Graphics.Path path = new Android.Graphics.Path())
                            {
                                using (Canvas canvas = new Canvas(temp))
                                {
                                    path.MoveTo(diameter / 2 + deltaX, diameter / 2 + deltaY);
                                    path.LineTo(startX + deltaX, startY + deltaY);
                                    path.AddArc(deltaX, deltaY, diameter + deltaX, diameter + deltaY, arcStart, arcAngle);
                                    path.LineTo(diameter / 2 + deltaX, diameter / 2 + deltaY);
                                    canvas.DrawPath(path, paint);

                                    //canvas.DrawArc(deltaX, deltaY, deltaX + diameter, deltaY + diameter, arcStart, arcAngle, true, paint);
                                }
                            }
                        }
                    }

                    // Now we translate back to have correct layout
                    result = GetTranslated(GetStreamFromBitmap(temp), -deltaX, -deltaY);

                    temp.Recycle();
                }
                bitmap.Recycle();
            }

            return(result);
        }
示例#15
0
        public static Bitmap ToRotatedBitmap(this Bitmap sourceBitmap, int rotationDegrees)
        {
            if (rotationDegrees == 0)
            {
                return(sourceBitmap);
            }

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

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

            var bitmap = Bitmap.CreateBitmap(width, height, sourceBitmap.GetConfig());

            using (var canvas = new Canvas(bitmap))
            {
                using (var paint = new Paint())
                {
                    using (var shader = new BitmapShader(sourceBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
                    {
                        using (var matrix = new Matrix())
                        {
                            canvas.Save();

                            switch (rotationDegrees)
                            {
                            case 90:
                                canvas.Rotate(rotationDegrees, width / 2, width / 2);
                                break;

                            case 270:
                                canvas.Rotate(rotationDegrees, height / 2, height / 2);
                                break;

                            default:
                                canvas.Rotate(rotationDegrees, width / 2, height / 2);
                                break;
                            }

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

            if (sourceBitmap.Handle == IntPtr.Zero || sourceBitmap.IsRecycled)
            {
                return(bitmap);
            }
            sourceBitmap.Recycle();
            sourceBitmap.Dispose();

            return(bitmap);
        }
        public static Bitmap ToRotated(Bitmap source, double degrees, bool ccw, bool resize)
        {
            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)
                            {
                                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);
                        }
        }
示例#17
0
 public CircleDrawable(Bitmap bitmap, int margin)
 {
     margin       = 0;
     bitmapShader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
     bitmapRect   = new RectF(margin, margin, bitmap.Width - margin, bitmap.Height - margin);
     //ÉèÖû­±Ê
     paint           = new Paint();
     paint.AntiAlias = true;
     paint.SetShader(bitmapShader);
 }
示例#18
0
 public CircleDrawable(Bitmap bmp)
 {
     this.bmp  = bmp;
     bmpShader = new BitmapShader(bmp, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
     paint     = new Paint {
         AntiAlias = true
     };
     paint.SetShader(bmpShader);
     oval = new RectF();
 }
示例#19
0
        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.TryDispose();
            }

            return(bitmap);
        }
 RoundedCornerDrawable(Bitmap bitmap, Resources r)
 {
     mBitmap       = bitmap;
     mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
     mBitmapWidth  = bitmap.GetScaledWidth(r.DisplayMetrics);
     mBitmapHeight = bitmap.GetScaledHeight(r.DisplayMetrics);
     mBitmapRect.Set(0, 0, mBitmapWidth, mBitmapHeight);
     mBitmapPaint = new Paint(PaintFlags.AntiAlias);
     mBitmapPaint.SetStyle(Paint.Style.Fill);
     mBitmapPaint.SetShader(mBitmapShader);
 }
示例#21
0
        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);
                    }
                }
            }
        }
示例#22
0
        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);
        }
示例#23
0
        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(CalculateBounds());
            mBorderRadius = Math.Min((mBorderRect.Height() - mBorderWidth) / 2.0f,
                                     (mBorderRect.Width() - mBorderWidth) / 2.0f);

            mDrawableRect.Set(mBorderRect);
            if (!mBorderOverlay && mBorderWidth > 0)
            {
                mDrawableRect.Inset(mBorderWidth - 1.0f, mBorderWidth - 1.0f);
            }
            mDrawableRadius = Math.Min(mDrawableRect.Height() / 2.0f, mDrawableRect.Width() / 2.0f);

            ApplyColorFilter();
            UpdateShaderMatrix();
            Invalidate();
        }
示例#24
0
        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();
        }
示例#25
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;
            //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();
        }
示例#26
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;
        }
示例#27
0
        private static Bitmap MakeCircle(Bitmap bitmap)
        {
            float width        = bitmap.Width;
            float height       = bitmap.Width;
            var   circleBitmap = Bitmap.CreateBitmap((int)width + 50, (int)height + 50, Bitmap.Config.Argb8888);
            var   shader       = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);

            var paint = new Paint();

            paint.SetShader(shader);
            paint.SetStyle(Paint.Style.Fill);
            paint.AntiAlias = true;
            var canvas = new Canvas(circleBitmap);

            canvas.DrawCircle(width / 2, height / 2, width / 2, paint);
            return(circleBitmap);
        }
        /// <summary>
        /// Rounds bitmap corners.
        /// </summary>
        /// <param name="bitmap">Bitmap to process.</param>
        /// <param name="ratioToRoundX">Ratio of radius to round in dp to width-side in dp.</param>
        /// <param name="ratioToRoundY">Ratio of radius to round in dp to height-side in dp.</param>
        /// <returns>Bitmap with rounded corners.</returns>
        public static Bitmap Round(this Bitmap bitmap, float ratioToRoundX, float ratioToRoundY)
        {
            var roundedBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, bitmap.GetConfig());

            using (var canvas = new Canvas(roundedBitmap))
                using (var shader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
                    using (var paint = new Paint(PaintFlags.AntiAlias))
                        using (var rectangle = new RectF(0, 0, bitmap.Width, bitmap.Height))
                        {
                            paint.SetShader(shader);
                            var radiusToRoundX = ratioToRoundX * bitmap.Width;
                            var radiusToRoundY = ratioToRoundY * bitmap.Height;

                            canvas.DrawRoundRect(rectangle, radiusToRoundX, radiusToRoundY, paint);

                            return(roundedBitmap);
                        }
        }
示例#29
0
        /**
         * Reinitializes the shader texture used to fill in
         * the Circle upon drawing.
         */
        public void RefreshBitmapShader()
        {
            try
            {
                if (_shader != null)
                {
                    return;
                }

                Bitmap scaledBitmap = Bitmap.CreateScaledBitmap(_image, _canvasSize, _canvasSize, false);
                _shader = new BitmapShader(scaledBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
            }
            catch (ArrayIndexOutOfBoundsException ex)
            {
                var logger = LogManager.Get(typeof(CircularImageView));
                logger.Error(ex);
            }
        }
示例#30
0
        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);
        }