Android

[안드로이드] 그라데이션 적용하기 / gradient / xml

유정주 2021. 7. 26. 00:10
반응형

그라데이션 / gradient

안드로이드에서 그라데이션을 적용하는 방법을 배워보겠습니다.

 

예제 프로젝트 Git

https://github.com/Yujeongju/blogExample/tree/2021-07-26-gradient

 

Android API reference

https://developer.android.com/reference/android/graphics/drawable/GradientDrawable

 

GradientDrawable  |  Android 개발자  |  Android Developers

 

developer.android.com


1. xml 파일 생성

drawble > New > Drawable Resourse File을 눌러줍니다.

 

파일 이름, Root element을 작성해주고 OK를 눌러줍니다.

Root element는 xml 생성 후에도 수정 가능합니다. (당연히 파일 이름도 변경 가능함)

 

2. xml 코드 작성

아래 xml 코드를 작성해 주세요.

두 가지 색상 그라데이션과 세 가지 색상 그라데이션의 차이점은 centerColor 속성의 유무입니다.

 

두 가지 색상의 그라데이션

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ff0000"
        android:endColor="#0000ff"
        android:angle="0"/>
</shape>

세 가지 색상의 그라데이션

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="0"/>
</shape>

startColor : 그라데이션 시작 색

centerColor : 그라데이션 가운데 색

endColor : 그라데이션 끝 색

angle : 그라데이션 각도

 

3. drawable 적용

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_1">
</LinearLayout>

background 등의 속성에 적용하여 사용하면 됩니다.


* 완성

 

그라데이션을 이용해서 다양한 디자인에 적용해보시길 바랍니다.

감사합니다!

 


아직은 초보 개발자입니다.

더 효율적인 코드 훈수 환영합니다!

공감 댓글 부탁드립니다.

반응형