LoginSignup
0
1

More than 1 year has passed since last update.

xmlのshapeタグって意外と自由度が高いっていう話 part3

Posted at

初めに

今回は、shapeタグの紹介記事第三弾になります
3個目はViewに一気対してこだわりを持たせられるgradientです。

本文

こちらは名前の通りグラデーションをつけることができ、色はstartColorcenterColorendColorの三種類を指定することがきます。
startColorスタート位置の色
centerColor中間地点の色
endColor最終地点の色を指定することができます。
色以外の要素となると
type
gradientRadius
centerX
centerY
angle
の5点があります

typeとはグラデーションの形を指定することができ、
linear
radial
sweep
の3種類があり、radial放射状に、sweep円形にグラデーションをかけることができ、typeを指定しない場合はlinearになり直線状のグラデーションがかかります。
gradientRadiusradialを使用してる時に半径を指定することができます。
centerXcenterYは0~1の間でグラデーションの場所を指定することができるので、放射状にグラデーションを表示している時などに便利です。
最後にangleです。
gradientはデフォルトだと左から右にグラデーションをかけるのでangleに0~360を指定することで方向を変更することができます。
各形状のサンプルコードと画像を置いておきます。


linear
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:centerColor="@color/white"
        android:endColor="@android:color/transparent"
        android:type="linear"
        android:startColor="@color/white" />
</shape>

スクリーンショット 2022-05-13 18.20.10.png


radial
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="@color/black"
        android:endColor="@android:color/white"
        android:type="radial"
        android:centerX="0.5"
        android:centerY="0.5"
        android:gradientRadius="200dp"
        android:startColor="@color/white" />
</shape>

スクリーンショット 2022-05-13 18.21.13.png


sweep
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="@color/black"
        android:endColor="@android:color/darker_gray"
        android:type="sweep"
        android:startColor="@color/white" />
</shape>

スクリーンショット 2022-05-13 18.22.08.png

最後に

Shapeタグを紹介していくシリーズは今回が最後になります。
自由度が高く、簡単に少しだけこだわりを持たせたり、見た目のグレードアップにつながるものなので皆さんもぜひ活用してみてください。
前回の記事はこちら:https://qiita.com/ryuji_sato/items/b2f1f716e686d26bc849

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1