如何在Xamarin android中实现线性渐变?

时间:2018-09-03 13:26:37

标签: xamarin.android

我想动态实现类似我在应用程序中附加的图像。但是我不知道如何在C#渐变enter image description here

中指定centercolor,startcolor,endcolor和angle。

如何在Xamarin android本机平台中以编程方式实现此目的

1 个答案:

答案 0 :(得分:0)

GradientActivity.cs

protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.GradientLayout);

    ImageView ivTest = FindViewById<ImageView>(Resource.Id.ivSquare);
    GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TrBl,  // "Top Right, Bottom Left"
        new int[] { new Color(Color.Red), new Color(Color.White), new Color(Color.Blue) });
    ivTest.Background = gd;
}

GradientLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@null"
    android:id="@+id/ivSquare" />
</RelativeLayout>

输出

Example Output