現在Xamarin FormsのPCL環境でiOSアプリを開発しています。
グラデーションのかかった直線の描画をしたいと思って、色々調べているのですがいまいち解りません。
描画方法をご教授いただけますと助かります。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
直線は細長い四角形で実現したいと思います。その四角形を
CoreGraphics.CGContext.DrawLinearGradient Method を使ってグラデーションで塗ることにします。
「Creating Mobile Apps with Xamarin.Forms」の「Chapter 27. Custom renderers」を参考に、 View のカスタムレンダラーを作って、Draw をオーバーライドします。色を指定できるようにプロパティ Color1 と Color2 を作ってみました。
DrawLinearGradientView.cs
C#
1using Xamarin.Forms; 2 3namespace DrawLinearGradientTest 4{ 5 public class DrawLinearGradientView : View 6 { 7 public Color Color1 { get; set; } 8 public Color Color2 { get; set; } 9 } 10}
DrawLinearGradientViewRenderer.cs
C#
1using CoreGraphics; 2using DrawLinearGradientTest; 3using DrawLinearGradientTest.iOS; 4using UIKit; 5using Xamarin.Forms; 6using Xamarin.Forms.Platform.iOS; 7 8[assembly: ExportRenderer(typeof(DrawLinearGradientView), typeof(DrawLinearGradientViewRenderer))] 9namespace DrawLinearGradientTest.iOS 10{ 11 public class DrawLinearGradientViewRenderer : ViewRenderer<DrawLinearGradientView, UIView> 12 { 13 public CGColor Color1 { get; set; } 14 public CGColor Color2 { get; set; } 15 16 protected override void OnElementChanged(ElementChangedEventArgs<DrawLinearGradientView> e) 17 { 18 base.OnElementChanged(e); 19 20 Color1 = Element.Color1.ToCGColor(); 21 Color2 = Element.Color2.ToCGColor(); 22 } 23 24 public override void Draw(CGRect rect) 25 { 26 base.Draw(rect); 27 28 using (var context = UIGraphics.GetCurrentContext()) 29 { 30 context.DrawLinearGradient( 31 new CGGradient(CGColorSpace.CreateDeviceRGB(), new [] { Color1, Color2 }), 32 new CGPoint(0, 0), 33 new CGPoint(rect.Width, rect.Height), 34 CGGradientDrawingOptions.None 35 ); 36 } 37 } 38 } 39}
MainPage.xaml
XML
1<?xml version="1.0" encoding="utf-8" ?> 2<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 3 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 4 xmlns:local="clr-namespace:DrawLinearGradientTest;assembly=DrawLinearGradientTest" 5 x:Class="DrawLinearGradientTest.MainPage"> 6 <ContentPage.Content> 7 <AbsoluteLayout> 8 <local:DrawLinearGradientView 9 AbsoluteLayout.LayoutBounds="0.5,0.5,200,2" AbsoluteLayout.LayoutFlags="PositionProportional" 10 Color1="#ff0000" Color2="#00ff00"/> 11 <local:DrawLinearGradientView 12 AbsoluteLayout.LayoutBounds="0.5,0.5,200,2" AbsoluteLayout.LayoutFlags="PositionProportional" 13 TranslationX="50" TranslationY="-86.6" Rotation="60" 14 Color1="#0000ff" Color2="#00ff00"/> 15 <local:DrawLinearGradientView 16 AbsoluteLayout.LayoutBounds="0.5,0.5,200,2" AbsoluteLayout.LayoutFlags="PositionProportional" 17 TranslationX="-50" TranslationY="-86.6" Rotation="-60" 18 Color1="#ff0000" Color2="#0000ff"/> 19 </AbsoluteLayout> 20 </ContentPage.Content> 21</ContentPage>
投稿2017/10/11 05:39
総合スコア374
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/18 02:26