Android xml 드로어 블 파일에서 원 모양을 정의하는 방법은 무엇입니까?
Android 용 XML에서 셰이프 정의 문서를 찾는 데 몇 가지 문제가 있습니다. XML 파일에 단색으로 채워진 간단한 원을 정의하여 레이아웃 파일에 포함하고 싶습니다.
안타깝게도 android.com의 문서는 Shape 클래스의 XML 속성을 다루지 않습니다. 원을 그리 려면 ArcShape 를 사용해야한다고 생각 하지만 원을 만드는 데 필요한 크기, 색상 또는 각도를 설정하는 방법에 대한 설명이 없습니다.
이것은 Android에서 드로어 블과 같은 간단한 원입니다.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
이것을보기 배경으로 설정하십시오.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>
단색 원 사용 :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#48b3ff"/>
</shape>
획이있는 솔리드 :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#199fff"/>
<stroke
android:width="2dp"
android:color="#444444"/>
</shape>
참고 : oval
모양을 원으로 표시 하려면 이 예제에서이 모양을 배경으로 사용하는보기가 정사각형이어야하거나 모양 태그 의 height
및 width
속성을 동일한 값 으로 설정해야 합니다.
단순 원용 코드
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2dp" android:color="#fff" />
<size android:width="80dp" android:height="80dp"/>
</shape>
Android SDK 샘플을 살펴보십시오. ApiDemos 프로젝트에는 몇 가지 예가 있습니다.
/ ApiDemos / res / drawable /
- black_box.xml
- shape_5.xml
- 기타
그라데이션 채우기가있는 원의 경우 다음과 같이 표시됩니다.
<? xml version = "1.0"encoding = "utf-8"?> <shape xmlns : android = "http://schemas.android.com/apk/res/android"android : shape = "oval"> <gradient android : startColor = "# FFFF0000"android : endColor = "# 80FF00FF" android : angle = "270"/> </ 모양>
VectorDrawable 을 아래와 같이 사용할 수 있습니다 .
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="64"
android:viewportWidth="64">
<path
android:fillColor="#ff00ff"
android:pathData="M22,32
A10,10 0 1,1 42,32
A10,10 0 1,1 22,32 Z" />
</vector>
위의 xml은 다음과 같이 렌더링됩니다.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- fill color -->
<solid android:color="@color/white" />
<!-- radius -->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- corners -->
<corners
android:radius="2dp"/>
</shape>
다음은 사전 재질에 대한 간단한 circle_background.xml입니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/color_accent_dark" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="@color/color_accent" />
</shape>
</item>
</selector>
'android:background="@drawable/circle_background"
버튼의 레이아웃 정의에 있는 속성과 함께 사용할 수 있습니다.
당신이 원하는 경우 원 이 등이
아래 코드를 사용해보십시오.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="10dp"
android:color="@color/white"/>
<gradient
android:startColor="@color/red"
android:centerColor="@color/red"
android:endColor="@color/red"
android:angle="270"/>
<size
android:width="250dp"
android:height="250dp"/>
</shape>
당신은 이것을 시도 할 수 있습니다-
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="700"
android:thickness="100dp"
android:useLevel="false">
<solid android:color="#CCC" />
</shape>
또한을 조정하여 원의 반경을 조정할 수 있습니다 android:thickness
.
어떤 이유로 내 ConstraintLayout 안에 원을 그릴 수 없었고 위의 답변을 사용할 수 없었습니다.
완벽하게 작동하는 것은 "Alt + 7"을 누를 때 나오는 텍스트가있는 간단한 TextView입니다.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0075bc"
android:textSize="40dp"
android:text="•"></TextView>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/text_color_green"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>
res / drawble / circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#e42828"/>
<stroke android:color="#3b91d7" android:width="5dp"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>
온라인 예 : https://code-android-example.blogspot.com/2019/07/how-to-circle-shape-in-xml-drawable-android.html
그냥 사용
ShapeDrawable circle = new ShapeDrawable( new OvalShape() );
'your programing' 카테고리의 다른 글
SO_REUSEADDR과 SO_REUSEPORT는 어떻게 다릅니 까? (0) | 2020.10.02 |
---|---|
Chrome 자동 완성 사용 중지 (0) | 2020.10.02 |
HTTP 헤더는 대소 문자를 구분합니까? (0) | 2020.10.02 |
Ruby의 "continue"에 해당 (0) | 2020.10.02 |
IEnumerable vs List-무엇을 사용합니까? (0) | 2020.10.02 |