[Android] XML을 통한 투명 그래디언트

4845 단어 Android
XML을 사용하여 다음 Drawable 에셋 준비
gradation.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
    >

<gradient
    android:angle="270"
    android:startColor="#ffff0000"
    android:endColor="#00ffffff"
    />

</shape>
첫 번째 ff는 "#ff0000"이고 00번째 "#00fff"는 투명도입니다.
배치에 다음과 같은 방식으로 배치하면 된다.

투명하기 때문에 아래처럼 다른 뷰에 겹칠 수도 있습니다.

그래디언트


상술한 방법을 적용하면 다음과 같은 점차적인 변화를 진행할 수 있다.

gradation.xml
<?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:startColor="#ffffffff"
        android:centerColor="#aaffffff"
        android:endColor="#00ffffff"
        />

</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="io.github.hkusu.sampleapp.MainActivity"
    >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_android_black_18dp"
            />

        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/gradation"
            />

    </FrameLayout>

</RelativeLayout>

좋은 웹페이지 즐겨찾기