텍스트 뷰의 배경을 원형 이미지로 지정하고, 배경 색상 채우기(안드로이드)

출처: Udacity

drawable/magnitude_circle.xml
(원모양 xml파일로 만들어주기)
<!-- Background circle for the magnitude value -->
<shape 
xmlns:android="http://schemas.android.com/apk/res/android"       
android:shape="oval">
    
<solid android:color="@color/magnitude1" />    
<size android:width="36dp" android:height="36dp" />    
<corners android:radius="18dp" />

</shape>


colors.xml
사용하고자 하는 색상 정의하기


<!-- Color for an earthquake with magnitude 0 and 2 -->
<color name="magnitude1">#4A7BA7</color>
<!-- Magnitude circle color for an earthquake with magnitude between 2 and 3 -->
<color name="magnitude2">#04B4B3</color>
<!-- Magnitude circle color for an earthquake with magnitude between 3 and 4 -->
<color name="magnitude3">#10CAC9</color>
<!-- Magnitude circle color for an earthquake with magnitude between 4 and 5 -->
<color name="magnitude4">#F5A623</color>
<!-- Magnitude circle color for an earthquake with magnitude between 5 and 6 -->
<color name="magnitude5">#FF7D50</color>
<!-- Magnitude circle color for an earthquake with magnitude between 6 and 7 -->
<color name="magnitude6">#FC6644</color>
<!-- Magnitude circle color for an earthquake with magnitude between 7 and 8 -->
<color name="magnitude7">#E75F40</color>
<!-- Magnitude circle color for an earthquake with magnitude between 8 and 9 -->
<color name="magnitude8">#E13A20</color>
<!-- Magnitude circle color for an earthquake with magnitude between 9 and 10 -->
<color name="magnitude9">#D93218</color>
<!-- Magnitude circle color for an earthquake with magnitude over 10 -->
<color name="magnitude10plus">#C03823</color>



텍스트뷰의 backbround속성 지정해주기

<TextView
    android:id="@+id/magnitude"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:layout_gravity="center_vertical"
    android:background="@drawable/magnitude_circle"
    android:fontFamily="sans-serif-medium"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    tools:text="8.9" />



그레디언트드로어블 정의 및 색 지정해주기

TextView magnitudeTextView = (TextView) listItemView.findViewById(R.id.magnitude);

GradientDrawable magnitudeCircle = (GradientDrawable) magnitudeTextView.getBackground();int magnitudeColor = getMagnitudeColor(currentEarthquake.getMagnitude());magnitudeCircle.setColor(magnitudeColor);


getMagnitudeColor메서드 정의

private int getMagnitudeColor(double magnitude) {
    int magnitudeColorResourceId;
    int magnitudeFloor = (int) Math.floor(magnitude);
    switch (magnitudeFloor) {
        case 0:
        case 1:
            magnitudeColorResourceId = R.color.magnitude1;
            break;
        case 2:
            magnitudeColorResourceId = R.color.magnitude2;
            break;
        case 3:
            magnitudeColorResourceId = R.color.magnitude3;
            break;
        case 4:
            magnitudeColorResourceId = R.color.magnitude4;
            break;
        case 5:
            magnitudeColorResourceId = R.color.magnitude5;
            break;
        case 6:
            magnitudeColorResourceId = R.color.magnitude6;
            break;
        case 7:
            magnitudeColorResourceId = R.color.magnitude7;
            break;
        case 8:
            magnitudeColorResourceId = R.color.magnitude8;
            break;
        case 9:
            magnitudeColorResourceId = R.color.magnitude9;
            break;
        default:
            magnitudeColorResourceId = R.color.magnitude10plus;
            break;
    }
    return ContextCompat.getColor(getContext(), magnitudeColorResourceId);}

댓글

  1. 좋은 글 감사합니다. 그런데 코드들이 바코드로 보이는것 같아요 그냥 하나의 검은화면에 소스코드들이 보이면 가독성이 많이 올라갈 것 같습니다

    답글삭제

댓글 쓰기

이 블로그의 인기 게시물

API 요청 URL(URI) 만들기(조합하기) -Uri.Builder사용하기-

TextView에 글자를 넣어주는 방법(setText, append)