Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms Shadow on Frame in Android

The Frame class in Xamarin Forms is quite limited, and can't allow me to get a shadow behind the Frame. I've made a custom renderer for iOS using this code:

public class RatingInfoFrameRenderer : FrameRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);

        Layer.BorderColor = UIColor.White.CGColor;
        Layer.CornerRadius = 10;
        Layer.MasksToBounds = false;
        Layer.ShadowOffset = new CGSize(-2, 2);
        Layer.ShadowRadius = 5;
        Layer.ShadowOpacity = 0.4f;
    }
}

Making a similar one on Android is causing me problems, since my knowledge on Android native is kind of limited. Could anyone tell me what to look at, perhaps some good code example? I haven't found anything that looks similar to this.

like image 601
Mikkel Larsen Avatar asked Feb 28 '17 10:02

Mikkel Larsen


People also ask

What is IsClippedToBounds?

IsClippedToBounds property -Determines if the Layout should clip its children to its bounds. – This makes the image have a visual effect as if it were inside the Frame, adhering with all the properties that the Frame indicates, in this case, it takes the circular shape.

What is StackLayout in xamarin forms?

A StackLayout organizes child views in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout is oriented vertically. In addition, a StackLayout can be used as a parent layout that contains other child layouts.


2 Answers

It can be very easy in Android platform, but first of all, you need to create your shadow under Drawable folder of Android resources. For example:

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#CABBBBBB" />
      <corners android:radius="2dp" />
    </shape>
  </item>

  <item
      android:left="0dp"
      android:right="0dp"
      android:top="0dp"
      android:bottom="2dp">
    <shape android:shape="rectangle">
      <solid android:color="@android:color/white" />
      <corners android:radius="2dp" />
    </shape>
  </item>
</layer-list>

Name this file as "shadow.xml" and place it under the Drawable folder of Android project, then in your RatingInfoFrameRenderer:

public class RatingInfoFrameRenderer : FrameRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            ViewGroup.SetBackgroundResource(Resource.Drawable.shadow);
        }
    }
}

To change the style of shadow, you can modify the shadow.xml file, for more information about this, you may refer to google's official document: LayerList.

like image 105
Grace Feng Avatar answered Oct 12 '22 10:10

Grace Feng


I know this question is old, but there is an updated way of getting a better shadow effect then the accepted answer. inherit from Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer and then use SetOutlineSpotShadowColor(Color color) to set the shadow color. You can use CardElevation to determine the strength and spread of the shadow as well.

[assembly: ExportRenderer(typeof(Myframe), typeof(MyFrameRenderer))]
namespace MyApp.Droid.Renderers
{
    public class MyFrameRenderer: Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer
    {
        public MyFrameRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
        {
            base.OnElementChanged(e);

            CardElevation = 10;

            if(((App)Application.Current).Theme != Core.Enums.Theme.Dark)
            {
                SetOutlineSpotShadowColor(Xamarin.Forms.Color.Gray.ToAndroid());
            }
            else
            {
                SetOutlineSpotShadowColor(Xamarin.Forms.Color.HotPink.ToAndroid());
            }
        }
    }
}

Hope this helps someone who stumbles in here like I did.

like image 40
ottermatic Avatar answered Oct 12 '22 11:10

ottermatic