shuhelohelo’s blog

Xamarin.Forms多めです.

アバターを自動生成してくれるサービス

海外のアニメっぽいアバター画像を自動生成してくれるサービス.

アプリケーションの作成でサンプルのアバター画像が必要になることが多いのでかなり役に立ちます.

robohash.org

https://robohash.org/の後に適当な文字列(本当になんでもいい)を指定すると,画像が表示されます.

例えば,https://robohash.org/qwertyuiと渡すと,以下のような画像が表示されます.

https://robohash.org/qwertyui

これを使って,Xamarin.FormsのListViewでImageコントロールで表示させると,こんな感じに表示されます.

f:id:shuhelohelo:20191201123047p:plain

XAML:

            <ListView ItemsSource="{Binding People}"
                      HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding AvatarUrl}"/>
                                <StackLayout>
                                    <Label Text="{Binding FirstName}" FontSize="18"/>
                                    <Label Text="{Binding LastName}" HorizontalOptions="End"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

コードビハインド: FirstNameプロパティ,LastNameプロパティを使ってURLを作成している. ちなみにクエリパラメータのset=set4というのは「猫の画像」を指定している.

public string AvatarUrl => $"https://robohash.org/{LastName}-{FirstName}?set=set4";