Last active
June 1, 2019 12:06
-
-
Save TheAlphamerc/eac44faefd5780b27fc056662b3b5f8a to your computer and use it in GitHub Desktop.
Add background color to xamarin android view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.Graphics; | |
using Android.Graphics.Drawables; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Support.V4.Graphics.Drawable; | |
using Android.Views; | |
using Android.Widget; | |
using Xamarin.Forms.Platform.Android; | |
using static Android.Graphics.Shader; | |
namespace Droid.Extensions | |
{ | |
public static class BackgroundExtension | |
{ | |
public static LinearGradient CreateLinearGradient(Color startColor, Color endColor, Color centerColor, float width, float height) | |
{ | |
return new LinearGradient(0, height/2, width, height/2, | |
new int[] { startColor, centerColor, endColor }, | |
null, | |
TileMode.Mirror | |
); | |
} | |
public static Drawable CreateBackgroundColor(Color color, float cornerRadius) | |
{ | |
GradientDrawable drawble = new GradientDrawable(); | |
drawble.SetShape(ShapeType.Rectangle); | |
drawble.SetCornerRadius(cornerRadius); | |
drawble.SetColors(new int[] { color, color }); | |
return drawble; | |
} | |
public static Drawable CreateBackgroundGradient(Color startColor, Color endColor, Color centerColor, | |
float cornerRadius, GradientDrawable.Orientation orientation) | |
{ | |
GradientDrawable gradientDrawable = new GradientDrawable(); | |
gradientDrawable.SetShape(ShapeType.Rectangle); | |
gradientDrawable.SetCornerRadius(cornerRadius); | |
if (centerColor != Xamarin.Forms.Color.Transparent.ToAndroid()) | |
{ | |
gradientDrawable.SetColors(new int[] { startColor, | |
centerColor,endColor}); | |
} | |
else | |
{ | |
gradientDrawable.SetColors(new int[] { startColor, endColor }); | |
} | |
gradientDrawable.SetGradientType(GradientType.LinearGradient); | |
gradientDrawable.SetOrientation(orientation); | |
return gradientDrawable; | |
} | |
public static Drawable CreateBackgroundBitmap(string image, int height, int width, float cornerRadius) | |
{ | |
var bitmap = Application.Context.Resources.GetBitmap(image); | |
if (bitmap != null) | |
{ | |
width = (int)(bitmap.Width * height / bitmap.Height); | |
bitmap = Bitmap.CreateScaledBitmap(bitmap, width, height, false); | |
var dr = RoundedBitmapDrawableFactory.Create(Application.Context.Resources, bitmap); | |
dr.CornerRadius = cornerRadius; | |
dr.Mutate().SetAlpha(40); | |
return dr; | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.Content.Res; | |
using Android.Graphics; | |
using Android.Graphics.Drawables; | |
using Android.Graphics.Drawables.Shapes; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Xamarin.Forms.Platform.Android; | |
namespace Droid.Extensions | |
{ | |
public static class ShadowExtension | |
{ | |
public static Drawable CreateShadow(View view) | |
{ | |
int elevationValue = 10; | |
Rect shapeDrawablePadding = new Rect(); | |
shapeDrawablePadding.Left = elevationValue; | |
shapeDrawablePadding.Right = elevationValue; | |
shapeDrawablePadding.Top = elevationValue; | |
shapeDrawablePadding.Bottom = elevationValue; | |
int DY = 0; | |
ShapeDrawable shapeDrawable = new ShapeDrawable(); | |
shapeDrawable.SetPadding(shapeDrawablePadding); | |
shapeDrawable.Paint.Color = Color.Red; | |
shapeDrawable.Paint.SetShadowLayer(12 / 3, 0, DY, Color.Red); | |
view.SetLayerType(LayerType.Software, shapeDrawable.Paint); | |
shapeDrawable.Shape = new RoundRectShape(new float[] { 12, 12, 12, 12, 12, 12, 12, 12 }, null, null); | |
LayerDrawable drawable = new LayerDrawable(new Drawable[] { shapeDrawable }); | |
drawable.SetLayerInset(0, elevationValue, elevationValue * 2, elevationValue, elevationValue * 2); | |
drawable.SetAlpha(40); | |
return drawable; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment