Programing

Android 그라디언트의 각도 속성

lottogame 2020. 11. 23. 07:36
반응형

Android 그라디언트의 각도 속성


테스트 예제를 살펴 보겠습니다. 그라디언트를 사용하는 일부 이미지 배경의 경우 코드는 다음과 같습니다.

<?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="180"/>
    <corners android:radius="5dp" />
   </shape>

위의 xml에서 angle속성을 얻지 못했습니다 . 하지만 angle패턴 경사 의 값을 약간 변경하면 . 누구든지 정확히 어떻게 작동하는지 설명 할 수 있습니까?


Gradient는 기본적으로 모든 수량의 공간 (방향) 변화를 나타냅니다. 색상은 각도로 표시되는 방향으로 색상 강도의 변화를 나타냅니다. 이 개념을 나타내는 몇 가지 다이어그램은 다음과 같습니다.
여기에 이미지 설명 입력

여기 그림은 수평 방향의 색상 변화를 보여줍니다 (각도는 0으로 설정 됨).
XML 코드 :

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#000000"
        android:angle="0"/>
   </shape>

여기에 이미지 설명 입력

여기 그림은 수직 방향의 색상 변화를 보여줍니다 (각도는 90으로 설정 됨).
XML 코드 :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#000000"
    android:angle="90"/>
 </shape>

시작, 중앙 및 끝 색상으로 다른 색상을 사용할 수도 있습니다. 첨부 한 코드에는 이러한 모든 요소가 포함되어 있습니다.


코드에서 대각선 그라디언트를 만들고 싶을 수 있습니다. 훨씬 쉽고 거기에서 많은 옵션을 열 수 있습니다. 이 스 니펫은 나를 도왔습니다

public void SetGradient(View view) {
        GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TL_BR,
                new int[]{0xFF141a24, 0xFF293f49, 0xFF72554c});
        view.setBackground(gd);
    }

GradientDrawable 클래스에서 사용 가능한 방향

/*public enum Orientation {
        *//** draw the gradient from the top to the bottom *//*
        TOP_BOTTOM,
        *//** draw the gradient from the top-right to the bottom-left *//*
        TR_BL,
        *//** draw the gradient from the right to the left *//*
        RIGHT_LEFT,
        *//** draw the gradient from the bottom-right to the top-left *//*
        BR_TL,
        *//** draw the gradient from the bottom to the top *//*
        BOTTOM_TOP,
        *//** draw the gradient from the bottom-left to the top-right *//*
        BL_TR,
        *//** draw the gradient from the left to the right *//*
        LEFT_RIGHT,
        *//** draw the gradient from the top-left to the bottom-right *//*
        TL_BR,
    }*/

그리고 조각에서 onCreate 또는 onCreateView에서 메서드를 호출하고 상위 뷰를 전달합니다 (제 경우).

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_view_parent, container);           
        ...

        SetGradient(view);

        return view;
    }

모양의 그라데이션 색상을 지정합니다. 속성 :

android : angle 정수. 기울기 각도 (도)입니다. 0은 왼쪽에서 오른쪽, 90은 아래쪽에서 위쪽입니다. 45의 배수 여야합니다. 기본값은 0입니다.

문서의 설명이 karn의 대답과 모순되는 것 같습니다 ??

문서 에서 자세한 내용을 찾을 수 있습니다.

참고 URL : https://stackoverflow.com/questions/12153890/angle-attribute-in-android-gradient

반응형