欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?gradient?使用小結(jié)

 更新時(shí)間:2025年04月17日 17:10:21   作者:stevenzqzq  
在Android中使用gradient(漸變)通常是通過drawable文件來設(shè)置背景,下面是可以直接用的幾種用法匯總,包括線性漸變、徑向漸變、掃描漸變(sweep)等,感興趣的朋友一起看看吧

在 Android 中使用 gradient(漸變) 通常是通過 drawable 文件來設(shè)置背景。下面是可以直接用的幾種用法匯總,包括線性漸變、徑向漸變、掃描漸變(sweep)等:

? 1. Linear Gradient(線性漸變)

<!-- res/drawable/bg_gradient_linear.xml -->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:type="linear"
    android:startColor="#FF512F"
    android:endColor="#DD2476"
    android:angle="45" />

?? angle 取值范圍:0~360,表示漸變方向(0 為從上往下,90 為從左往右)。

? 2. Radial Gradient(徑向漸變)

<!-- res/drawable/bg_gradient_radial.xml -->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:type="radial"
    android:centerX="0.5"
    android:centerY="0.5"
    android:gradientRadius="200"
    android:startColor="#FF512F"
    android:endColor="#DD2476" />

?? centerX/Y: 百分比(0.5 表示中心)
?? gradientRadius: 漸變半徑,單位為 px

? 3. Sweep Gradient(掃描漸變)

<!-- res/drawable/bg_gradient_sweep.xml -->
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:type="sweep"
    android:centerX="0.5"
    android:centerY="0.5"
    android:startColor="#FF512F"
    android:endColor="#DD2476" />

? 4. 多色漸變

<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:type="linear"
    android:startColor="#FF512F"
    android:centerColor="#F09819"
    android:endColor="#DD2476"
    android:angle="90" />

? 5. 設(shè)置背景到 View

android:background="@drawable/bg_gradient_linear"

? 6. 代碼中創(chuàng)建 GradientDrawable

val gradient = GradientDrawable(
    GradientDrawable.Orientation.LEFT_RIGHT,
    intArrayOf(Color.RED, Color.BLUE)
)
gradient.cornerRadius = 20f
yourView.background = gradient

? 7. 圓角 + 漸變(常用)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="16dp"/>
    <gradient
        android:type="linear"
        android:startColor="#FF512F"
        android:endColor="#DD2476"
        android:angle="90"/>
</shape>

android:angle 方向圖解

在 Android 的 gradient 中使用 android:angle 屬性時(shí),它控制漸變的方向。它的單位是角度,**以“從左到右順時(shí)針旋轉(zhuǎn)”**為標(biāo)準(zhǔn)。

? android:angle 方向圖解(基于 type="linear"

angle漸變方向說明(startColor ? endColor
0從左 ? 右左邊是 startColor,右邊是 endColor
45從左下 ? 右上斜向上漸變
90從下 ? 上下邊是 startColor,上邊是 endColor
135從右下 ? 左上斜向上漸變
180從右 ? 左右邊是 startColor,左邊是 endColor
225從右上 ? 左下斜向下漸變
270從上 ? 下上邊是 startColor,下邊是 endColor
315從左上 ? 右下斜向下漸變

? 舉例說明:

<gradient
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:angle="90"/>

上面是 從下往上 漸變(即底部是紅色,頂部是藍(lán)色),不是從左到右!這也是 Android 和 CSS 的一個(gè)差異點(diǎn),容易混淆。

?注意:

  • angle 必須是 45 的整數(shù)倍,否則會(huì)被忽略或默認(rèn)處理。
  • angle 的值是 順時(shí)針旋轉(zhuǎn)角度,從 0 度(從左 ? 右)開始

0:從左到右
45:從左下到右上
90:從下到上
135:從右下到左上
180:從右到左
225:從右上到左下
270:從上到下
315:從左上到右下

?圖示參考:

          ↑
    270°  ↑  90°
← 180° ←     → 0° →
         ↓

?? 注意:這個(gè)角度是 Android 中 定義漸變方向用的邏輯值,不是數(shù)學(xué)角度的坐標(biāo)方向。

? 示例一:從左到右漸變

<gradient
    android:type="linear"
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:angle="90"/>

?? 顏色從左(紅) → 右(藍(lán)) 漸變。

? 示例二:從上到下漸變

<gradient
    android:type="linear"
    android:startColor="#00FF00"
    android:endColor="#000000"
    android:angle="0"/>

?? 顏色從上(綠) → 下(黑) 漸變。

?? 注意事項(xiàng):

  • angle 只能是 45 的倍數(shù)(如 0、45、90、135…),否則 Android 會(huì)忽略。
  • 默認(rèn) angle0,也就是 從上到下
  • android:type="linear" 時(shí),angle 才生效。
  • 對(duì)于 radialsweep 類型,angle 不起作用。

radialsweep的區(qū)別

?? radial(放射狀漸變)

? 特點(diǎn):

  • 中心向外發(fā)散。
  • 漸變是圓形擴(kuò)散的效果。
  • 類似水波或聚光燈、光暈。

? 用法示例:

<gradient
    android:type="radial"
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:centerX="0.5"
    android:centerY="0.5"
    android:gradientRadius="200"/>

? 參數(shù)說明:

  • centerX / centerY:中心點(diǎn)位置(0~1,表示百分比)。
  • gradientRadius:漸變的半徑(必須設(shè)置)。
  • angle 無效!

? 視覺示意:

  漸變像個(gè)圓圈擴(kuò)散出去:
     R G B
     ↓↓↓
   ●●●●●●●●
   ●●◎◎◎●●
   ●●◎◎◎●●
   ●●◎◎◎●●
   ●●●●●●●●

?? sweep(掃描/掃描狀漸變)

? 特點(diǎn):

  • 從中心點(diǎn)繞一圈旋轉(zhuǎn)(360°)改變顏色。
  • 類似時(shí)鐘的指針旋轉(zhuǎn)、雷達(dá)掃描。

? 用法示例:

<gradient
    android:type="sweep"
    android:startColor="#FF0000"
    android:endColor="#0000FF"
    android:centerX="0.5"
    android:centerY="0.5"/>

? 參數(shù)說明:

  • centerX / centerY:設(shè)置漸變中心。
  • 不支持 angle方向是固定的:從 0° 順時(shí)針到 360°。

? 視覺示意:

   色彩從中間繞一圈:
      紅 → 橙 → 黃
      ↑       ↓
     紫 ← 藍(lán) ← 綠

?? 總結(jié)對(duì)比表:

類型視覺效果可設(shè)置角度中心點(diǎn)常用場(chǎng)景
linear直線方向漸變? 支持?按鈕、背景、邊框線
radial中心向外擴(kuò)散? 不支持?光暈、聚光燈、圓形按鈕
sweep中心旋轉(zhuǎn)漸變? 不支持?表盤、雷達(dá)、加載動(dòng)畫

到此這篇關(guān)于Android gradient 使用的文章就介紹到這了,更多相關(guān)Android gradient 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論